kat35601 Posted June 22, 2016 Share Posted June 22, 2016 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']; } } Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/ Share on other sites More sharing options...
Psycho Posted June 22, 2016 Share Posted June 22, 2016 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> Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533907 Share on other sites More sharing options...
kat35601 Posted June 22, 2016 Author Share Posted June 22, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533908 Share on other sites More sharing options...
Psycho Posted June 22, 2016 Share Posted June 22, 2016 Did you try the code I provided? If I understand you correctly, the values you need should be generated correctly. Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533910 Share on other sites More sharing options...
kat35601 Posted June 23, 2016 Author Share Posted June 23, 2016 (edited) Cpu totals does not show up at all and the totals are way off. I don't know what to try next. Edited June 23, 2016 by kat35601 Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533937 Share on other sites More sharing options...
Muddy_Funster Posted June 23, 2016 Share Posted June 23, 2016 Could you post up the code as you have it now, the values you are getting for the totals and the values you should be getting for the totals? "way off" isn't really something that we can work with i'm afraid. Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533939 Share on other sites More sharing options...
kat35601 Posted June 23, 2016 Author Share Posted June 23, 2016 (edited) 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 June 23, 2016 by kat35601 Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533940 Share on other sites More sharing options...
Muddy_Funster Posted June 23, 2016 Share Posted June 23, 2016 that looks like it should be fine. what's the differences between the actual and expected results? Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533941 Share on other sites More sharing options...
kat35601 Posted June 23, 2016 Author Share Posted June 23, 2016 see the results here http://s1064.photobucket.com/user/kat35601/media/Selection_053_zps4utaxhfd.png.html Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533945 Share on other sites More sharing options...
Muddy_Funster Posted June 23, 2016 Share Posted June 23, 2016 Seems as though your if condition is never returning positive. Remember php is case sensitive. Can you add the following just before the if line and post up the output: var_dump($row['ompShippingMethodID']); Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533946 Share on other sites More sharing options...
kat35601 Posted June 23, 2016 Author Share Posted June 23, 2016 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 " Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533948 Share on other sites More sharing options...
kat35601 Posted June 23, 2016 Author Share Posted June 23, 2016 (edited) I see a space with CPU?? maybe. I tried it with a space but same results. Edited June 23, 2016 by kat35601 Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533949 Share on other sites More sharing options...
Psycho Posted June 23, 2016 Share Posted June 23, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533952 Share on other sites More sharing options...
Muddy_Funster Posted June 23, 2016 Share Posted June 23, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533953 Share on other sites More sharing options...
kat35601 Posted June 23, 2016 Author Share Posted June 23, 2016 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" Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533954 Share on other sites More sharing options...
kat35601 Posted June 23, 2016 Author Share Posted June 23, 2016 after refresh this is the results http://s1064.photobucket.com/user/kat35601/media/Selection_054_zpsvvmmz03f.png.html Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533955 Share on other sites More sharing options...
kat35601 Posted June 23, 2016 Author Share Posted June 23, 2016 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533957 Share on other sites More sharing options...
Psycho Posted June 23, 2016 Share Posted June 23, 2016 Right, the number_format() function is adding a comma as a thousands separator. Then, trying to add that formatted value to a number it is only adding the value before the comma. Quote Link to comment https://forums.phpfreaks.com/topic/301378-how-do-i-sum-with-a-if-statement/#findComment-1533958 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.