nethnet Posted February 10, 2008 Share Posted February 10, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90326-populating-array-based-off-of-database-field/ Share on other sites More sharing options...
nethnet Posted February 10, 2008 Author Share Posted February 10, 2008 Wow, nevermind... found my mistake almost immediately after posting, haha... I forgot braces around my foreach loop.... Quote Link to comment https://forums.phpfreaks.com/topic/90326-populating-array-based-off-of-database-field/#findComment-463152 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.