Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-04-2013, 07:39 AM   PM User | #1
juslai
New Coder

 
Join Date: Jul 2012
Location: Philippines
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
juslai is an unknown quantity at this point
What is wrong in summing up the column?

Code:
<?php require_once('Connections/connect.php'); ?>
<?php
  $id_customer = mysql_real_escape_string($_GET['id_customer']);



 $sql_PK = "SELECT * FROM tbl_delivery_details WHERE tbl_customer_id_customer = {$id_customer}";
 $PK = mysql_query($sql_PK, $connect);
 if ( mysql_error() ) {
  die ( mysql_error());
}
 $row_PK = mysql_fetch_assoc($PK);

 $customer_name = $row_PK['tbl_customer_id_customer'];



$customer_name = mysql_real_escape_string($customer_name);



$sql = "SELECT 
	SUM(delivery_details_revenue) as revenue,
	tbl_customer.customer_name, 
	tbl_delivery_details.delivery_details_route, 
	tbl_delivery_details.delivery_details_destination, 
	tbl_delivery_details.delivery_details_van_no, 
	tbl_delivery_details.delivery_details_waybill_no, 
	tbl_delivery_details.delivery_details_charge_invoice,
	tbl_delivery_details.delivery_details_revenue,
	tbl_delivery_details.delivery_details_strip_stuff,
	tbl_delivery_details.delivery_details_date

FROM tbl_customer, tbl_delivery_details 

WHERE tbl_customer.id_customer = tbl_delivery_details.tbl_customer_id_customer 
AND tbl_customer.id_customer = '{$customer_name}'";


$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($res);
$sum = 0;


?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/x	html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Customer Revenue</title>
<link rel="stylesheet" type="text/css" href="qcc.css"/>
</head>

<body>


<table border="1">
        <tr>
          <th>Revenue</th>
          
          
        </tr>
        <?php do { ?>
          <tr>
          	
            <td><?php echo $row_PK['delivery_details_revenue'];?></td>
            
           
          </tr>
    
          
          <?php } while ($row_PK = mysql_fetch_assoc($PK));?>
		 
         <?php  
			while ($arr=mysql_fetch_array($res)){  
			$sum += $arr['delivery_details_revenue']; 
			}?> 
          
               
      </table>
      
TOTAL: <?php echo $sum; ?> <br/>

TOTAL:<?php echo $row_PK['revenue'];?>
</body>
</html>

i cant display the sum of all the revenue how to properly do it?
juslai is offline   Reply With Quote
Old 01-04-2013, 09:24 AM   PM User | #2
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
you are doing fetch_array twice on the same result set?

$row = mysql_fetch_array($res);

and

while ($arr=mysql_fetch_array($res)){
$sum += $arr['delivery_details_revenue'];
}

also you'd be better off calculating it all prehtml and then in your html all you have to do is spit out the var rather than having the while within the html
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Old 01-04-2013, 09:57 AM   PM User | #3
juslai
New Coder

 
Join Date: Jul 2012
Location: Philippines
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
juslai is an unknown quantity at this point
how to do it pre html? i tried doing it on sql code and html that's why there is 2 output i tried both but none of them worked
juslai is offline   Reply With Quote
Old 01-04-2013, 01:12 PM   PM User | #4
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
something like this though i suspect what you're doing is wrong over all not just code wise.

you're double looping one table (not required), selecting a sum but then referencing the individual rows rather than just the sum, you're then redoing a manual sum in code (after you've done it in the db) and your displaying purely the customer delivery revenue in the table but the total twice at the bottom from 2 different sources neither of which would provide you with the total anyway.

I think you need to start again and plan what you want to achieve from the datasets you have available to you.

PHP Code:
<?php require_once('Connections/connect.php'); ?>
<?php
  $id_customer 
mysql_real_escape_string($_GET['id_customer']);

 
$sql_PK "SELECT * FROM tbl_delivery_details WHERE tbl_customer_id_customer = {$id_customer}";
 
$PK mysql_query($sql_PK$connect);
 if ( 
mysql_error() ) {
  die ( 
mysql_error());
}
$html "";
 while (
$row_PK mysql_fetch_assoc($PK)) {
    
//get first itteration vars
    
$customer_name mysql_real_escape_string($row_PK['tbl_customer_id_customer']);
    
$deliverydetails $row_PK['delivery_details_revenue']
    
//build start html
    
$html .= "<tr><td>$deliverydetails</td></tr>"
    
//do second query
    
$sql "SELECT 
    SUM(delivery_details_revenue) as revenue,
    tbl_customer.customer_name, 
    tbl_delivery_details.delivery_details_route, 
    tbl_delivery_details.delivery_details_destination, 
    tbl_delivery_details.delivery_details_van_no, 
    tbl_delivery_details.delivery_details_waybill_no, 
    tbl_delivery_details.delivery_details_charge_invoice,
    tbl_delivery_details.delivery_details_revenue,
    tbl_delivery_details.delivery_details_strip_stuff,
    tbl_delivery_details.delivery_details_date 
    FROM tbl_customer, tbl_delivery_details 
    WHERE tbl_customer.id_customer = tbl_delivery_details.tbl_customer_id_customer 
    AND tbl_customer.id_customer = '{$customer_name}'"
;
    
$res mysql_query($sql) or die(mysql_error());
    
$sum 0;
    
//loop and sum
    
while ($row mysql_fetch_array($res)) {
        
$sum += $row['revenue']; 
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/x    html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Customer Revenue</title>
<link rel="stylesheet" type="text/css" href="qcc.css"/>
</head>

<body>
<table border="1">
        <tr>
          <th>Revenue</th>
        </tr>
        <?php echo $html;?>
</table>
      
TOTAL: <?php echo $sum?> <br/>

TOTAL:<?php echo $row_PK['revenue'];?>
</body>
</html>
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:15 AM.


Advertisement
Log in to turn off these ads.