...

View Full Version : number of idems for each category



CodyNPaige
12-02-2006, 01:43 AM
hey every one

I have a list of categories and idems in all the different categories I'm trying to pull the amount of idems in each different category what im getting is

Family ()

() is where I was trying to pull up the amount of idems in that cat

This is the code I have tried but no luck

<?php

$link_query = "SELECT count(*) FROM link " .
"WHERE catalogid={$row['linkid']}";
$link_result = mysql_query ($link_query);
$link_row =mysql_fetch_row($link_result);

?>

<?php print "($link_row[0])"; ?></a>

Tyree
12-02-2006, 03:07 AM
If I'm following what you're trying to accomplish...
I think you're trying to get the number of items from a db table assigned a certain category.

Here's code to do that:


$category = "ThisCat";

$link_query = "SELECT itemid FROM link " .
"WHERE catalogid='". $category ."'";
$link_result = mysql_query ($link_query);

while ($row = mysql_fetch_assoc($link_result))
{
$quantity[] = $row;
}

print count($quantity);


If I'm misunderstanding your goal, please elaborate. Your description and your code are kinda contradictory. So, I'm not completely sure.

Hopefully this is what you need though.

CodyNPaige
12-02-2006, 03:20 AM
If I'm following what you're trying to accomplish...
I think you're trying to get the number of items from a db table assigned a certain category.

Here's code to do that:


$category = "ThisCat";

$link_query = "SELECT itemid FROM link " .
"WHERE catalogid='". $category ."'";
$link_result = mysql_query ($link_query);

while ($row = mysql_fetch_assoc($link_result))
{
$quantity[] = $row;
}

print count($quantity);


If I'm misunderstanding your goal, please elaborate. Your description and your code are kinda contradictory. So, I'm not completely sure.

Hopefully this is what you need though.

yes that is what i'm trying to do I have sevole different categories

Family (0)
School (0)
Life (0)

and so on I just tried your codes and I get the 0 after all categories but I do have idems in some of them so the categories that I have idems in I want it count them and post it after the right category name

Tyree
12-02-2006, 03:31 AM
Okay...so you want to print "Family(quantity)" and so on.

You'd have to do the query 3 times, or set up a function to do it...like this:


function checkQuan($category) {


$link_query = "SELECT itemid FROM link " .
"WHERE catalogid='". $category ."'";
$link_result = mysql_query ($link_query);

while ($row = mysql_fetch_assoc($link_result))
{
$quantity[] = $row;
}

$total = count($quantity);

return $total;
}

print "Family(".checkQuan('Family').")<br />";
print "School(".checkQuan('School').")<br />";
print "Life(".checkQuan('Life').")";


Here's an example I did performing the same function on one of my tables:
http://tyreeonline.com/tests/count_items/

In your table, are your categories represented by "Family", "Life" and "School" or are they ID numbers?

CodyNPaige
12-02-2006, 04:03 AM
Okay...so you want to print "Family(quantity)" and so on.

You'd have to do the query 3 times, or set up a function to do it...like this:


function checkQuan($category) {


$link_query = "SELECT itemid FROM link " .
"WHERE catalogid='". $category ."'";
$link_result = mysql_query ($link_query);

while ($row = mysql_fetch_assoc($link_result))
{
$quantity[] = $row;
}

$total = count($quantity);

return $total;
}

print "Family(".checkQuan('Family').")<br />";
print "School(".checkQuan('School').")<br />";
print "Life(".checkQuan('Life').")";


Here's an example I did performing the same function on one of my tables:
http://tyreeonline.com/tests/count_items/

In your table, are your categories represented by "Family", "Life" and "School" or are they ID numbers?

I have 2 db tables one is link in that table I have
linkid ids of all the idems in this table
catalogid id for the right category like family has id num 1, school id num 2, life id num 3 this is taken from the catalog table this don't show the category name it just shows the id of that category name from the catalog table

the other is catalog in here I store all my categories
catalogid id of each categories
catalogname name of the categories

I have made a page to where I can add new categories to the site and also a page that I can add idems to a special category at any time

hope I explained myself right lol

Tyree
12-02-2006, 04:17 AM
Okay, that does explain things better. Still, we only need to deal with the link table to get this info.
So, a typical table row would look like this?...
Item1 | 1
Item2 | 2

That would indicate that "Item1" is in the "Family" category and "Item2" is in the "School" category, right? The categories are represented by their id number, not by name.

If that's the case, then we'll still use similar code...


