Jump to content

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));  {

 

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.