Solved

Sum of all rows in table using PHP

Posted on 2011-09-07
7
499 Views
Last Modified: 2012-05-12
I have a table whose data is coming from a MySQL table. here is my query. and I am using DREAMWEAVER CS5 .

$query_Recordset2 = "SELECT internetitems.SKU, internetitems.`DESCRIPTION`, internetitems.`UNIT COST`, internetitems.`UNIT RETAIL`, SUM(allcatalogs.`NET UNITS`) as QtyTotal FROM internetitems, allcatalogs WHERE internetitems.SKU = allcatalogs.SKU GROUP BY internetitems.SKU";

Open in new window


and I am calcuating the Total Cost and Totat Retail using a If clause with the help of PHP like below

<?php $WtotalQuantity = 0; $TotalCost = 0; $TotalRetail = 0; ?> 
<table cellpadding="0" cellspacing="0">
  <tr>
    <th style="width:120px;">Sku</th>
    <th style="width:360px;">Description</th>
    <th style="width:60px;">Unit Cost</th>
    <th style="width:60px;">Unit Retail</th>
	<th style="width:60px;">Rerun Start OH</th>
    <th style="width:80px;">Qty Sold as of <?php echo date("m/d/y"); ?></th>
    <th style="width:120px;">Total Cost</th>
    <th style="width:120px;">Total Retail</th>
  </tr>
   <?php do { ?>
     <tr>
       <td style="width:120px; text-align:left;"><?php echo $row_Recordset1['SKU']; ?></td>
       <td style="width:360px; text-align:left;"><?php echo $row_Recordset1['DESCRIPTION']; ?></td>
       <td style="width:60px; text-align:right;">$<?php echo $row_Recordset1['UNIT COST']; ?></td>
       <td style="width:60px; text-align:right;">$<?php echo $row_Recordset1['UNIT RETAIL']; ?></td>
	   <td style="width:60px; text-align:right;"><?php echo $row_Recordset1['RERUN START OH']; ?></td>
       <td style="width:60px; text-align:right;"><?php echo $row_Recordset1['QtyTotal']; ?></td>
       <td style="width:120px; text-align:right;">
       <?php
		  if($row_Recordset1['QtyTotal'] < $row_Recordset1['RERUN START OH'])
		    {
			   echo $row_Recordset1['QtyTotal'] * $row_Recordset1['UNIT COST']; 
			}
		  else
		    {
				echo $row_Recordset1['RERUN START OH'] * $row_Recordset1['UNIT COST']; 
			}
				
						  
		 ?>
       </td>
       <td style="width:120px; text-align:right;">$
       <?php
		  if($row_Recordset1['QtyTotal'] < $row_Recordset1['RERUN START OH'])
		    {
			   echo $row_Recordset1['QtyTotal'] * $row_Recordset1['UNIT RETAIL'];
			}
		  else
		    {
				echo $row_Recordset1['RERUN START OH'] * $row_Recordset1['UNIT RETAIL']; 
			}
				
	  
		 ?>
       
       </td>
    </tr>
     <tr>
    <?php $WtotalQuantity += $row_Recordset1['QtyTotal']; ?> 
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
     <td style="width:120px;">&nbsp;</td>
     <td style="width:360px;">&nbsp;</td>
     <td style="width:60px;">&nbsp;</td>
     <td style="width:60px;">&nbsp;</td>
	 <td style="width:60px;">&nbsp;</td>
      <td style="width:60px; text-align:right;"><b><?php echo $WtotalQuantity; ?>  </b></td>
      <td style="width:120px; text-align:right;"><b>$<?php echo $TotalCost; ?></b></td>
      <td style="width:120px; text-align:right;"><b>$<?php echo $TotalRetail; ?> </b></td>
  </tr>
</table>

Open in new window


Now I am trying to display the SUM of Total Cost from the first condition and Sum of Total Retail

Thanks and appreciate it
0
Comment
Question by:niceoneishere
7 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36495853
At lines 273, 274, 275 you have attribute value set two times for element input.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36495865
I'm sorry, too fast to post an answer: I said stupid thing :-(
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 36495868
lol its ok I was like whaaaaaaaaat lol
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 36497220
try:
<?php $WtotalQuantity = 0; $TotalCost = 0; $TotalRetail = 0; ?> 
<table cellpadding="0" cellspacing="0">
  <tr>
    <th style="width:120px;">Sku</th>
    <th style="width:360px;">Description</th>
    <th style="width:60px;">Unit Cost</th>
    <th style="width:60px;">Unit Retail</th>
	<th style="width:60px;">Rerun Start OH</th>
    <th style="width:80px;">Qty Sold as of <?php echo date("m/d/y"); ?></th>
    <th style="width:120px;">Total Cost</th>
    <th style="width:120px;">Total Retail</th>
  </tr>
   <?php do { ?>
     <tr>
       <td style="width:120px; text-align:left;"><?php echo $row_Recordset1['SKU']; ?></td>
       <td style="width:360px; text-align:left;"><?php echo $row_Recordset1['DESCRIPTION']; ?></td>
       <td style="width:60px; text-align:right;">$<?php echo $row_Recordset1['UNIT COST']; ?></td>
       <td style="width:60px; text-align:right;">$<?php echo $row_Recordset1['UNIT RETAIL']; ?></td>
	   <td style="width:60px; text-align:right;"><?php echo $row_Recordset1['RERUN START OH']; ?></td>
       <td style="width:60px; text-align:right;"><?php echo $row_Recordset1['QtyTotal']; ?></td>
       <td style="width:120px; text-align:right;">
       <?php
		  if($row_Recordset1['QtyTotal'] < $row_Recordset1['RERUN START OH'])
		    {
				$TotalCost+=$row_Recordset1['QtyTotal'] * $row_Recordset1['UNIT COST']
			   echo $row_Recordset1['QtyTotal'] * $row_Recordset1['UNIT COST']; 
			}
		  else
		    {
				$TotalCost+=$row_Recordset1['RERUN START OH'] * $row_Recordset1['UNIT COST']; 
				echo $row_Recordset1['RERUN START OH'] * $row_Recordset1['UNIT COST']; 
			}
				
						  
		 ?>
       </td>
       <td style="width:120px; text-align:right;">$
       <?php
		  if($row_Recordset1['QtyTotal'] < $row_Recordset1['RERUN START OH'])
		    {
				$TotalRetail +=$row_Recordset1['QtyTotal'] * $row_Recordset1['UNIT RETAIL'];
			   echo $row_Recordset1['QtyTotal'] * $row_Recordset1['UNIT RETAIL'];
			}
		  else
		    {
				$TotalRetail +=$row_Recordset1['RERUN START OH'] * $row_Recordset1['UNIT RETAIL']; 
				echo $row_Recordset1['RERUN START OH'] * $row_Recordset1['UNIT RETAIL']; 
			}
				
	  
		 ?>
       
       </td>
    </tr>
     <tr>
    <?php $WtotalQuantity += $row_Recordset1['QtyTotal']; ?> 
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
     <td style="width:120px;">&nbsp;</td>
     <td style="width:360px;">&nbsp;</td>
     <td style="width:60px;">&nbsp;</td>
     <td style="width:60px;">&nbsp;</td>
	 <td style="width:60px;">&nbsp;</td>
      <td style="width:60px; text-align:right;"><b><?php echo $WtotalQuantity; ?>  </b></td>
      <td style="width:120px; text-align:right;"><b>$<?php echo $TotalCost; ?></b></td>
      <td style="width:120px; text-align:right;"><b>$<?php echo $TotalRetail; ?> </b></td>
  </tr>
</table>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36497734
Here's your problem: I am using DREAMWEAVER CS5

I am not saying that to be flip - Dreamweaver is a terrible place to learn PHP.  It has some of the worst code I have ever seen, and was never meant to be a teaching example.  So when you start by following Dreamweaver's model you put yourself at a disadvantage right away.

This little book will help you learn the PHP and MySQL part of things correctly.  As you come to understand how those parts work together (instead of just trying to model your programming on Dreamweaver) you will be way ahead in your programming, I promise.
http://www.sitepoint.com/books/phpmysql4/

It's a great investment - a little money, a little time, and a lot of knowledge.  Best of luck with your project. ~Ray
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 36498100
Brilliant it  worked. I can't believe i missed it.

Thanks and appreciate it.
0
 
LVL 1

Author Closing Comment

by:niceoneishere
ID: 36498114
Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now