I was wondering if someone out there could help me with this issue.
I am trying to get the number of products in a seperate table which share the same Category that is in its own table. The category table has 2 fields, Category and CatID, while the products table has each product with a CatID.
What I want to do is display the Category, and then next to each category have it display the number of products in the category. I have tried it a few ways, and while I am not getting any errors, I am unable to get it to display the number. Please help
The code is intertwined in some html so I did my best to pull out what I felt was important from the page.
Code: Select all
<?
include 'conf.php';
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$query = "SELECT Category.CatID, products.CatID, Category.Category, products.name, products.price, products.desc FROM products, Category where products.CatID = Category.CatID and products.CatID = '$CatID'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
$num_results = mysql_num_rows($result);
if (mysql_num_rows($result) > 0)
$row = mysql_fetch_object($result)
// is displayed in heading portion of page
echo $row->Category;
?>
<?
//second part, just the list of categories
// generate and execute query
$query2 = "SELECT Category, CatID FROM Category";
$result2 = mysql_query($query2) or die ("Error in query: $query2. " . mysql_error());
if (mysql_num_rows($result2) > 0)
{
// check for categories and put them in the list
while($row2 = mysql_fetch_object($result2))
{
// this is where I want the number of results to be displayed next to each link
?>
<a href ="products.php?CatID=<? echo $row2->CatID; ?>"><? echo $row2->Category .$num_results.?></a><br>
<?
}
?>
<?
}
?>