Forum Moderators: coopster

Message Too Old, No Replies

array count query

         

jake66

12:28 am on Apr 20, 2006 (gmt 0)

10+ Year Member



this query is supposed to hide any categories with 0 products... it does just that, but it hides EVERY category. i cannot figure out why.

anyone have a suggestion what i did wrong?

function tep_count_products_in_manufacturer($manufacturers_id, $include_inactive = false) {
$manu_count = 0;
if ($include_inactive == true) {
$manu_products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . ", " . TABLE_MANUFACTURERS . " where products.manufacturers_id = manufacturers.manufacturers_id and manufacturers.manufacturers_id = '" . (int)$manufacturers_id . "'");
} else {
$manu_products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . ", " . TABLE_MANUFACTURERS . " where products.manufacturers_id = manufacturers.manufacturers_id and products.products_status = '1' and manufacturers.manufacturers_id = '" . (int)$manufacturers_id . "'");
}
$manu_products = tep_db_fetch_array($manu_products_query);
$manu_count += $manu_products['total'];
return $manu_count;
}

omoutop

10:27 am on Apr 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



check the first line of your function
$include_inactive = false // never becomes true?

also check your table names // try top echo your query and see what it tells you.

use die() for debugging

hope that should help you

jake66

5:33 am on Apr 21, 2006 (gmt 0)

10+ Year Member



> $include_inactive = false // never becomes true?
that's supposed to remain false, yes

> try top echo your query and see what it tells you.
top echo?
i tried the echo $tablename and nothing shown, so definately i'm missing something

> use die() for debugging
not sure how to use this function?

omoutop

6:34 am on Apr 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To use die():

$my_query="SELECT * FROM...... ";
$my_result = mysql_query($my_query) or die(mysql_error().'<p>Error found in: '.$my_query.'</p>');
while ($my_myrow = mysql_fetch_array($my_result))
{
// do something with results
}

Also try to echo somethig like:

echo "select count(*) as total from " . TABLE_PRODUCTS . ", " . TABLE_MANUFACTURERS . " where products.manufacturers_id = manufacturers.manufacturers_id and manufacturers.manufacturers_id = '" . (int)$manufacturers_id . "'" "; and see what it goes into your query

hakre

8:31 am on Apr 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi jake66,

i just hacked that func within a clients osc setup lately.

this query is supposed to hide any categories with 0 products

doesn't it hide inactive categrories only? otherwise there would be a need of a join first not a cross-query over both tables i assume. or a better where clause. but shouldn't be categrories with 0 products disabled anyway?

so maybe this is not a php based prob at all, right?

omoutop:

check the first line of your function
$include_inactive = false // never becomes true?

it's not the first line of the function, it's the parameters definition of the function - defining the default value of parameter #2 as false which will come true if the value true is passed.

for echoing the query - which is a good idea to debug - , please use this line:

echo('<pre>' . htmlspecialchars($manu_products_query). '</pre>');

because this will contain the exact query which is passed to your database server.

keep in mind with another peoples source code to understand it fully first otherwise you will run into many problems. the osc source is not the best-structured one so you will need some time for this but you will be rewarded afterwards ;).