Ray Hines Posted November 26, 2009 Share Posted November 26, 2009 Hi Guys/Gals I have a problem with limiting the number of chars shown from a mysql field. Everything works as long as the field content is more than my limit. But, if the field content is less than my limit the whole thing hangs. Any help would be appreciated. Regards Ray <?php include_once 'libs/sql.php'; $query = "SELECT newsheader, news, date_format(newsdate, '%a %b %D %Y') newdate " . "FROM projectnews " . // "where newsdate <= '$datetime' " . "ORDER BY newsdate DESC " . "LIMIT 1 "; $db = new Sql(); $db->Query($query); Print "<table width = 100% >"; while ($db->NextRecord()) { $newdate = $db->FieldValue('newdate'); $header = $db->FieldValue('newsheader'); $news = $db->FieldValue('news'); //NEW BIT OF CODE $position=350; // Define how many characters you want to display. $post = substr($news,$position,1); // Find what is the last character displaying. We find it by getting only last one character from your display message. if($post !=" "){ // In this step, if last character is not " "(space) do this step . // Find until we found that last character is " "(space) // by $position+1 (14+1=15, 15+1=16 until we found " "(space) that mean character 20) while($post !=" "){ $i=1; $position=$position+$i; $post = substr($news,$position,1); } } $post = substr($news,0,$position); // Display your message //echo $post; //echo "..."; //END OF NEW BIT OF CODE echo "<tr><td><b><h2>" . $header . "</h2></b>\n"; echo "<i><h5><td style=\"width: 150px; text-align: right;\">" . $newdate . "</h5></i></td></tr>"; echo "<tr><td colspan=\"2\">" . $post . "</td></tr>\n"; } $db->Close(); Print "</table>"; ?> Link to comment https://forums.phpfreaks.com/topic/183010-limit-chars-from-mysql-problem/ Share on other sites More sharing options...
Mchl Posted November 26, 2009 Share Posted November 26, 2009 Check if(strlen($news) >= $position) and do your trimming only then. Link to comment https://forums.phpfreaks.com/topic/183010-limit-chars-from-mysql-problem/#findComment-965900 Share on other sites More sharing options...
Ray Hines Posted November 26, 2009 Author Share Posted November 26, 2009 Now why didn't I think of that? Thank you very much for your quick reply. Ray Link to comment https://forums.phpfreaks.com/topic/183010-limit-chars-from-mysql-problem/#findComment-965957 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.