Mr Chris Posted April 15, 2010 Share Posted April 15, 2010 Hello, Quick question i've been learning about arrays using php, but not with php & mysql. I was wondering if anyone could help me write the below to query a database for colours and put the results it finds in an array? IE instead of Using PHP where I manually enter the colours like so: $colours = array ("Black", "White", "Green", "Purple"); for ($i = 0; $i < count($colours); $i++) { echo "These are the Colours ".$i." is ".$colours[$i]."<br />"; } I put the results of this query select * from colors into the $colours array and they are outputted? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/198625-help-with-an-array/ Share on other sites More sharing options...
oni-kun Posted April 15, 2010 Share Posted April 15, 2010 It's fairly straightforward, $colours = array ("Black", "White", "Green", "Purple"); $ids = join(',',$colors); $sql = "SELECT * FROM colors WHERE id IN ($ids)"; Quote Link to comment https://forums.phpfreaks.com/topic/198625-help-with-an-array/#findComment-1042313 Share on other sites More sharing options...
Mr Chris Posted April 15, 2010 Author Share Posted April 15, 2010 Thanks, but sorry maybe I did not explain myself well. Basically the colours" Black", "White", "Green", "Purple" all exist in a MYSQL database (I don't want any values hard coded into the array) so I simply want to call them out of the database and put them in an array then run a 'for' statement to echo them to the page. How would I do that? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/198625-help-with-an-array/#findComment-1042319 Share on other sites More sharing options...
oni-kun Posted April 15, 2010 Share Posted April 15, 2010 Sorry, Try this: $row = mysql_fetch_assoc(mysql_query("SELECT * FROM colours")); for ($i=0; $i<=count($row); $i++) { print "Number $i = {$row[$i]}"; } I'm not sure what you're wanting to do though, list all colours fields or only fields which contain those colours? Quote Link to comment https://forums.phpfreaks.com/topic/198625-help-with-an-array/#findComment-1042326 Share on other sites More sharing options...
Mr Chris Posted April 15, 2010 Author Share Posted April 15, 2010 Thanks again, but still have some issues if you could kindly still help Here's what I want to do: MYSQL table: colors id color So it can have lots of records in it ie: id = 1 color = black id = 2 colour = red Now what I want to do is basically query that table to get all the records out of the database and stick them in an array: Now this queries the database, but does not get the colour names out of it? $row = mysql_fetch_assoc(mysql_query("SELECT name FROM colours")); for ($i=0; $i<=count($row); $i++) { print "Number $i = {$row[$i]}"; } So it should echo: Number 0 = black Number 1 = red etc... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/198625-help-with-an-array/#findComment-1042335 Share on other sites More sharing options...
oni-kun Posted April 15, 2010 Share Posted April 15, 2010 Try this: Sorry, It's late. $result = mysql_query("SELECT name FROM colours"); while ($row = mysql_fetch_array($result)) { echo "{$row['id']} = {$row['colour']} <br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/198625-help-with-an-array/#findComment-1042338 Share on other sites More sharing options...
Mr Chris Posted April 15, 2010 Author Share Posted April 15, 2010 Magic. Thank You Quote Link to comment https://forums.phpfreaks.com/topic/198625-help-with-an-array/#findComment-1042346 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.