shashib Posted March 27, 2015 Share Posted March 27, 2015 (edited) producttable : id | mid | pid | owgh | nwgh | 1 3 12 1.5 0.6 2 3 12 1.5 0.3 3 3 14 0.6 0.4 4 3 15 1.2 1.1 5 4 16 1.5 1.0 6 4 17 2.4 1.2 7 3 19 3.0 1.4 8 3 19 3.0 1.2 I need result in while loop as **below** : as per mysql query $sql = "SELECT pid,owgh,nwgh from produttable WHERE mid = 3 "; **RESULT I WANT AS BELOW :** Product Id | Old weight | New weight --------------------------------------- 12 1.5 0.6 12 1.5 0.3 Tot: 3.0 0.9 -------------------------------- 14 0.6 0.4 Tot: 0.6 0.4 -------------------------------- 15 1.2 1.1 Tot: 1.2 1.1 -------------------------------- 19 3.0 1.4 19 3.0 1.2 Tot: 6.0 2.6 $query = mysql_query($sql); echo"<table> <tr> <td> Product Id </td> <td> Old Weight </td> <td> New Wight </td> </tr> "; while($row = mysql_fetch_array($query )){ echo "<tr>"; echo "<td>".$row['pid']."</td>"; echo "<td>".$row['owgh']."</td>"; echo "<td>".$row['nwgh']."</td>"; echo "</tr>"; echo "<tr><td>-----</td> <td>Tot : </td> <td> </td></tr>"; } echo " </table>"; But result i am getting is as below Product Id | Old weight | New weight --------------------------------------- 12 1.5 0.6 -------------- Tot 12 1.5 0.3 ---------------Tot 14 0.6 0.4 ---------------Tot 15 1.2 1.1 ---------------Tot 19 3.0 1.4 ---------------Tot 19 3.0 1.2which i dont want , **i want as below** : Product Id | Old weight | New weight --------------------------------------- 12 1.5 0.6 12 1.5 0.3 Tot: 3.0 0.9 -------------------------------- 14 0.6 0.4 Tot: 0.6 0.4 -------------------------------- 15 1.2 1.1 Tot: 1.2 1.1 -------------------------------- 19 3.0 1.4 19 3.0 1.2 Tot: 6.0 2.6 **Update : 27/3/2015**Can i get below result, with sum of each Product Id | Old weight | New weight --------------------------------------- 12 1.5 0.6 12 1.5 0.3 Tot: 3.0 0.9 -------------------------------- 14 0.6 0.4 Tot: 0.6 0.4 -------------------------------- 15 1.2 1.1 Tot: 1.2 1.1 -------------------------------- 19 3.0 1.4 19 3.0 1.2 Tot: 6.0 2.6------------------------------------------Grand Total : 6.3 5 Note in above Old weight : repeat value is not added thats 1.5 , 3.0 is coming twice hence while addition takensingle of it**OR** Product Id | Old weight | New weight --------------------------------------- 12 1.5 0.6 1.5 0.3 Tot: 3.0 0.9 -------------------------------- 14 0.6 0.4 Tot: 0.6 0.4 -------------------------------- 15 1.2 1.1 Tot: 1.2 1.1 -------------------------------- 19 3.0 1.4 3.0 1.2 Tot: 6.0 2.6------------------------------------------ Grand Total : 6.3 5Note in above Old weight : repeat value is not added thats 1.5 , 3.0 is coming twice hence while addition takensingle of it as product id 12, 19 are coming twice hence shown once with its resp valuePlease help me, if i am wrong in HTML tr td format please correct me , it not necessary to get in same table tr,td ..i just want my result in above format Edited March 30, 2015 by shashib Link to comment Share on other sites More sharing options...
davej Posted March 27, 2015 Share Posted March 27, 2015 Maybe... echo "<table> <tr> <td> Product Id </td> <td> Old Weight </td> <td> New Weight </td> </tr>"; $pid = null; $sumold = 0; $sumnew = 0; while($row = mysql_fetch_array($query)){ if ($row['pid'] == $pid || $pid === null){ echo "<tr>"; echo "<td>".$row['pid']."</td>"; echo "<td>".$row['owgh']."</td>"; echo "<td>".$row['nwgh']."</td>"; echo "</tr>"; $pid = $row['pid']; $sumold += $row['owgh']; $sumnew += $row['nwgh']; }else{ echo "<tr><td></td><td></td><td></td></tr> <tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr> <tr><td>------</td><td>------</td><td>------</td></tr>"; echo "<tr>"; echo "<td>".$row['pid']."</td>"; echo "<td>".$row['owgh']."</td>"; echo "<td>".$row['nwgh']."</td>"; echo "</tr>"; $sumold = $row['owgh']; $sumnew = $row['nwgh']; $pid = $row['pid']; } } echo "<tr><td></td><td></td><td></td></tr> <tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr> <tr><td>------</td><td>------</td><td>------</td></tr></table>"; 1 Link to comment Share on other sites More sharing options...
shashib Posted March 28, 2015 Author Share Posted March 28, 2015 @ davej, Thanks , this is what i needed and struggling from last 1 week for same .. You made my day Sir . Thanks alot alot... thank you sooo muchhh Link to comment Share on other sites More sharing options...
shashib Posted March 30, 2015 Author Share Posted March 30, 2015 (edited) Hi , Can i get Grand Total : at last ? of each column Please note : in old weight column weight are repeated twice , trice but i want its single value to be added as below : Product Id | Old weight | New weight --------------------------------------- 12 1.5 0.6 12 1.5 0.3 Tot: 3.0 0.9 -------------------------------- 14 0.6 0.4 Tot: 0.6 0.4 -------------------------------- 15 1.2 1.1 Tot: 1.2 1.1 -------------------------------- 19 3.0 1.4 19 3.0 1.2 Tot: 6.0 2.6------------------------------------------ Grand Total : 6.3 5 Note in above Old weight : repeat value is not added thats 1.5 , 3.0 is coming twice hence while addition takensingle of it**OR** Product Id | Old weight | New weight --------------------------------------- 12 1.5 0.6 1.5 0.3 Tot: 3.0 0.9 -------------------------------- 14 0.6 0.4 Tot: 0.6 0.4 -------------------------------- 15 1.2 1.1 Tot: 1.2 1.1 -------------------------------- 19 3.0 1.4 3.0 1.2 Tot: 6.0 2.6------------------------------------------ Grand Total : 6.3 5Note in above Old weight : repeat value is not added thats 1.5 , 3.0 is coming twice hence while addition takensingle of it echo "<table><tr><td> Product Id </td><td> Old Weight </td><td> New Weight </td></tr>";$pid = null;$sumold = 0;$sumnew = 0;while($row = mysql_fetch_array($query)){if ($row['pid'] == $pid || $pid === null){echo "<tr>";echo "<td>".$row['pid']."</td>";echo "<td>".$row['owgh']."</td>";echo "<td>".$row['nwgh']."</td>";echo "</tr>";$pid = $row['pid'];$sumold += $row['owgh'];$sumnew += $row['nwgh'];}else{echo "<tr><td></td><td></td><td></td></tr><tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr><tr><td>------</td><td>------</td><td>------</td></tr>";echo "<tr>";echo "<td>".$row['pid']."</td>";echo "<td>".$row['owgh']."</td>";echo "<td>".$row['nwgh']."</td>";echo "</tr>";$sumold = $row['owgh'];$sumnew = $row['nwgh'];$pid = $row['pid'];}}echo "<tr><td></td><td></td><td></td></tr><tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr><tr><td>------</td><td>------</td><td>------</td></tr><tr><td>Grand Tol </td><td>Total OLD WGT</td><td>Total NEW WGT</td></tr></table>"; Please help me Edited March 30, 2015 by shashib Link to comment Share on other sites More sharing options...
davej Posted March 31, 2015 Share Posted March 31, 2015 Yes, you would just add two more variables. This might work... echo "<table><tr><td> Product Id </td><td> Old Weight </td><td> New Weight </td></tr>";$pid = null;$sumold = 0;$sumnew = 0;$grandold = 0;$grandnew = 0;while($row = mysql_fetch_array($query)){if ($row['pid'] == $pid || $pid === null){echo "<tr>";echo "<td>".$row['pid']."</td>";echo "<td>".$row['owgh']."</td>";echo "<td>".$row['nwgh']."</td>";echo "</tr>";$pid = $row['pid'];$sumold += $row['owgh'];$sumnew += $row['nwgh'];}else{$grandold += $sumold;$grandnew += $sumnew;echo "<tr><td></td><td></td><td></td></tr><tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr><tr><td>------</td><td>------</td><td>------</td></tr>";echo "<tr>";echo "<td>".$row['pid']."</td>";echo "<td>".$row['owgh']."</td>";echo "<td>".$row['nwgh']."</td>";echo "</tr>";$sumold = $row['owgh'];$sumnew = $row['nwgh'];$pid = $row['pid'];}}$grandold += $sumold;$grandnew += $sumnew;echo "<tr><td></td><td></td><td></td></tr><tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr><tr><td>------</td><td>------</td><td>------</td></tr><tr><td>Grand Tol </td><td>Total OLD WGT:".$grandold."</td><td>Total NEW WGT:".$grandnew."</td></tr></table>"; Link to comment Share on other sites More sharing options...
shashib Posted March 31, 2015 Author Share Posted March 31, 2015 Hi davej , Thanks for reply ... Sir Old Weight is coming wrong .. its taking SUM of all ... but if you check above i need only SUM of ( 1.5 + 0.6 + 1.2 + 3.0 ) = 6.3 but its doing actually ( (1.5 + 1.5)+ 0.6 + 1.2 + (3.0 +3.0 )) ii dont want that repeated value sum of same group but if sum of this should work ( 1.5 + 0.6 + 1.5 + (3.0 +3.0 )) in this 1.5 are of other group id hence its should added Hope you got me .... and sir New weight is working perfect ... if i have another column suppose as name = another_weight which values are ( 1+2+4+5+6+7) = 25 .. which belong to each above resp. id ..then can i get sum of it Thanks Link to comment Share on other sites More sharing options...
davej Posted March 31, 2015 Share Posted March 31, 2015 Hmmm... I don't see the error, the grand totals are... 3.0 + 0.6 + 1.2 + 6.0 = 10.8 0.9 + 0.4 + 1.1 + 2.6 = 5.0 ...but here is an alternative approach... echo "<table><tr><td> Product Id </td><td> Old Weight </td><td> New Weight </td></tr>";$pid = null;$sumold = 0;$sumnew = 0;$grandold = 0;$grandnew = 0;while($row = mysql_fetch_array($query)){ if ($row['pid'] == $pid || $pid === null){ echo "<tr>"; echo "<td>".$row['pid']."</td>"; echo "<td>".$row['owgh']."</td>"; echo "<td>".$row['nwgh']."</td>"; echo "</tr>"; $pid = $row['pid']; $grandold += $row['owgh']; $grandnew += $row['nwgh']; $sumold += $row['owgh']; $sumnew += $row['nwgh']; }else{ echo "<tr><td></td><td></td><td></td></tr> <tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr> <tr><td>------</td><td>------</td><td>------</td></tr>"; echo "<tr>"; echo "<td>".$row['pid']."</td>"; echo "<td>".$row['owgh']."</td>"; echo "<td>".$row['nwgh']."</td>"; echo "</tr>"; $sumold = $row['owgh']; $sumnew = $row['nwgh']; $grandold += $row['owgh']; $grandnew += $row['nwgh']; $pid = $row['pid']; }}echo "<tr><td></td><td></td><td></td></tr><tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr><tr><td>------</td><td>------</td><td>------</td></tr><tr><td>Grand Tol </td><td>Total OLD WGT:".$grandold."</td><td>Total NEW WGT:".$grandnew."</td></tr></table>"; 1 Link to comment Share on other sites More sharing options...
shashib Posted April 2, 2015 Author Share Posted April 2, 2015 Thanks u so much Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now