Jump to content

Convert an array into several variables?


Punk Rock Geek

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;

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

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

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.