Adding data

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Adding data

Post 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
User avatar
boo
Forum Commoner
Posts: 42
Joined: Mon Jul 02, 2007 11:30 am
Location: NY

Post 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
Post Reply