-3
<?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" class="btn btn-danger" ><a href="cartdelete.php?deleteid='.$id.'"class="text-light">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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 2
    the `$total` wasn't initialize – WolfCode May 10 '22 at 08:16
  • 2
    Do you really want to output the total on each line? If not, you should do `$total += $PRICE` and output that variable after the loop instead of in it. Don't forget to add `$total = 0;` before the loop though. – M. Eriksson May 10 '22 at 08:16
  • 2
    Set `$total = 0` before while loop. And `$total += (double) $PRICE` in the loop. – Han Moe Htet May 10 '22 at 08:17
  • How Can i Initialize it please help –  May 10 '22 at 08:17
  • 1
    You initialize it just like we mentioned; add `$total = 0;` before the loop. – M. Eriksson May 10 '22 at 08:18
  • 1
    Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly May 10 '22 at 08:22
  • @M.Eriksson i tried your sugggestion sir still i am getting total as 0 –  May 10 '22 at 09:00
  • That's because `$PRICE` isn't a numeric value, it's a string (as you've mentioned in a comment under one of the answers) – M. Eriksson May 10 '22 at 09:44

2 Answers2

2

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" class="btn btn-danger" ><a href="cartdelete.php?deleteid='.$id.'"class="text-light">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.

Saroj Shrestha
  • 2,696
  • 4
  • 21
  • 45
  • 2
    When posting an answer, you should always include an explanation of what you've changed and why. Give a man a fish vs teaching him to fish and all that... – M. Eriksson May 10 '22 at 08:19
  • 1
    I have added the comment on the code itself, shouldn't that be enought? – Saroj Shrestha May 10 '22 at 08:22
  • also he's counter is missing an auto increment , and you too – WolfCode May 10 '22 at 08:24
  • I Tried this too still i am not getting the total amount –  May 10 '22 at 08:25
  • @VINIITHSHAH can u explain which total you want ? total each product price ( `product amount * price `) or a total of all ? – WolfCode May 10 '22 at 08:29
  • @VINIITHSHAH could you please paste `echo "
    "; print_r($row); exit();` after `while ($row = $result->fetch_assoc()){` this row, and let me know what it returns?
    – Saroj Shrestha May 10 '22 at 08:30
  • @WolfCode sir i want when user add the products i should get the total amount at the end like user add 2 products of Rs 3000 and Rs 4000 i should get total 7000 at the end in short total of all the products in the end –  May 10 '22 at 08:33
  • @VINIITHSHAH did you try what I mentioned above? Also, is the column name `PRICE` in capital or small letter? – Saroj Shrestha May 10 '22 at 08:43
  • @SarojShrestha yes i got this Array ( [id] => 5 [username] => 1 [productid] => NFS [PRICE] => RS 5000/- [active] => 0 ) –  May 10 '22 at 08:51
  • @VINIITHSHAH just paste this code `echo "
    "; print_r($row); exit();` after `while `\($row = $result->fetch_assoc()){` this will show what data are present on `$row`, in that way I could suggest you the answer.
    – Saroj Shrestha May 10 '22 at 08:51
  • @VINIITHSHAH I could trim `RS ... /-` and add but thats not the correct approach, on database just store 5000, and while showing only show in this way `echo 'Rs'.$row['PRICE].'/-'` , this way only you could add correctly. On the database, just store number. – Saroj Shrestha May 10 '22 at 08:53
  • @SarojShrestha i did that but i think that doesnt help my query i want to total all amount of products which are being added to cart please just see the below code i have pasted you will come to know –  May 10 '22 at 08:54
  • @VINIITH SHAH I had just mentioned why your code is not working, you cannot add some text with `RS...`, it needs to be a number check here. https://onlinephp.io/c/c659b – Saroj Shrestha May 10 '22 at 09:12
  • @VINIITHSHAH - You should fix the data in your database and make sure the price _only_ contain numbers. Now you're trying to do addition with strings, not numbers, which is why you can't get the total. – M. Eriksson May 10 '22 at 09:38
  • Thank you so much sir i got the Total Really Very Very Much thankful to the entire team –  May 10 '22 at 09:52
  • @M.Eriksson hello sir how to hide pay button till there is no product in cart section like what will be the condition for it i tried one still i am facing errors –  May 13 '22 at 14:01
0
function get_numerics ($str) {
    preg_match_all('/\d+/', $str, $matches);
    return $matches[0];
}
<table>
    <thead>
        <tr>
            <th>N°</th>
            <th>Product ID</th>
            <th>Price</th> 
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
<?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'];
        $product_id =$row['productid'];           //Change $productid to $product_id, by adapting snake case
        $price      =$row['PRICE'];               //Change $PRICE to $price ,variable as upcase is for constant varaible unique  
        $total     +=get_numerics($row['PRICE']); //Adding product price to total amount
        
        echo'
        <tr>
        <td>'.$counter.'</td>
        <td>'.$product_id.'</td>
        <td>'.$price.'</td>
        <td><button type="button" class="btn btn-danger" ><a href="cartdelete.php?deleteid='.$id.'"class="text-light">REMOVE</a></button></td>
        </tr>';

        $counter ++; //product type counter
    }
}

    ?>
    <!-- Style improvement for more information check css attribute guild -->
    <tr><td style='colspan:4;text-align: right;'>Total : <?php  echo $total ?> </td></tr>
    </tbody>
</table>

try this out , you will get the total price at the end the table with an individuel row

and also dont forget to change the table header

WolfCode
  • 145
  • 1
  • 8
  • getting this error on your code A non-numeric value encountered –  May 10 '22 at 09:09
  • @VINIITHSHAH after the data example you gived , you should never store the currency name with the price, price is semthing numerique , is more easy the concat value then split the data to the value we need – WolfCode May 10 '22 at 09:32
  • Unsupported operand types @WolfCode –  May 10 '22 at 09:32
  • btw the get_numerics function is from here -> https://stackoverflow.com/questions/11243447/get-numbers-from-string-with-php – WolfCode May 10 '22 at 09:33