Jump to content

associative arrays from database


AdRock

Recommended Posts

Just looking for some advice on how to do this.  I can probably code it if someone tells me the best way to do this

 

I want to connect to the database and from one field in the database (e.g. colours) I want to get all possible values and count how many times each value occurs.

 

I then want to create an associative array with each vale and how many times that value occured e.g.

 

Red => 10

Yellow => 5

Green => 3

Blue => 7

 

I know I can create an array of the colours (or whatever i want) and I know I can comibe the arrays but how would I get each value to increment?

 

Would I use a switch statement inside the while loop from query that checks the value and increments the variable assingned to the value each time it occurs

 

while ($row = mysql_fetch_array($result) {
    switch($row['field']) {
         case 'red':
                $red++;
                 break;
         case 'yellow':
                $yellow++;
                 break;
         case 'green':
                $green++;
                 break;
         case 'blue':
                $blue++;
                 break;
         default:
    }
}

 

If i do it that way how do i get all those variables into an array so i can comibne the 2 arrays?

 

Any help or advice appreciated

 

Link to comment
https://forums.phpfreaks.com/topic/204986-associative-arrays-from-database/
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.