Home > Back-end >  Shopping cart total concept
Shopping cart total concept

Time:05-10

<?php
include 'Conn.php';
$sql="SELECT * FROM `cart` where  active=0";
$result=$conn->query($sql);
if($result){
    while ($row = $result->fetch_assoc()){
  
        $counter=1;
        $id=$row['id'];
        $productid=$row['productid'];
        $PRICE=$row['PRICE'];
        $total=$row['PRICE'];
 
        echo'
            <tr>
                <td>'.$counter.'</td>
                <td>'.$productid.'</td>
                <td>'.$PRICE.'</td>
                <td><button type="button"  ><a href="cartdelete.php?deleteid='.$id.'">REMOVE</a></button></td>
                <td>'.$total.'</td>
            </tr>';
    }
} 
?>

I Need A help for Total how can I total the amount I have tried the total =$PRICE this piece of code

CodePudding user response:

Try this

<?php
     include 'Conn.php';
     $sql="SELECT * FROM `cart` where  active=0";
     $result=$conn->query($sql);

     //initializing total to 0
     $total = 0;

    if($result){
      while ($row = $result->fetch_assoc()){
      
        $counter=1;
        $id=$row['id'];
        $productid=$row['productid'];
        $PRICE=$row['PRICE'];
       
        //adding product price to total amount
        $total  = (double)$row['PRICE'];
     
        
        echo'
        <tr>
        <td>'.$counter.'</td>
        <td>'.$productid.'</td>
        <td>'.$PRICE.'</td>
        <td><button type="button"  ><a href="cartdelete.php?deleteid='.$id.'">REMOVE</a></button></td>
        </tr>
            ';
      
       }

       //displaying total, you can show wherever you like
       echo 'Total: '.$total;
    } 
?>

I have also added comment on the code, so that you could get an idea, about what you are doing.

CodePudding user response:

                                                         <?php
                                                         include 'Conn.php';
                                                         $sql="SELECT * FROM `cart` where  active=0";
                                                         $result=$conn->query($sql);
                                                         $total=0;
                                                        if($result){
                                                      while ($row = $result->fetch_assoc()){
                                                      
                                                        $counter=1;
                                                        $id=$row['id'];
                                                        $productid=$row['productid'];
                                                        $PRICE=$row['PRICE'];
                                                        $total  =(double)$row['PRICE'];
                                                     
                                                        
                                                        echo'
                                                        <tr>
                                                        <td>'.$counter.'</td>
                                                        <td>'.$productid.'</td>
                                                        <td>'.$PRICE.'</td>
                                                        <td><button type="button"  ><a href="cartdelete.php?deleteid='.$id.'">REMOVE</a></button></td>
                                                        <td>'.$PRICE.'</td>
                                                        </tr>
                                                            ';
                                                  
                                                        
                                               
                                                       }
                                                      } ?>
                                                        </tbody>
                                                    </table>
                                          <?php
                                          echo 'Total: '.$total;
                                          ?>

                                                  
                                                        
  //I tired This yet i am getting total as 0    
  • Related