Page 1 of 1

Adding data

Posted: Fri Jul 06, 2007 4:31 am
by phpcoder
Hi,
I got database table containg following fileds.
Product_code
Box
Qty

What I want to do is to display all the records from the data base groyp them according to Product_code and after every product I want to display sum of all Qty. say if we have three record in the table
Product_code Box Qty
ckone 2 4
ckone 3 10
armani 3 3

The select result should be like this:

Product_code Box Qty
ckone 2 4
ckone 3 10
total 14
armani 3 3
total 3

Please help me .
Thanks

Posted: Fri Jul 06, 2007 7:39 am
by boo
I would assume that you are going to be displaying this information on either a report or some screen someplace?

If this is the case I wouldnt worry about trying to get your totals within an SQL statement I would order the SQL by product code and then as I am reading the records to format them correctly I would total each record and display that total once the product code has changed.

So the SQL would be something like this

Code: Select all

Select * from Products order by product_code
And the logic for the programming would be something like this

Code: Select all

do { 

   if($saved_product_code ==  $row_product['product_code']){
       $total_qty = total_qty + $row_product['product_qty']
   }else{
       $saved_product_code =  $row_product['product_code']
       //Put formating of total line here
       .........
       $total_qty = $row_product['product_qty']
   }

// Put formatting of record here
.......
} while ($row_product = mysql_fetch_assoc($product));
Hopes this helps