foobar82 Posted April 15, 2010 Share Posted April 15, 2010 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 More sharing options...
andrewgauger Posted April 15, 2010 Share Posted April 15, 2010 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 https://forums.phpfreaks.com/topic/198614-simple-calculation-and-display-question/#findComment-1042498 Share on other sites More sharing options...
Ken2k7 Posted April 15, 2010 Share Posted April 15, 2010 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 https://forums.phpfreaks.com/topic/198614-simple-calculation-and-display-question/#findComment-1042511 Share on other sites More sharing options...
andrewgauger Posted April 15, 2010 Share Posted April 15, 2010 Thanks Ken, nice catch! Link to comment https://forums.phpfreaks.com/topic/198614-simple-calculation-and-display-question/#findComment-1042516 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.