Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Sum of Dynamic Column

Codes here !

Moderators: macek, egami, gesf

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
<table>
 <?php
 $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>
         <?php
     $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;
      
          ?>
           
            </td>
            <?php
              
          } ?>
         </tr>
                 
 <?php } ?>
</table>

Attachments
php-array-sum.jpg
php-array-sum.jpg (161.87 KiB) Viewed 105 times
jewelbd24
New php-forum User
New php-forum User
 
Posts: 1
Joined: Sun Jul 13, 2014 10:55 pm

Re: Sum of Dynamic Column

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

Hi,
Maybe add:
Code: Select all
  echo $total_product;
  $sProdCode = $product['id'];
  $aTotal[$sProdCode] += $total_product;

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

-A
landi
New php-forum User
New php-forum User
 
Posts: 236
Joined: Thu Mar 15, 2012 3:59 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.