florapse Posted May 8, 2008 Share Posted May 8, 2008 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"; } Link to comment https://forums.phpfreaks.com/topic/104647-solved-limiting-the-length-truncating-of-results-returned-from-query/ Share on other sites More sharing options...
Fadion Posted May 8, 2008 Share Posted May 8, 2008 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(). Link to comment https://forums.phpfreaks.com/topic/104647-solved-limiting-the-length-truncating-of-results-returned-from-query/#findComment-535663 Share on other sites More sharing options...
florapse Posted May 8, 2008 Author Share Posted May 8, 2008 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! Link to comment https://forums.phpfreaks.com/topic/104647-solved-limiting-the-length-truncating-of-results-returned-from-query/#findComment-535701 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.