Jump to content

[SOLVED] I can't get a foreach loop to work


86Stang

Recommended Posts

I have this code:

//init output var
$output = "";

//pull categories from database
$sql_cat = "SELECT parent_cat, COUNT(description) as classifieds FROM table GROUP BY parent_cat ORDER BY cat_id";
$cat_results = mysql_query($sql_cat) or die(mysql_error());
$cat_array = mysql_fetch_assoc($cat_results);

//loop through categories, and create links to sub cats
foreach($cat_array as $row)
{
//pull parent categories from db
$sql_sub = "SELECT DISTINCT category FROM table WHERE parent_cat = '" . $row['parent_cat'] . "'";
$parent_cats = mysql_query($sql_sub) or die(mysql_error());
$parent_array = mysql_fetch_assoc($parent_cats);

//write category link
$output .= $row['category'] . "(" . $row['classifieds'] . ")" . "\r\n";

	//write sub cat links
	foreach($parent_array as $sub)
	{
		//count classifieds
		$sql_classifieds = "SELECT COUNT(description) as classifieds FROM table WHERE parent_cat = '" . $row['parent_cat'] . "' AND category = '" . $sub['category'] . "'";
		$classifieds = mysql_query($sql_classifieds) or die(mysql_error());

		$output .= ('<div class="subcat"> . $sub['category'] . ') (' . $classifieds->row['classifieds'] . ')</div>' . "\r\n");
	}

	//close parent_cat div
	$output .= ('</div>' . "\r\n");	
}

echo $output;

 

and the only thing it outputs is:

0(0)
1(1)

 

I have a couple of hundred entries in the database so it should be returning WAY more lines as well as text instead of just numbers.  The database has the following columns:

 

parent_cat - the name of the parent category of the article.

category - the name of the category that the article belongs to.

cat_id  - the number assigned to the category that the article belongs to.

description - the description of the article.

 

Any help would be much appreciated.  I *HAVE* to get this running asap so if it means donating a few bucks somebody's way, so be it!

Link to comment
https://forums.phpfreaks.com/topic/88089-solved-i-cant-get-a-foreach-loop-to-work/
Share on other sites

I should've stated what I need to have happen.

 

I need this to list all parent categories along with all the sub categories underneath them along with the number of records per category.  I.E.:

Ford (13)
  Mustang (6)
  Taurus (3)
  F-150 (4)
Chevy (
  Tahoe (3)
  Impala (5)

 

When I try the WHILE statement, all I'm getting is:

 

(5)
()
()
()
(5)
()
()
()

try

<?php
$out = array();
mysql_connect('localhost','root');
mysql_select_db('test');
$sql = 'SELECT category, parent_cat FROM test';
$r = mysql_query($sql);
while($row = mysql_fetch_array($r)){
$c = $row['category'];
$p = $row['parent_cat'];
if (!isset($out[$p][$c])) $out[$p][$c] = 0;
$out[$p][$c]++;
}
foreach ($out as $p => $v){
$u = array_sum($v);
echo "$p ($u)<br />\n";
foreach ($v as $c => $x) echo "    $c ($x)<br />\n";
}
?>

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.