PunjabHaker Posted January 7, 2007 Share Posted January 7, 2007 HelloI have 2 columns on table cartcolumn 1 = 230;column 2 = 250;i do that:[code]<?phpinclude("sqlconnect.php");$totalprice = "0";$ok = mysql_query("select `price` from `cart`");while(list($price)=mysql_fetch_row($ok)){$totalprice = $price;$totalprice++;}print $totalprice;?>[/code]I need it to show the total price 480.How can i do it ?Please help me, i need urgentlyThanks very much Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/ Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 [code]<?php$query = "select `price` from `cart`";$result = mysql_query($query) or die(mysql_error());$total = 0;while(mysql_fetch_array($result)) {foreach($result as $price) {$total += $price;}}print $total;?>[/code] Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155205 Share on other sites More sharing options...
printf Posted January 7, 2007 Share Posted January 7, 2007 You have (2) rows or (2) columns? What are the column names, if they are columns, or you can do what [b]magic2goodil[/b] said if they are rows of [b]price[/b]!printf Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155209 Share on other sites More sharing options...
PunjabHaker Posted January 7, 2007 Author Share Posted January 7, 2007 I got this error:Warning: Invalid argument supplied for foreach() in E:\Web Design\rolex\test.php on line 7Warning: Invalid argument supplied for foreach() in E:\Web Design\rolex\test.php on line 7Warning: Invalid argument supplied for foreach() in E:\Web Design\rolex\test.php on line 70 :(The column is products and i have 2 rows on it with 2 products, each product has a price and i want to calculate the total price,Thank you very much Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155212 Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 Oops on my partI misread it:try this:[code]<?php$query = "select `price` from `cart`";$result = mysql_query($query) or die(mysql_error());$total = 0;while(mysql_fetch_array($result)) {foreach($result as $price) {$total += $price[1]; //assuming that the second column of your table is the price and the first column is the product name or something}}print $total;?>[/code] Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155214 Share on other sites More sharing options...
PunjabHaker Posted January 7, 2007 Author Share Posted January 7, 2007 I got the same errorWarning: Invalid argument supplied for foreach() in E:\Web Design\rolex\test.php on line 9Warning: Invalid argument supplied for foreach() in E:\Web Design\rolex\test.php on line 9Warning: Invalid argument supplied for foreach() in E:\Web Design\rolex\test.php on line 90 :( Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155216 Share on other sites More sharing options...
printf Posted January 7, 2007 Share Posted January 7, 2007 [code]<?php$query = "SELECT price FROM cart";$result = mysql_query ( $query ) or die ( mysql_error () );$total = 0;if ( mysql_num_rows ( $result ) > 0 ){ while ( $row = mysql_fetch_assoc ( $result ) ) { $total += intval ( $row['price'] ); }}echo $total;?>[/code] Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155217 Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 Lol, I modified mine 3 times the last one I just added should work fine, but...printf's is better..his does error suppression first in a way, so that if the results come back 0 rows then nothing is done.. Good show ol' chap. Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155219 Share on other sites More sharing options...
PunjabHaker Posted January 7, 2007 Author Share Posted January 7, 2007 That code works,guys i can't believe that, i solved it in less than 1 minute. ahahahahaThank you very much guys Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155220 Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 Good job printf. And no problem on my part PunjabHaker. Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155222 Share on other sites More sharing options...
PunjabHaker Posted January 7, 2007 Author Share Posted January 7, 2007 Hey guys, one thing more pleaseIf i have another row, "quantity" If quantity =2 and the price is 240, i want the total price to be 480$.Please help me this time,i want it to print the total price for products including quantity, please help me.the code that prinf gave me is very good but i want it to show me the total price including quantity.thanks you very much Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155265 Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 Here is a modified version of printf's code that should work for this.[code]<?php$query = "SELECT price FROM cart";$result = mysql_query ( $query ) or die ( mysql_error () );$total = 0;if ( mysql_num_rows ( $result ) > 0 ){ while ( $row = mysql_fetch_assoc ( $result ) ) { $total += intval ( $row['price'] * $row['quantity']); }}echo $total;?>[/code] Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155286 Share on other sites More sharing options...
PunjabHaker Posted January 7, 2007 Author Share Posted January 7, 2007 That's greatJust on thing:instead of:[code]$query = "SELECT price FROM cart";[/code]should be[code]$query = "SELECT * FROM cart";[/code]thank you very muchworks perfect now Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155288 Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 oops, yea i failed to see that we had u selecting the price instead of *glad it's working now though :) Link to comment https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/#findComment-155295 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.