RE: [PHP] while loop: detect next loop?
| From: | Matt Friedman | Date: | Wed, 27 Mar 2002 21:11:44 +0000 |
| Subject: | RE: [PHP] while loop: detect next loop? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-90426@lists.php.net to get a copy of this message | ||
When you have all your items in an array use implode to add a character
in between every item.
$str = implode($yourArray, $separator);
This will add the separator to the end of each string except the last
one.
Matt Friedman
www.SpryNewMedia.com
-----Original Message-----
From: Erik Price [mailto:pricee@hhbrown.com]
Sent: Wednesday March 27, 2002 4:05 PM
To: php-general@lists.php.net
Subject: [PHP] while loop: detect next loop?
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