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 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    more problems with mysql

    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 Code:
    <?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($resultMYSQL_ASSOC)) {
            echo 
    '<br>';

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

    ?>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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).


  •  

    Posting Permissions

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