Sum of Dynamic Column

Codes here !

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Sun Jul 13, 2014 10:55 pm

Sum of Dynamic Column

Postby jewelbd24 » Sun Jul 13, 2014 11:35 pm

Hello experts,
I am having a hard time to show the total of each product ( see the attached image)

Actually I want to show the value of xxx, yyy, zzz(according to the image)
steps that I have followed:
1. list the customer(customer loop)
2. show all product column in each customer row(product loop in every customer).
3. now sum up the total of each product column.

Code: Select all

 $sql = "select * from customer order by id desc";
$exe = mysql_db_query($sqldb,$sql);
while($cust = mysql_fetch_array($exe))
<tr class="<?php echo $class; ?>" >
   <td align="center"><?php echo $cust['customer_id']; ?></td>   
   <td><?php echo $cust['company']; ?></td>
     $sql_product = "select id, name from product order by name asc ";
 $exe_product = mysql_db_query($sqldb,$sql_product);
 while($product = mysql_fetch_array($exe_product))
   <td align="center">
          <?php // total products...
$sql_sum = "select SUM(voucher_qty) from invoice where cust_id = '$cust[id]' and product_id = '$product[id]'"; 
$exe_sum = mysql_db_query($sqldb,$sql_sum);
$sum = mysql_fetch_array($exe_sum);
$total_product = $sum['SUM(voucher_qty)'];

      echo $total_product;
          } ?>
 <?php } ?>

php-array-sum.jpg (161.87 KiB) Viewed 526 times

php-forum Active User
php-forum Active User
Posts: 384
Joined: Thu Mar 15, 2012 3:59 pm

Re: Sum of Dynamic Column

Postby landi » Tue Aug 05, 2014 11:13 am

Maybe add:

Code: Select all

  echo $total_product;
  $sProdCode = $product['id'];
  $aTotal[$sProdCode] += $total_product;

Then print out $aTotal[xxx] at the end.


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 1 guest