Jump to content

Help with an array?


Mr Chris

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/198625-help-with-an-array/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/198625-help-with-an-array/#findComment-1042319
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/198625-help-with-an-array/#findComment-1042326
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/198625-help-with-an-array/#findComment-1042335
Share on other sites

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.