function checkQuan($category) {


$link_query = "SELECT linkid FROM link " .
"WHERE catalogid=$category";
$link_result = mysql_query ($link_query);

while ($row = mysql_fetch_assoc($link_result))
{
$quantity[] = $row;
}

$total = count($quantity);

return $total;
}

print "Family(".checkQuan(1).")<br />";
print "School(".checkQuan(2).")<br />";
print "Life(".checkQuan(3).")";



That make sense?

CodyNPaige
12-02-2006, 04:24 AM
Okay, that does explain things better. Still, we only need to deal with the link table to get this info.
So, a typical table row would look like this?...
Item1 | 1
Item2 | 2

That would indicate that "Item1" is in the "Family" category and "Item2" is in the "School" category, right? The categories are represented by their id number, not by name.

If that's the case, then we'll still use similar code...


function checkQuan($category) {


$link_query = "SELECT linkid FROM link " .
"WHERE catalogid=$category";
$link_result = mysql_query ($link_query);

while ($row = mysql_fetch_assoc($link_result))
{
$quantity[] = $row;
}

$total = count($quantity);

return $total;
}

print "Family(".checkQuan(1).")<br />";
print "School(".checkQuan(2).")<br />";
print "Life(".checkQuan(3).")";



That make sense?

my categories might not always be the same tho or I might add meny more to the site so if I have to use
print "Family(".checkQuan(1).")<br />";
print "School(".checkQuan(2).")<br />";
print "Life(".checkQuan(3).")";

then I would always have to manually update this php page with adding more to it I'm trying to get around that if there is anyway

Tyree
12-02-2006, 04:34 AM
Easy...do this instead of the manual part:


$cat_query = "SELECT * FROM catalog";
$cat_result = mysql_query ($cat_query);

while ($row = mysql_fetch_assoc($cat_result))
{
$categories[] = $row;
}

foreach ($categories AS $category) {
print $category['catalogname']."(".checkQuan($category['catalogid']).")<br />";
}


That will go through your catalog table and for each category it finds, run the function on the item table to retrieve how many items are in each. So, at this point, it's totally automatic.

CodyNPaige
12-02-2006, 04:59 AM
that didn't list anything here is all the codes on I'm using



<?php
while ( list($key,$val)=each($cataresult) ) {
$catalogid = stripslashes($val["catalogid"]);
$catalogname = stripslashes($val["catalogname"]);
?>

<?php
$cat_query = "SELECT * FROM linkcatalog";
$cat_result = mysql_query ($cat_query);

while ($row = mysql_fetch_assoc($cat_result))
{
$categories[] = $row;
}

foreach ($categories AS $category) {
print $category['catalogname']."(".checkQuan($category['catalogid']).")<br />";
}
?>

<a href="<?php print "$PHP_SELF?catid=$catalogid"; ?>"><?php print "$catalogname"; ?> (<?php print count($quantity); ?>)</a><br>
<?php
}
?>

Tyree
12-02-2006, 05:15 AM
You dropped the checkQuan function.

It'd be like this:


<?php
function checkQuan($category) {
$link_query = "SELECT linkid FROM link " .
"WHERE catalogid=$category";
$link_result = mysql_query ($link_query);

while ($row = mysql_fetch_assoc($link_result))
{
$quantity[] = $row;
}
$total = count($quantity);
return $total;
}


$cat_query = "SELECT * FROM linkexcatalog";
$cat_result = mysql_query ($cat_query);

while ($row = mysql_fetch_assoc($cat_result))
{
$categories[] = $row;
}

foreach ($categories AS $category) {
print $category['catalogname']."(".checkQuan($category['catalogid']).")<br />";
}


This would return "catnum1(x)" for each category.

If you want to make that into a link then add in the html tags to do so.

You changed the name of your table from "catalog" to "linkexcatalog". I have no idea what you're naming things. So make sure you adjust for that.

CodyNPaige
12-02-2006, 05:27 AM
That did it :thumbsup: Think you very very much Tyree for all your great help sorry I made things confusing from the start.

Tyree
12-02-2006, 11:51 AM
Awesome! Glad that worked for you!

Two tips for getting better help next time around:
1. Include most, if not all, of the code of the trouble page. Sometimes this isn't feasible since you may not have anything much to start with.
2. If dealing with databases, include the structure of the database(s) from the get-go. That just helps the helper in giving you code that will already work for you without you having to track down all the variables that you have to change to work with your code.

Nevertheless, nice to meet you and glad to help you anytime!

Later bro!
Matt



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum