Jump to content

Simple calculation and display question


foobar82

Recommended Posts

I'm looking for a basic way to calculate some data and display it in a table.

 

For example, lets say I have a database that catalogs different fruit by color and weight. I'd like to be able to display the total amount of weight added together for a particular color into a web table. I'm sure there is a lot of great information on this forum however the search function is not currently working. I was hoping someone could provide an quick explanation or maybe knows of a good online resource for something basic like that.

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/198614-simple-calculation-and-display-question/
Share on other sites

mysql_connect("localhost", "root");
echo "<table>";
$sql="SELECT fruit, color, weight FROM fruit WHERE color='$color'";
$result=mysql_query($sql);
while ($row=mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['fruit']."</td><td>".$row['color']."</td><td>".$row['weight']."</td>";
echo "</tr>
$sql="SELECT SUM(weight) as 'total_weight' FROM fruit WHERE color='$color'";
$result=mysql_query($sql);
$row=mysql_fetch_assoc($result);
echo "<tr><td></td><td>total:</td><td>".$row['total_weight']."</td></tr></table>";

mysql_connect("localhost", "root");
echo "<table>";
$sql="SELECT fruit, color, weight FROM fruit WHERE color='$color'";
$result=mysql_query($sql);
while ($row=mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['fruit']."</td><td>".$row['color']."</td><td>".$row['weight']."</td>";
echo "</tr>";
$sql="SELECT SUM(weight) as 'total_weight' FROM fruit WHERE color='$color'";
$result=mysql_query($sql);
$row=mysql_fetch_assoc($result);
echo "<tr><td></td><td>total:</td><td>".$row['total_weight']."</td></tr></table>";

Fixed an echo line.

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.