Page 1 of 1

Create an associative array from table

Posted: Thu May 15, 2008 4:14 pm
by csdavis0906
I am trying to create an associative array reading fields tag & count from a table named tags.

Doing it manually, I would use:

Code: Select all

/* array of items and sizes
$tagArray = array(
	"Apple" => 9,
	"Cell Phone" => 12,
	"Computer" => 6,
	"eBay" => 1,
	"Gadget" => 3
);
I have tried looping through the table as follows:

Code: Select all

$query = mysql_query("SELECT * FROM tags ORDER BY count DESC");

// loop through table world counting occurrences of parent
while ($row = mysql_fetch_array($query)) {
	
   $tagArray [$tag] = $count;  // tried this and several other variations with no success
	
}
Any assistance would be greatly appreciated - this is driving me crazy!!! Thanking you in advance...

Re: Create an associative array from table

Posted: Thu May 15, 2008 4:47 pm
by csdavis0906
I finally figured it out - I just added the param. MYSQL_ASSOC to the mysql_fetch_array statement and then
used: $tagArray[$parent] = $count;

Finally, yikes!!!!

Re: Create an associative array from table

Posted: Thu May 15, 2008 7:48 pm
by dbemowsk
It is difficult to understand what you are trying to do. In your first post, where is the variable $tag and $count coming from? Likewise, in your second post, where is $parent coming from?

Re: Create an associative array from table

Posted: Fri May 16, 2008 1:37 am
by VladSun
mysql_fetch_assoc()