while loop: detect next loop?
| From: | Erik Price | Date: | Wed, 27 Mar 2002 21:04:55 +0000 |
| Subject: | while loop: detect next loop? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-90423@lists.php.net to get a copy of this message | ||
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