Jump to content

[SOLVED] Limiting the length (truncating?) of results returned from query.


florapse

Recommended Posts

Hi Folks.

 

Sorry to bother, but I'm a hack at best when it comes to PHP and my normal google till I find the code approach is not working this time. I hope someone can jump in and give me a quick pointer or two.

 

Simply enough, the results returned from the $description field are too long for my design. I would like to shorten the length of characters / words returned and displayed. I've been playing with different code that I have found for hours and I still don't get it. Any help would be greatly appreciated. TIA!

 

Here is the code:

 

$sql = "SELECT * FROM $table order by category";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
	extract($row);
	echo "<table width='100%' border='0' cellspacing='0' cellpadding='3'> \n";
	echo "<tr>\n";
	echo "<td valign='top'>\n<a href='$link$aid'> <img src='$image' border=0> </a> \n <br> <b> Price: </b> $$price\n </td> \n";
	echo "<td valign='top'>\n<a href='$link$aid'> \n<b> $product </b> </a> \n<br> $description\n <br> <b> Item: </b> <i> $sku </i> \n <b> Category: </b> <i> $category </i> <br> </td>\n";
	echo "</tr>\n";
	echo "</table>\n";
	echo "<hr size='1'>\n";
}

Ure using a variable variable $$price which isnt the case and shouldnt you echo $row['price'] instead of $price? Anyway about the real deal:

 

<?php
$description = substr($description, 0, 100); //truncate to 100 chars
$lastDot = strrpos($description, '.'); //find the last occurrence of a dot
echo $description = substr($description, 0, $lastDot) . '..';
?>

 

Added the "find the dot" thing so your sentences have meaning.

 

EDIT: Sorry for the second part of my question (shouldnt it be $row['price']?) as i didnt notice the extract().

Excellent! Thanks for the quick reply. On top of your code, I also realized what I was doing wrong when I was trying things out. Like I said I'm a hack. But as long as I knwo someone is telling me the right thing to do, I can usually catch on and figure it out from there. Thanks again!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.