Re: while loop: detect next loop?
| From: | Tom Rogers | Date: | Thu, 28 Mar 2002 09:32:50 +0000 |
| Subject: | Re: while loop: detect next loop? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-90495@lists.php.net to get a copy of this message | ||
Hi
add the newline and white space first like this
$data_printed = "";
while ($row = mysql_fetch_assoc($result)) {
if($data_printed != ""){
$data_printed .= " \n";
}
$data_printed = "<file id=\"" . $row['file_id'] . "\">" . $row['file_name'] . "</file>";
}
That will add to all except the last one.
Tom
At 07:04 AM 28/03/2002, you wrote:
There is a nice feature of a for loop, that I would like to replicate somehow within a while loop. The short question is this -- is there any way to detect if the while loop has one or more iterations remaining (after the current iteration) to go through, so that I can [for instance] add a comma or a newline or something to the text that is printed in the loop iteration? In a for loop I can do this: for ($c = 0; $c < sizeof($array); $c++) { $data_printed = $array[$c]; if ($c < sizeof($array) - 1) {$data_printed .= "\n ";} } I have a while loop like this: while ($row = mysql_fetch_assoc($result)) { $data_printed = "<file id=\"" . $row['file_id'] . "\">". $row['file_name'] . "</file>";} This prints things like this: <file id="1">somefile.txt</file><file id="2">otherfile.pdf</file> etc I do not want to blindly add a newline and some whitespace to EVERY iteration, because this is XML and the whitespace is significant (and this code will impact thousands of files), but I can't think of a clever way of adding the newline and whitespace to every iteration EXCEPT the last one. TIA for any Jedi insight, Erik ---- Erik Price Web Developer Temp Media Lab, H.H. Brown pricee@hhbrown.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php