Home > Enterprise >  Why can't i access variable inside if loops in php
Why can't i access variable inside if loops in php

Time:01-20

I am making simple shopping cart and i got really stucked with final price a.k.a $fp variable. there is always: Notice: Undefined variable: fp in C:\xampp\htdocs... the thing is everything is working, data from database are fetched on site, price included but when i want to display $fp(final price) it shows that error. I tried setting it to 0 in else statement after if($row['ID'] == $id) loop, nothing happened I tried to make it global like:

global $fp; 
$fp = $fp   intval($row['price']);

when i did that, error disappeared but it just didnt do anything(empty space). maybe i am just sitting too long looking at that but i just can't wrap my head around why i cant acess that variable or what is happening.

<section >
                <?php
                if(isset($_SESSION['cart'])){
                    $product_id = array_column($_SESSION['cart'],'product_id');
                    $sql = "SELECT * FROM muzi;";
                    $result = mysqli_query($conn, $sql);
                    while($row = mysqli_fetch_assoc($result)){
                        foreach($product_id as $id){
                            if($row['ID'] == $id){          
                echoItem($row['fotografia'],$row['price'],$row['znacka'],$row['konkretne'],$size);
                                $fp = $fp   intval($row['price']);
                            }
                        }
                    }
                }else{
                    echo "<h5>Cart is empty</h5>";
                }
               ?>
   <h2 >PRICE: <?php $fp;?> €</h2>
</section>
function echoItem($productIMG, $productPrice, $znacka, $typ_oblecenia){
    $product = "
    <article class='clearfix'>
        <img src='../img/$productIMG' alt='hihi' style='width: 10%;'>
        <div class='info-text-cart'>
            <p>specific type: $typ_oblecenia</p>
            <p>Brand: $znacka</p>
            <button><i class='fas fa-times'></i></button>
        </div>
        <div class='important-info-cart'>
            <p>Price: $productPrice €</p>
            <p>quantity: 1</p>
        </div>
    </article>
    ";
    echo $product;
}

ps: i have everything in terms of typing right. I just translated some things that are involved in this to english so it makes more sense.

CodePudding user response:

$fp has never been declared as mentioned before.

If you declare it as global (or not) you should be able to use it on the second code block, the reason that you didn't see anything when you tried is because you forgot to echo it.

A working example would be:

<section >
                <?php
                $fp = 0;
                if(isset($_SESSION['cart'])){
                    $product_id = array_column($_SESSION['cart'],'product_id');
                    $sql = "SELECT * FROM muzi;";
                    $result = mysqli_query($conn, $sql);
                    while($row = mysqli_fetch_assoc($result)){
                        foreach($product_id as $id){
                            if($row['ID'] == $id){          
                echoItem($row['fotografia'],$row['price'],$row['znacka'],$row['konkretne'],$size);
                                $fp = $fp   intval($row['price']);
                            }
                        }
                    }
                }else{
                    echo "<h5>Cart is empty</h5>";
                }
               ?>
   <h2 >PRICE: <?php echo $fp;?> €</h2>
</section>
  •  Tags:  
  • Related