Jump to content

Limit chars from mysql problem


Ray Hines

Recommended Posts

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

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.