Echo rows until the nth character is reached

How would you use php to echo all the rows until the nth character is reached with a while statement?
For instance,say i want the while statement to echo 3000 characters which might end up displaying 30 rows but the 3000th character is in the middle of the 30th row; i want the while statement to stop and delete the unfinished row.
:eye:

Guido -i got yours to work by removing the &&$length<3000.Thanks.

Sorry ive tried both and they are looping (i added a<hr/> to test this> but no information is coming up on screen

Thanks also to MrSterling I’ll try your code later also :slight_smile:


<?php

$limit = 3000;

do{
  
  list($name, $age, $description) = $row;
  
  $limit -= strlen($name . $age . $description);
  
  printf(
    '<li><b>&#37;s</b><em>%s</em><br/> <br/>%s</li>',
    $name,
    $age,
    $description
  );
  
}while($row = mysql_fetch_array($result) && 0 < $limit);

?>

thanks guido ill give that a try. :slight_smile: Im playing scrabble now! :smiley:


$result = mysql_query("SELECT name,age,description FROM `life`");

$length = 0;
while(list($name,$age,$description) = mysql_fetch_row($result) && $length &lt; 3000)
{
   $length += strlen($name) + strlen($age) + strlen($description);
   if ($length &lt; 3000) echo "&lt;li&gt;&lt;b&gt;$name&lt;/b&gt;&lt;em&gt;$age&lt;/em&gt;&lt;br/&gt; &lt;br/&gt;$description&lt;/li&gt;";
}

$result = mysql_query(“SELECT name,age,description FROM life”);

while(list($name,$age,$description)= mysql_fetch_row($result))
{
echo “<li><b>$name</b><em>$age</em><br/> <br/>$description</li>”;

I have this to work with

Post a before and after, along with how the ‘after’ came to be. :slight_smile:

Sorry I’m new to this.would it be len($name.$age.$description,3000)?

The strings are actually three fields (name,age,description)I want to add their strings up.

Are all these characters in a string?

http://www.php.net/manual/en/function.strlen.php

Well, do so :slight_smile:

No problem Guido i was gonna let the loop continue anyway and print >=3000 to a separate file which i can access with links from my main page.

Yes, that’ll work, but it means you loop through the entire result set, even if you already reached the 3000 characters limit.
It should work with the limit, maybe it needs some brackets. Try:


$result = mysql_query("SELECT name,age,description FROM `life`");
 
$length = 0;
while( (list($name,$age,$description) = mysql_fetch_row($result)) && ($length &lt; 3000) )
{
   $length += strlen($name) + strlen($age) + strlen($description);
   if ($length &lt; 3000) echo "&lt;li&gt;&lt;b&gt;$name&lt;/b&gt;&lt;em&gt;$age&lt;/em&gt;&lt;br/&gt; &lt;br/&gt;$description&lt;/li&gt;";
}