Jump to content

Total of looper region?


Recommended Posts

Hi everyone, I have a looped region on my page where its calling all data from the db where 'x = user_id', Ive got all the data being called and displayed in a table, However I need to calculate a value of all the data in 1 of the columns. say I have 3 rows of data and they all have a field saying £3. its bringing them back as

 

1- product - 3.00

2- product - 3.00

3- product - 3.00

 

Is there a php function to output an addition of all the prices?

 

Link to comment
https://forums.phpfreaks.com/topic/200834-total-of-looper-region/
Share on other sites

Sorry to sound naive, but ive tried to put it in my while loop and it breaks the syntax, is this incorrect?

 

    <?php do { ?>
      <tr>
        <td><?php echo $row_cart['id']; ?></td>
        <td><?php echo $row_cart['photo_id']; ?></td>
        <td><?php echo $row_cart['sess_id']; ?></td>
        <td><?php echo $row_cart['photo_name']; ?></td>
        <td><?php echo $row_cart['photo_price']; ?></td>
      </tr>
      
      <?php }while ($row_cart = mysql_fetch_assoc($cart));  {
$total+=$row_cart['photo_price'];  
}  ?>

This would work better:

    <?php 
      echo "<tr>"
while ($row_cart = mysql_fetch_assoc($cart))  {       
echo "<td>".$row_cart['id']."</td>"
         echo "<td>".$row_cart['photo_id']."</td>"
         echo "<td>".$row_cart['sess_id']."</td>"
         echo "<td>".$row_cart['photo_name']."</td>"
         echo "<td>".$row_cart['photo_price']."</td>"
         $total = $total + $row_cart['photo_price'];  
          }  
      echo "</tr>"
?>

 

the syntax error you had was a ; in the while line that shouldn't be there :

<?php }while ($row_cart = mysql_fetch_assoc($cart));  {

 

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.