Jump to content

Populating Array based off of Database Field


nethnet

Recommended Posts

So it's getting late and I feel like I'm too tired to catch my simple mistake here, so I'm posting it for all to take a stab at.  Basically what this script is supposed to do is select all of the tutorials from my database and organize them into categories.  The final result would look something like this, with the number in each category in parentheses:

 

  • AJAX (23)
  • MySQL (40)
  • PHP (65)
  • RSS (7)
  • XML (12)

 

You get the idea...  So the problem here is that my script is pulling the necessary information from the database, but for some reason it isn't populating my $two_dim array (which is supposed to keep track of the counts of different categories).

 

<?php

$sql = "SELECT * FROM tutorials";
$query = mysql_query($sql);

$two_dim = array();

while($result = mysql_fetch_object($query)){
$category = explode(",",$result->keywords);
foreach($category as $cat)
	// echo "$cat, ";
	$two_dim[$cat]++;
}

// echo "<p>";
// print_r($two_dim);
// echo "</p>";

?>

 

The commented lines are lines I added in to debug...  Currently there is just one record in the table, and it's value for "keywords" is "PHP,XML,RSS,MySQL", so the resulting array should look like:

 

Array (
    [code=php:0] => 1,
    [xml] => 1,
    [RSS] => 1,
    [MySQL] => 1
)

 

However, when I uncomment the last three commented lines to see what my array contains I only see:

 

Array (
    [MySQL] => 1
)

 

For some reason only the last value from the exploded array is being put into the new array to track counts...  So then I added the first commented line to see if $cat was working properly, and when it runs it prints out "PHP, XML, RSS, MySQL, " like expected...  I don't understand what is going on here, and it has to be something simple as this is a very simple script...

 

Thanks in advance,

 

Theo

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.