Punk Rock Geek Posted June 3, 2009 Share Posted June 3, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/160764-convert-an-array-into-several-variables/ Share on other sites More sharing options...
ToonMariner Posted June 3, 2009 Share Posted June 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/160764-convert-an-array-into-several-variables/#findComment-848434 Share on other sites More sharing options...
JonnoTheDev Posted June 3, 2009 Share Posted June 3, 2009 If in the scope of a function you must return an array to the global scope $cities = array(); while($row = $this->DB->fetch()) { $cities[] = $row['city']; } return $cities; Quote Link to comment https://forums.phpfreaks.com/topic/160764-convert-an-array-into-several-variables/#findComment-848436 Share on other sites More sharing options...
Punk Rock Geek Posted June 3, 2009 Author Share Posted June 3, 2009 If in the scope of a function you must return an array to the global scope $cities = array(); while($row = $this->DB->fetch()) { $cities[] = $row['city']; } return $cities; Thank you! This is what I was looking for. Quote Link to comment https://forums.phpfreaks.com/topic/160764-convert-an-array-into-several-variables/#findComment-848603 Share on other sites More sharing options...
Andy-H Posted June 3, 2009 Share Posted June 3, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/160764-convert-an-array-into-several-variables/#findComment-848607 Share on other sites More sharing options...
JonnoTheDev Posted June 3, 2009 Share Posted June 3, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/160764-convert-an-array-into-several-variables/#findComment-848652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.