Cypher_489 Posted July 1, 2006 Share Posted July 1, 2006 Hi,Just a simple question.[code] $sql = mysql_query("SELECT * FROM dt_directories WHERE category = '$category'") or die(mysql_error()); while ( $dirs = mysql_fetch_array($sql) ) { $name = $dirs['name']; echo "$name, "; }[/code]This echos out the names of directories which are from the category stored in the variable $category like this (category is "general"): Dmoz, Yahoo, Best of the Web, and so on...What I want to do is, rather then echo it, is to store them in a variable with the formatting like in the example. TIA Quote Link to comment https://forums.phpfreaks.com/topic/13379-mysql-fetch-array/ Share on other sites More sharing options...
wildteen88 Posted July 1, 2006 Share Posted July 1, 2006 Yes you can do that. Just git rid of the echo statement. But the $name variable can [b]only[/b] be used on the page it was created on. It cannot be used on another page. So if you want to use $name over multiple pages you might want to store it in a session/cookie. Quote Link to comment https://forums.phpfreaks.com/topic/13379-mysql-fetch-array/#findComment-51656 Share on other sites More sharing options...
Cypher_489 Posted July 1, 2006 Author Share Posted July 1, 2006 I've tried echoing the $name variable outside while() { } before and I only got the last directory in the array. Quote Link to comment https://forums.phpfreaks.com/topic/13379-mysql-fetch-array/#findComment-51658 Share on other sites More sharing options...
shocker-z Posted July 1, 2006 Share Posted July 1, 2006 Also you would need to set it like this to make the variable hold all the values comma seperated$sql = mysql_query("SELECT * FROM dt_directories WHERE category = '$category'") or die(mysql_error()); while ( $dirs = mysql_fetch_array($sql) ) { $name = $name."$dirs[name], "; }to make name 1 list like it echo's if i'm right in how i interprited what you wnat?RegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/13379-mysql-fetch-array/#findComment-51660 Share on other sites More sharing options...
wildteen88 Posted July 1, 2006 Share Posted July 1, 2006 In that case you'll want to make the $name variabled an array which you can do by adding [] at the end of variable name like so:$name[] = $dirs['name']; What will happen now $name will store the results as in an array. Quote Link to comment https://forums.phpfreaks.com/topic/13379-mysql-fetch-array/#findComment-51662 Share on other sites More sharing options...
Cypher_489 Posted July 1, 2006 Author Share Posted July 1, 2006 Brilliant, thats exactly what I wanted. Thanks for the quick replies. Quote Link to comment https://forums.phpfreaks.com/topic/13379-mysql-fetch-array/#findComment-51665 Share on other sites More sharing options...
Cypher_489 Posted July 1, 2006 Author Share Posted July 1, 2006 Just one more thing on this topic, I've found the last element in the array:$last_sql = mysql_query("SELECT * FROM dt_directories WHERE category = '$category' ORDER BY name DESC LIMIT 1") or die(mysql_error()); while ( $last = mysql_fetch_object($last_sql) ) { $last_element = "$last->name";}How will I incorporate this into the $name variable so that the last element doesn't have the comma after it.I've tried a simpleif( $name != $last_element) {$name = $name."$dirs[name], ";else { $name = $name."$dirs[name]"; }but that doesn't work, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/13379-mysql-fetch-array/#findComment-51682 Share on other sites More sharing options...
Cypher_489 Posted July 1, 2006 Author Share Posted July 1, 2006 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/13379-mysql-fetch-array/#findComment-51784 Share on other sites More sharing options...
Cypher_489 Posted July 2, 2006 Author Share Posted July 2, 2006 NVM, I've fixed it now.[code]while ( $dirs = mysql_fetch_array($sql) ) { $name = $name."$dirs[name], "; $names = rtrim($name, ','); }[/code]Didn't need the last element info. Quote Link to comment https://forums.phpfreaks.com/topic/13379-mysql-fetch-array/#findComment-51982 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.