#1
  1. No Profile Picture
    kumars
    Guest
    Devshed Newbie (0 - 499 posts)
    I have a mySQL table items. With Category, Product, Description and Price. I have to find all the category and list them using PHP in my web page (pricelane.tzo.com)

    Say I have 10 Electronics and 3 Computers and 6 Software, the list should only say Electronics(10) and Computers(3) and Software(6).

    Is there any PHP-MYSQL function to cut this?

    Appreciate any help

    Kumars
    www.pricelane.com
  2. #2
  3. No Profile Picture
    kumars
    Guest
    Devshed Newbie (0 - 499 posts)
    well I have figured (it is not that difficult) out a way:

    Here is my code:
    <body bgcolor=YELLOW>
    <h1>Item Index</h1>
    <?
    mysql_connect("localhost", "userid", "");
    $query = "SELECT category FROM items ORDER BY category";
    $result = mysql_db_query("mydatabase", $query);
    if($result) {
    $number = mysql_numrows($result);
    echo "Item Index: ($number) <ul>";
    $j = 0;
    $icat = "";
    WHILE ($j < $number){

    $category = mysql_result($result,$j,"category");
    if ($j == 0){
    //if it is the first item set icat value
    $icat = $category;
    //depending on the keyword clicked the correspondig page will be opened, at this moment this is not complete and need some work here
    echo "<li><A href='$category.php3'>$category</A>";
    }elseif ($category <> $icat){ //icat value has changed
    $icat = $category; //again set icat value
    echo "<li><A href='$category.php3'>$category</A>"; //display as a href link
    }
    $j++; //increment the value.
    }
    echo "</ul>";
    } else {
    echo "No data."; //if not data
    }
    mysql_free_result($result); //clear the array

    ?>
    ..some footer code here
    </body>
  4. #3
  5. No Profile Picture
    scollo
    Guest
    Devshed Newbie (0 - 499 posts)
    Hi Kumars,

    I didn't take the time to read all of your code (sorry), so I may have misinterpreted what you are trying to do, but you may be able to save some time with MySQL's count() function:

    http://www.mysql.com/Manual_chapter/manual_Reference.html#Functions

    --Christopher
  6. #4
  7. No Profile Picture
    kumars
    Guest
    Devshed Newbie (0 - 499 posts)
    Thank you. GROUP BY is what exactly I wanted to use. I just did the whole script with one line here is the command
    $query = "select category from items GROUP BY
    category";
    WOW! it is neat.

    Thank you Christopher for pointing the direction.

    kumars
    pricelane.tzo.com (future www.pricelane.com)

Similar Threads

  1. Replies: 3
    Last Post: February 11th, 2004, 09:30 AM
  2. Replies: 1
    Last Post: January 30th, 2004, 07:04 PM
  3. how to keep table on top, not centered??
    By vinyl in forum HTML Programming
    Replies: 2
    Last Post: January 20th, 2004, 07:19 PM
  4. Validate PHP form and update MySQL table
    By tlugosi in forum PHP Development
    Replies: 2
    Last Post: January 18th, 2004, 03:26 PM
  5. Replies: 1
    Last Post: January 16th, 2004, 08:39 PM

IMN logo majestic logo threadwatch logo seochat tools logo