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 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to echo out the group query result

    hi there,

    please help me finish this group query script.

    what i want to do is to print out the quantity of each items in a DB table and show them to the visitors.

    for example, there are item A, item B and item C in the table, i can use the following script find out their quantity(table name:"items")
    PHP Code:
    $itemname "A B C";
    $items_array explode(" "$itemname);
    $count count($items_array);
    for (
    $i=0$i<$count$i++) { 
    $item $items_array[$i]; 
    $query  "SELECT * FROM items WHERE itemname = '$item'"
    $result mysql_query($query) or die('Error, query failed');  
    $query   "SELECT COUNT(item_id) AS numrows FROM items WHERE itemname = '$item'"
    $results  mysql_query($query) or die('Error, query failed'); 
    $row     mysql_fetch_array($resultsMYSQL_ASSOC); 
    $itemquantity $row['numrows'];

    but how can i print the result out, like:
    Number of A: <? echo " " ?>
    Number of B: <? echo " " ?>
    Number of C: <? echo " " ?>

    thanks

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I cleaned your code up a bit...

    PHP Code:
    $items_array = array('A','B','C');
    for (
    $i=0$i<count($items_array); $i++) {
    $query  "SELECT * FROM items WHERE itemname = '".$items_array[$i]."'"
    $result mysql_query($query) or die('Error, query failed');
    echo 
    "Number of ".$items_array.": ".mysql_num_rows($result);


  • #3
    New Coder
    Join Date
    Jun 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank SeeIT for your reply and code up, but i knew how to echo out the result as a group, what i want is echo out the result respectively, and print out like:
    Number of A: <? echo $something; ?>
    not:
    <? echo "Number of ".$items_array.": ".mysql_num_rows($result); ?>
    the "Number of A:" is not a variable, how to find out its quantity from the results? hope made myself clear

  • #4
    New Coder
    Join Date
    Jun 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    to make it more clear:

    I have items A to Z in the table, to find out the quantity, im currently using:
    PHP Code:
    $query  "SELECT * FROM items WHERE itemname = 'A'";  
    $result mysql_query($query) or die('Error, query failed'); 
    $numberofA mysql_num_rows($result);

    .......
    .......
    .......

    $query  "SELECT * FROM items WHERE itemname = 'Z'";  
    $result mysql_query($query) or die('Error, query failed'); 
    $numberofZ mysql_num_rows($result); 
    i have to do this 26 times, in order to print out
    Number of A: <? echo $numberofA; ?>
    .......
    Number of Z: <? echo $numberofZ; ?>

    so how can i use just one piece of script instead of 26 of them?

    thanks

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Use a variable variable, I think this will work, don't know about the array bit.

    PHP Code:
    $numberof$items_array[$i] = mysql_num_rows($result); 
    OR

    PHP Code:
    $item $items_array[$i];
    $numberof$item mysql_num_rows($result); 


  •  

    Posting Permissions

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