Jump to content

[SOLVED] While loop result as variable


greenday

Recommended Posts

I have a while loop like this:

 

<?php do { ?>
<?php echo $row_mapdirections['name']; ?>@<?php echo $row_mapdirections['lat']; ?>, <?php echo $row_mapdirections['lon']; ?> to:
<?php } while ($row_mapdirections = mysql_fetch_assoc($mapdirections)); ?>

 

This will ouput something like:

 

Name@lat, lon to: Name@lat, lon to: Name@lat, lon to:

 

How can I store the above output as a vairable? The reason i want to to do this is to echo the result later with a right trim on to remove the unwanted 'to' at the end.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/126859-solved-while-loop-result-as-variable/
Share on other sites

No need to trim anything

 

<?php

while ($row_mapdirections = mysql_fetch_assoc($mapdirections))
{
    $outputAry[] = $row_mapdirections['name'] . '@' . $row_mapdirections['lat'] . ', ' . $row_mapdirections['lon'];
}

echo implode(' to: ', $outputAry);

?>

No need to break in and out of PHP like that.

 

<?php
$output = array();
do {
$output[] = $row_mapdirections['name']."@".$row_mapdirections['lat'].", ".$row_mapdirections['lon'];
} while ($row_mapdirections = mysql_fetch_assoc($mapdirections));

echo "<pre>", print_r($output), "</pre><br />";

//Now to seperate with to?
echo implode(" to: ", $output);
?>

 

Edit: mjdamato got it. :P

  • 2 weeks later...

Sorry for the REALLY late reply, got so busy, and then couldnt find this post in the forum.

 

The first suggestion works good, but removes the whole last loop, not just the last "to:"

 

The second suggetsion works perfectly.

 

The only main differance I can see is the second suggestion echos out the output with <pre>?

 

Anyway thanks very much for your help, it solved my problem  8)

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.