hi! i'm having a hard time figuring out how to add two values(numbers) retrieved from a table. They should be added and displayed. how can i do this?

here's the code snippet:

require('connect.php');

        $result = mysql_query("SELECT * FROM `order` where date = '$date' ");

        while ($row = mysql_fetch_array($result)){

        echo "The product Code: {$row['product_code']} <br>
        had been sold {$row['quantity']} times <br> 
        and the total price is {$row['total_price']}.
        Report from : {$row['date2']}";

        }

when I try : $price = $row['total_price'] + $row['total_price'] , the value returned is 0 . Same goes if I try adding the quantity. Help?

Recommended Answers

All 6 Replies

It is not evident from your code where the $price is getting echoed. This should work:

$result = mysql_query("SELECT * FROM `order` where date = '$date' ");

        $price = $row['total_price'] + $row['total_price'];

        echo "Double price is $price<br />";
}

as long as $row['total_price'] exists and has some value;

thanks! it works but i wanted the values in the rows to be added and have 1 result.
example I have 400 in one row then 200 at the other row. I wan to echo their total in one display. can it be done?

and the answer should depend on the value of the quantity.

OK, I get it. You want to sum the total price for a product coe and a selected date, I guess. The query should be something like:

$query = "SELECT *, SUM(total_price) AS sum_total_price FROM order WHERE date = '$date' GROUP BY product_code ";

As you can see you can get the result just by preparing appropriate query. You might want to ask also in a mysql forum here on DW if you need more complex queries.

I have a new problem. How can I echo a label without it being repeated in my loop?
I want the output to be:

Product Code | Item1 Item2

the code is this:

$result1 = mysql_query("SELECT product_code FROM `order` where date = '$date'");

                while ($row = mysql_fetch_array($result1)){

                    $product = $row['product_code'];


echo "<pre> Poduct Code |   $product</pre>";
        }

whenever I execute this however, the result will be

Product Code | Item1
Product Code | Item2

help again?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.