...

View Full Version : how to echo out the group query result



abanana
11-22-2006, 12:50 AM
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")


$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($results, MYSQL_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

SeeIT Solutions
11-22-2006, 01:02 AM
I cleaned your code up a bit...


$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);
}

abanana
11-22-2006, 01:40 AM
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

abanana
11-22-2006, 01:56 AM
to make it more clear:

I have items A to Z in the table, to find out the quantity, im currently using:

$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

SeeIT Solutions
11-22-2006, 05:10 AM
Use a variable variable, I think this will work, don't know about the array bit.


$numberof$items_array[$i] = mysql_num_rows($result);

OR


$item = $items_array[$i];
$numberof$item = mysql_num_rows($result);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum