...

View Full Version : more problems with mysql



sergxp
12-20-2007, 10:08 PM
Okay so I have a problem. I have a mysql table called "category". In the table it has some fields like name, product_count, etc. I need to query the category to display 'name' fields in all rows of the mysql table and then the "product_count" field next to it.

So it would be
name (product_count)
name (product_count)
and so on...
So far I got this:

<?php
include("dbconfig.php");

$result = mysql_query("Select name, parent, products_count From category");

if(mysql_num_rows($result)>0) {

while($rows = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<br>';

foreach($rows as $data) {
if($rows['name']) {
echo $data;
}

}
}
}

?>

Fumigator
12-20-2007, 10:19 PM
The foreach loop is useless... just do echo "{$row['name']} ({$row['product_count']})<br />\n";

FYI, you shouldn't be storing the number of products in every category. Assuming you have another table that stores each product, you should always select the count from your product table. The reason is because when you store a total, you must always update that total field whenever a change occurs with the product table-- a real headache. When you calculate totals on the fly, you just have to store the detail. Much better.

aedrin
12-20-2007, 10:29 PM
Normally I would agree.

Except in this case that might be too much.

There's no additional detail needed so a simple number would be okay.

Unless every product was very unique, of course (where there wouldn't be more than a handful of each).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum