Add field values from MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Add field values from MySQL

Post by JimiH »

Hello

I have the code below

Code: Select all

include("conn.php");


$result = mysql_query("SELECT * FROM projects where status = 'O'") or die(mysql_error());


echo "<table Align ='center' border='1'>"; 
echo "<tr> <th>VSI Number</th> <th>Customer</th> <th>Engineer</th> <th>Value (£)</th> <th>Year Qty</th><th>Total (£)</th>  <th>Comments</th> <th>Last update</th> </tr>";
 
// keeps getting the next row until there are no more to get 

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

// Print out the contents of each row into a table 

echo "<tr><td>"; 
echo $row['VSI_NUMBER']; 
echo "</td><td>"; 
echo $row['CUSTOMER']; 
echo "</td><td>"; 
echo $row['ENGINEER']; 
echo "</td><td>"; 
echo $row['VALUEExworks']; 
echo "</td><td>"; 
echo $row['YEARQTY'];
echo "</td><td>"; 
echo $row['COMMENTS']; 
echo "</td><td>"; 
echo $row['ACTIONDATE']; 
echo "</td></tr>";
 } 
echo "</table>"; 

?> 


</body>

</html>
I have added a columb "Total (£)" and would like to include the result of "VALUEExworks" multiplied by "YEAR QTY" into this row.

How would I achieve this?

Thanks

Geoff
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
while($row = mysql_fetch_array( $result )) {
  // Print out the contents of each row into a table

  $total = $row['VALUEExworks'] * $row['YEARQTY'];

  echo "<tr><td>";
  echo $row['VSI_NUMBER'];
  echo "</td><td>";
  echo $row['CUSTOMER'];
  echo "</td><td>";
  echo $row['ENGINEER'];
  echo "</td><td>";
  echo $row['VALUEExworks'];
  echo "</td><td>";
  echo $row['YEARQTY'];
  echo "</td><td>";
  echo $row['COMMENTS'];
  echo "</td><td>";
  echo $row['ACTIONDATE'];
  echo "</td></tr>";
}
echo "</table>";

?>
Then echo $total out where you want it to go in the output.
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Thanks I'll have a go.

Geoff
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Thanks worked a treat!!

Code: Select all

$total = $row['VALUEExworks'] * $row['YEARQTY']; 

echo "<tr><td>"; 
echo $row['VSI_NUMBER']; 
echo "</td><td>"; 
echo $row['CUSTOMER']; 
echo "</td><td>"; 
echo $row['ENGINEER']; 
echo "</td><td>"; 
echo $row['VALUEExworks']; 
echo "</td><td>"; 
echo $row['YEARQTY'];
echo "</td><td>"; 
echo $total;
echo "</td><td>"; 
echo $row['COMMENTS']; 
echo "</td><td>"; 
echo $row['ACTIONDATE']; 
echo "</td></tr>";
 } 
echo "</table>";
Geoff
Post Reply