Jump to content

Recommended Posts

Hello.  I have a select query which returns multiple results.  In order to display each result, I run a while statement, like so:

 

while ( $row = $this->DB->fetch() )
{ 
$city = strval( $row['city'] );
echo "$city<br>";
}

 

Everything works.  Each city is displayed properly.  Here's the problem:

 

I can't use echo.  For the sake of simplicity, I will just say that the software I'm modifying specifically uses return.  And when I replace "echo" with "return", I now only get 1 result instead of 20 or so.

 

Ideally, I would like to have every query result returned, and as a different variable.  Any suggestions?  I imagine this is very simple and that I am having a brain lapse.  :-X

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/160764-convert-an-array-into-several-variables/
Share on other sites

err not that clear a description...

 

why not stick the whole thing into an array so you can use it later on. return will break out of a function or stop the current script working - which makes me believe the code you are using is a bit gash.

This way will return a string with the line breaks already added. Saves having to format it later before display.

 

$cities = '';

while( $row = $this->DB->fetch() )
{
   $cities .= $row['city'] . '<br >
';
}
return $cities;

This way will return a string with the line breaks already added. Saves having to format it later before display.

 

$cities = '';

while( $row = $this->DB->fetch() )
{
   $cities .= $row['city'] . '<br >
';
}
return $cities;

You would never do it like that. You should implode an array.

$string = implode("<br />", $cities);
print $string;

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.