How to truncate multiple query results....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
noisenet
Forum Newbie
Posts: 2
Joined: Mon Jun 18, 2007 2:38 pm

How to truncate multiple query results....

Post by noisenet »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Heya,
I'm new here, as well as to PHP. I know the basics of what PHP can do, even methods by which to implement a lot of what I want, however my coding skills aren't yet up to par. Being impatient, I've turned to GUI based coding, such as Dreamweaver, to faster do what I want.

Here's my issue: I have a news section on my website, where the article(s) are stored in a MySQL database. Titles in one column, article body in another. On the actual page, I have a limited space to which to print the content, and need to truncate it so it does not overlap, or stretch the content div and blow the design out of whack.

When only returning one article from the db, I can do this easily, however, the problem is that not all articles are/will be lengthy enough to fill the div. In such cases, I want to return the next article, up to the remaining div space, then truncate it.

For example, right now, the article that's being displayed fills up more than the allotted space, so it's truncated. But the next three articles I post may be short, where I'd need all three plus a segment of a fourth to fill the space. Using Dreamweaver MX2k4 I can do this by "repeating that region", but limiting the number of characters is only applicable to each query, not to the overall div. As such, it won't work in the way I need.

Below is the code I'm using for this purpose...

Code: Select all

<?php
$maxRows_ssh_news = 1;
$pageNum_ssh_news = 0;
if (isset($_GET['pageNum_ssh_news'])) {
  $pageNum_ssh_news = $_GET['pageNum_ssh_news'];
}
$startRow_ssh_news = $pageNum_ssh_news * $maxRows_ssh_news;

mysql_select_db($database_ssh_db, $ssh_db);
$query_ssh_news = "SELECT * FROM newsContent ORDER BY id DESC";
$query_limit_ssh_news = sprintf("%s LIMIT %d, %d", $query_ssh_news, $startRow_ssh_news, $maxRows_ssh_news);
$ssh_news = mysql_query($query_limit_ssh_news, $ssh_db) or die(mysql_error());
$row_ssh_news = mysql_fetch_assoc($ssh_news);

if (isset($_GET['totalRows_ssh_news'])) {
  $totalRows_ssh_news = $_GET['totalRows_ssh_news'];
} else {
  $all_ssh_news = mysql_query($query_ssh_news);
  $totalRows_ssh_news = mysql_num_rows($all_ssh_news);
}
$totalPages_ssh_news = ceil($totalRows_ssh_news/$maxRows_ssh_news)-1;

?>

And in the page body:

Code: Select all

<?php do { ?>
      <span class="header"><?php echo $row_ssh_news['title']; ?></span><br>
      <span class="whitetext2"><?php 
	  if(strlen($row_ssh_news['content'])>2000){
echo substr($row_ssh_news['content'], 0, 2000) . '<span class="footnote">...(continued)</span>';
}
else {
echo $row_ssh_news['content'];
}
?>
</span><br><br>
	  <?php } while ($row_ssh_news = mysql_fetch_assoc($ssh_news)); ?>
    <div align="right"><span class="footnote">
	  <?php if ($pageNum_ssh_news < $totalPages_ssh_news) { // Show if not last page ?>
<a href="javascript:void" onClick="MM_openBrWindow('news_full.php','newsFull','scrollbars=yes,width=500,height=400')">View All <?php echo $totalRows_ssh_news ?> News Items... </a>
<?php } // Show if not last page ?>
As you can see, this limits the string length by query. If I had it set for a max of 4 items returned by changing the line {$maxRows_ssh_news = 1;}, the character count is reset at the beginning of each returned result.

As I'm an uber noob, I don't know if the data is being returned in a string (or strings), or as (an) array(s). I've wondered if I could use the implode function to kind of compress everything into one array, then limit character count, but I need titles and body text to appear in their rightful place, & as imploding would just create one big array of both title and body, that wouldn't work. So I'm stumped.....

Any info on how I can achieve this would be greatly appreciated! Thank you for taking the time to read this lengthy post from a total noob.......

To look at the page and see what I'm talking about for space limitations, you can go to http://www.superswamp.com The page I'm talking about is the index page.[/url]


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Thanks for pointing that out for me. I'll be sure to adhere to that in the future
Last edited by noisenet on Mon Jun 18, 2007 4:09 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Space limitation is a product of bad design. Everything should be flexible and dynamic.

I don't see at all why that website can't stretch. How many websites with news articles do you know of that don't scale vertically?
noisenet
Forum Newbie
Posts: 2
Joined: Mon Jun 18, 2007 2:38 pm

Post by noisenet »

While I understand your point, there are reasons for the design not being flexible. For one, as the index page, I want it concise, with not a lot of scrolling. It's basically a "quick hits" front page, that's it. As the site develops, other pages will be more flexible to their content.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Interesting. Though I don't agree, I do see your point and it's partially valid. :P

Anyway, what you'll want to do is save all of the articles into an array before you output them, then check their contents' lengths using a foreach loop. Then, calculate which ones are too short, and compensate. Maybe create a $limit variable for how many characters the whole page will hold, then subtract the length of content of each item from the limit. Then, determine which ones are too short, and divide the rest of the limit amongst the ones that are not.

Personally, I'd just use the overflow-y CSS attribute, but it doesn't look like that's what you're after.
Post Reply