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
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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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
:(
Link to comment
Share on other sites

[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
Share on other sites

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
Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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