Pradeep_Chinna Posted August 6, 2013 Share Posted August 6, 2013 please help me out... How to get this (img.jpg) type of result. I have 4 columns namely, ID, NEW, AMOUNT, TOTAL. please see my image attachment to understand easily. for first row, TOTAL= AMOUNT, for 2nd row, TOTAL= AMOUNT(ROW1) + AMOUNT(ROW2) OR AMOUNT(ROW2) + TOTAL(ROW1), for 3rd row, TOTAL= AMOUNT(ROW1) + AMOUNT(ROW2)+AMOUNT(ROW3) OR AMOUNT(ROW3) + TOTAL(ROW2). and so on... means loop will be upto the end of table. I hope u understood my doubt. please help me out... THANK YOU. Link to comment https://forums.phpfreaks.com/topic/280886-adding-two-columns-of-different-rows/ Share on other sites More sharing options...
PravinS Posted August 6, 2013 Share Posted August 6, 2013 refer: http://www.w3schools.com/php/php_mysql_select.asp and for total value, you can define $total = 0; above while loop and then in while loop just add database total value to $total as $total = $total + AMOUNT(ROW) may this will help you Link to comment https://forums.phpfreaks.com/topic/280886-adding-two-columns-of-different-rows/#findComment-1443650 Share on other sites More sharing options...
Pradeep_Chinna Posted August 6, 2013 Author Share Posted August 6, 2013 @PravinS: sorry...Not useful. Link to comment https://forums.phpfreaks.com/topic/280886-adding-two-columns-of-different-rows/#findComment-1443700 Share on other sites More sharing options...
kicken Posted August 6, 2013 Share Posted August 6, 2013 As you loop through the result set, add the amount for the current row to a total variable and save that variable to the row. eg: $total = 0; $allRows = array(); while ($row=$result->fetch()){ $total += $row['amount']; $row['total'] = $total; $allRows[] = $row; } Then when you output the data in your table you'll have the total field in each row to output. Link to comment https://forums.phpfreaks.com/topic/280886-adding-two-columns-of-different-rows/#findComment-1443745 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.