Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    number of idems for each category

    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 Code:
    <?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>

  • #2
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
    $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.

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Tyree View Post
    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:
    PHP Code:
    $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

  • #4
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
    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?

  • #5
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Tyree View Post
    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:
    PHP Code:
    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
    Last edited by CodyNPaige; 12-02-2006 at 05:06 AM.

  • #6
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...
    PHP 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?

  • #7
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Tyree View Post
    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...
    PHP 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

  • #8
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Easy...do this instead of the manual part:
    PHP Code:
    $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.
    Last edited by Tyree; 12-02-2006 at 05:37 AM.

  • #9
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    that didn't list anything here is all the codes on I'm using

    PHP Code:
                <?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
                
    }
                
    ?>
    Last edited by CodyNPaige; 12-02-2006 at 06:11 AM.

  • #10
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You dropped the checkQuan function.

    It'd be like this:
    PHP Code:
    <?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.

  • #11
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    That did it Think you very very much Tyree for all your great help sorry I made things confusing from the start.

  • #12
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •