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
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>";

Link to comment
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>";

Fixed an echo line.

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.