Jump to content

[SOLVED] Total Numbers from database, Help


PunjabHaker

Recommended Posts

Hello
I have 2 columns on table cart
column 1 = 230;
column 2 = 250;
i do that:
[code]
<?php
include("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 urgently
Thanks very much
Link to comment
https://forums.phpfreaks.com/topic/33244-solved-total-numbers-from-database-help/
Share on other sites

I got this error:
Warning: Invalid argument supplied for foreach() in E:\Web Design\rolex\test.php on line 7

Warning: Invalid argument supplied for foreach() in E:\Web Design\rolex\test.php on line 7

Warning: Invalid argument supplied for foreach() in E:\Web Design\rolex\test.php on line 7
0
:(

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
Oops on my part

I 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]
I got the same error

Warning: Invalid argument supplied for foreach() in E:\Web Design\rolex\test.php on line 9

Warning: Invalid argument supplied for foreach() in E:\Web Design\rolex\test.php on line 9

Warning: Invalid argument supplied for foreach() in E:\Web Design\rolex\test.php on line 9
0
:(
[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]
Hey guys, one thing more please
If 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
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.