Jump to content

Recommended Posts

I need to sum the total field if 'ompShippingMethodID' == "CPU" then thats one number and then total everything else.

 

I know my code is wrong but I did it so you could see what I was after.

 while ($row = odbc_fetch_array($result))
{
	echo "<tr><td>" .$row['ompShippingMethodID'] ."</td>";
 echo "<td>" .$row['UOMPTRUCKNUMBER'] ."</td>";
 echo "<td>" .$row['cmoName'] ."</td>";
echo "<td><a href='kf_order_by_id.php?id={$row['ompSalesOrderID']}'>" .$row['ompSalesOrderID'] ."</td>";
echo "<td>" .  number_format($row['total'],2) ."</td></tr>"; 
  $gr_total = $gr_total + $row['total'];
  if ('ompShippingMethodID' == "CPU" ){
  	$cpu_total=$cpu_total +  $row['total'];
 } else {
 	$kith_total=$kith_total +  $row['total'];
 }
}

Link to comment
https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/
Share on other sites

Your question doesn't make sense. For example you state

 

 

if 'ompShippingMethodID' == "CPU" then thats one number . . . 

 

No, that's not a number, that's a string.

 

Looking at your code, this is what I see:

 

You have a running total calculated in the loop ($gr_total). Then the if/else code has two subtotals based on whether a record matches the condition or not. But, you are not outputting those values. Are you outputting them after the loop is complete? If so, how are the values different from what you expect? I would suggest creating some additional output for development/debugging purposes. You could then show that output and explain how it needs to change for your purposes.

 

 

<?php
 
$gr_total = 0;
$cpu_total = 0;
$kith_total = 0;
$output = '';
while ($row = odbc_fetch_array($result))
{
    //Update totals
    $row_total = number_format($row['total'], 2);
    $gr_total = $gr_total + $row_total;
    if ($row['ompShippingMethodID'] == "CPU" ){
        $cpu_total = $cpu_total + $row_total;
    } else {
        $kith_total = $kith_total + $row_total;
    }
//Create output
    $output .= "<tr>\n";
    $output .= "<td>{$row['ompShippingMethodID']}</td>\n";
    $output .= "<td>{$row['UOMPTRUCKNUMBER']}</td>\n";
    $output .= "<td>{$row['cmoName']}</td>\n";
    $output .= "<td><a href='kf_order_by_id.php?id={$row['ompSalesOrderID']}'>{$row['ompSalesOrderID']}</td>\n";
    $output .= "<td>{$row_total}</td>\n";
    $output .= "<td>{$cpu_total}</td>\n";
    $output .= "<td>{$kith_total}</td>\n";
    $output .= "<td>{$gr_total}</td>\n";
    $output .= "</tr>\n"; 
}
 
?>
<table border="1">
  <tr>
    <th>Shipping Method</th>
    <th>TRUCK NUMBER</th>
    <th>cmo Name</th>
    <th>Order ID</th>
    <th>Row Total</th>
    <th>CPU Total</th>
    <th>Kith Total</th>
    <th>Grand Total</th>
  </tr>
  <?php echo $output; ?>
</table>

Sorry let me try to explain better:

" need to sum the total field if 'ompShippingMethodID' == "CPU" then thats one number"

the total field is a number and that is what I want to add up for ever row of data that has  ompShippingMethodID' == "CPU" and then add everything else up for my second number.

The now is 

<?php
 
$gr_total = 0;
$cpu_total = 0;
$kith_total = 0;
$output = '';
while ($row = odbc_fetch_array($result))
{
    //Update totals
    $row_total = number_format($row['total'], 2);
    $gr_total = $gr_total + $row_total;
    if ($row['ompShippingMethodID'] == "CPU" ){
        $cpu_total = $cpu_total + $row_total;
    } else {
        $kith_total = $kith_total + $row_total;
    }
//Create output
    $output .= "<tr>\n";
    $output .= "<td>{$row['ompShippingMethodID']}</td>\n";
    $output .= "<td>{$row['UOMPTRUCKNUMBER']}</td>\n";
    $output .= "<td>{$row['cmoName']}</td>\n";
    $output .= "<td><a href='kf_order_by_id.php?id={$row['ompSalesOrderID']}'>{$row['ompSalesOrderID']}</td>\n";
    $output .= "<td>{$row_total}</td>\n";
    $output .= "<td>{$cpu_total}</td>\n";
    $output .= "<td>{$kith_total}</td>\n";
    $output .= "<td>{$gr_total}</td>\n";
    $output .= "</tr>\n"; 
}
 
?>
<table border="1">
  <tr>
    <th>Shipping Method</th>
    <th>TRUCK NUMBER</th>
    <th>cmo Name</th>
    <th>Order ID</th>
    <th>Row Total</th>
    <th>CPU Total</th>
    <th>Kith Total</th>
    <th>Grand Total</th>
  </tr>
  <?php echo $output; ?>
</table>

 

Edited by kat35601

output

 

string(5) "CPU " string(5) "CPU " string(5) "CPU " string(5) "CPU " string(5) "CPU " string(5) "CPU " string(5) "CPU " string(5) "CPU " string(5) "CPU " string(5) "CPU " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH " string(5) "KITH "

I see a space with CPU?? maybe.

 

I tried it with a space but same results.

 

Really, just one space? The debug step that Muddy had you use shows exactly what the problem is, just look at ANY of the values returned:

 

 

string(5) "CPU "

It is a string with FIVE characters. There are probably TWO spaces at the end. You are probably only seeing one space because of how the browser renders multiple spaces.

 

 

string(5) "KITH "

This is also a string with five characters. This one is obvious that there is just one space at the end.

 

You should fix your data (remove the spaces). Somewhere in your code you are setting these values in the DB and you are introducing those spaces. Fix that code and then fix the existing data.

As @Psycho said, you need to sanitise your data before it is stored in the database. You should be using php's trim() on your inputs as part of that process.  You can also run the same function directly in mysql to clean up your existing data. Always make sure you have control over your data when you can.

still same results as before even after trimming 

 

string(3) "CPU" string(3) "CPU" string(3) "CPU" string(3) "CPU" string(3) "CPU" string(3) "CPU" string(3) "CPU" string(3) "CPU" string(3) "CPU" string(3) "CPU" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH" string(4) "KITH"

This solved it 

 while ($row = odbc_fetch_array($result))
{
echo "<tr><td>" .$row['ompShippingMethodID'] ."</td>";
echo "<td>" .$row['UOMPTRUCKNUMBER'] ."</td>";
echo "<td>" .$row['cmoName'] ."</td>";
echo "<td><a href='kf_order_by_id.php?id={$row['ompSalesOrderID']}'>" .$row['ompSalesOrderID'] ."</td>";
echo "<td>" .  number_format($row['total'],2) ."</td></tr>"; 
  $gr_total = $gr_total + $row['total'];
   if ($row['ompShippingMethodID'] == "CPU" ){
        $cpu_total = $cpu_total +  $row['total'];
    } else {
        $kith_total = $kith_total +  $row['total'];
    }
}

//******************************************************************


odbc_close($connect);
 $num2 = number_format( $gr_total, 2);
echo " Total: $num2";
echo "  ";
echo "$cpu_total";
echo "  ";
echo "$kith_total";
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.