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
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);

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.