johntp Posted September 4, 2008 Share Posted September 4, 2008 Hey guys, I have a query that shows a row in the table but i have it filtered to show only the first 10 words of one column. This works fine however I am trying to make it do a ... IF it had to cut off, but im lost on how to do so. Below is the code i use: <?php $query2 = "SELECT * FROM news WHERE Viewable = 'Yes' AND Section = '$department' ORDER BY RECNO DESC"; $result2 = mysql_query($query2); while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) { $body = $row2['News']; $length = 200; // The number of words you want $text = $body; $words = explode(' ', $text); // Creates an array of words $words = array_slice($words, 0, $length); $text = implode(' ', $words); echo "$text"; ?> Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/122701-solved-use-a-certain-amount-of-words-on-a-sql-query/ Share on other sites More sharing options...
JasonLewis Posted September 4, 2008 Share Posted September 4, 2008 Here is an easier way using substr(). <?php $query2 = "SELECT * FROM news WHERE Viewable = 'Yes' AND Section = '$department' ORDER BY RECNO DESC"; $result2 = mysql_query($query2); while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) { $body = $row2['News']; $length = 200; // The number of words you want $text = $body; $text = substr($text, 0,$length)."..."; echo $text; ?> Link to comment https://forums.phpfreaks.com/topic/122701-solved-use-a-certain-amount-of-words-on-a-sql-query/#findComment-633629 Share on other sites More sharing options...
johntp Posted September 4, 2008 Author Share Posted September 4, 2008 Thanks for the clean up code, it looks alot better less lines less hassle , but I still cannot get it to do what I want. I realize that it wasnt that clear in my original post. What i need is it to show the ... if it's more than 200 words, and not show the ... if it's less then 200 words. Link to comment https://forums.phpfreaks.com/topic/122701-solved-use-a-certain-amount-of-words-on-a-sql-query/#findComment-633640 Share on other sites More sharing options...
Hinty Posted September 4, 2008 Share Posted September 4, 2008 <?php $query2 = "SELECT * FROM news WHERE Viewable = 'Yes' AND Section = '$department' ORDER BY RECNO DESC"; $result2 = mysql_query($query2); while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) { $body = $row2['News']; $length = 200; // The number of words you want if (strlen($body) > $length) { echo substr($body, 0, $length)."..."; } else { echo $text; } } ?> That what you mean? Link to comment https://forums.phpfreaks.com/topic/122701-solved-use-a-certain-amount-of-words-on-a-sql-query/#findComment-633648 Share on other sites More sharing options...
johntp Posted September 4, 2008 Author Share Posted September 4, 2008 Perfect. I had to change it a little because i have a table that i echo no matter what the result is, so here it is incase somone else has this problem <?php $query2 = "SELECT * FROM news WHERE Viewable = 'Yes' AND Section = '$department' ORDER BY RECNO DESC"; $result2 = mysql_query($query2); while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) { $body = $row2['News']; $length = 200; // The number of words you want $text = $body; $text = substr($text, 0,$length)."..."; if (strlen($body) > $length) { $news = $text; } else { $news = $body; } echo "<table style='border-bottom: solid 1px #CCCCCC' width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td> <table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td width='10%'><span class='g'><strong>$row2[Date]</strong></span></td> <td width='80%'><div align='center'><span class='style24'><strong>$row2[Title]</strong></span></div></td> <td width='10%'><div align='right'><span class='g'><strong>$row2[section]</strong></span></div></td> </tr> </table> <table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <br><td style='padding-left:30px' colspan='2'><span class='style9'> <span class='g style4 style6'><span class='style4'>$news</span></span></span></td> </tr> </table><br> </td> </tr> </table>"; } ?> Thanks for everyone's help, I do appreciate it. Link to comment https://forums.phpfreaks.com/topic/122701-solved-use-a-certain-amount-of-words-on-a-sql-query/#findComment-633670 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.