Last Updated: July 10, 2017
·
765
· ibrahimdunya

Show Undefined index: cart when my shopping cart is empty

Here is my all code.
checkout.php
<?php sessionstart();
require
once("../dataAccessLayer/dalSession.php");
requireonce("../dataAccessLayer/dalProduct.php");
require
once("../dataAccessLayer/dalService.php");
?>
<?php include ("../header2.php");?>
<br>
<div class="w3-content " style="max-width:1400px; margin-top:40px;">

<!-- The Grid -->
<div class="w3-row-padding">

<div class="w3-container w3-card-2 w3-white w3-margin-bottom ">

<br>
<div class="container">

<h1>Shopping Cart</h1><hr>
<table class="table table-striped table-hover table-bordered">
    <tbody>
    <tr>
        <th>Product</th>
        <th>Price</th>
        <th>Vat</th>
        <th>Discount</th>
        <th>Quantity</th>
        <th>Total Price</th>
        <th>Delete</th>
    </tr> 

    <?php

    if(isset($_POST['sub']))
    {

        for($i=0; $i<count($_SESSION['cart']); $i++)
        {
            //print "for loop in";
            if(isset($_POST['qty_' . $_SESSION['cart'][$i]]))
            {

                //print $_POST['qty_' . $_SESSION['cart'][$i]];
                $_SESSION['qty'][$i] = $_POST['qty_'.$_SESSION['cart'][$i]]; 
            }
        }

    }



    $total = 0;
    //$_SESSION['cart']=[];
   //if(isset($_POST['sub'])){      
        for($i=0; $i<count($_SESSION['cart']); $i++) //line 57
        {
            $p = new Product();
            $p->product_id = $_SESSION['cart'][$i];
            $r = $p->SelectById();
            print "<tr>";

print "<td align=\"center\"></td>";
echo "<td align=\"center\">$r[2]"," TK","</td>";
echo "<td align=\"center\">$r[3]","%","</td>";
echo "<td align=\"center\">$r[4]","%","</td>";
print "<td align=\"center\">";

    ?>
                    <form action="" method="post">
                      <input type="text" name="qty_<?php print $_SESSION['cart'][$i];?>" value="<?php print $_SESSION['qty'][$i];?>">
                      <input type="submit" name="sub" value="Update">
                    </form>
    <?php               
                print "</td>";
                print "<td align=\"center\">";
                       $price = $r[2] * $_SESSION['qty'][$i];               
                       echo "TK ".$t= $price + ($price * $r[3])/100 - ($price * $r[4])/100; 
                       $total += $t;  
                print "</td>";
                print "<td align=\"left\"><a class=\"btn btn-danger\" href=\"cart_remove.php?id={$_SESSION['cart'][$i]}\">Remove</a></td>";

            print "</tr>";

    }   


    ?>

                <form action="purchase.php" method="post">

                    <tr>
                        <th colspan="5"><span class="pull-right">Total</span></th>
                        <th><?php print "".$total." TK"; Session::Set("total",$total); ?></th>
                    </tr>
                    <tr>

                        <td colspan="6"><a href="../index.php" class="btn btn-primary">Continue Shopping</a></td>
                       <td colspan="5"><button type="submit" class="btn btn-success" name="sub" value="Confirm">
                        Checkout <span class="glyphicon glyphicon-play"></span></button>
                       </td>
                    </tr>   
                </form>
</table>
</div>
<br>
</div>
</div>
</div>

<?php include ("footer.php");?>

dalSession.php

<?php

class Session
{
public static function Start(){
session_start();
}

public static function Set($key , $value){
    $_SESSION[$key] = $value;
}

public static function Get($key){
    if (isset($_SESSION[$key])) {
        return $_SESSION[$key];
    }
    else{
        return false;
    }
}

public static function Stop(){
    session_destroy();
    header("Location:user/login.php");
}

public static function StopA(){
    session_destroy();
    header("Location:../user/login.php");
}

public static function StopB(){
    session_destroy();
    header("Location:login.php");
}


public static function Check()
{
    self::Start();
    //echo $this->user_id;;
    if (self::Get("Mlogin")==false) 
    {
        self::Stop();
        header("Location:login.php");
    }
}

public static function CheckA(){
    self::Start();
    if (self::Get("Alogin")==false) {
       self::StopA();
       header("Location:../user/login.php");
    }
}

public static function CheckAll()
{
    if (self::Get("Mlogin")==false && self::Get("Alogin")==false) 
    {
        return false;
    }
    else
    {
        return true;    
    }    
}

public static function CheckUserLogin()
{self::Start();
    if (self::Get("Mlogin")==false) 
    {
        return false;
    }
    else
    {
        return true;    
    }    
}

public static function Auto(){
    self::Start();
    if(self::Get("Mlogin")==false){
       echo "<a style=\"color:white;\" href='user/login.php'>Login</a>";
 } else {
       echo "<a style=\"color:red;\" href='?action=logout'>Logout</a>";
      if(isset($_GET['action']) && ($_GET['action']== "logout")){
        self::Stop();
    }
 }

}

public static function AutoA(){
    self::Start();
    if(self::Get("Mlogin")==false){
       echo "<a style=\"color:white;\" href='login.php'>Login</a>";
    } else {
       echo "<a style=\"color:red;\" href='?action=logout'>Logout</a>";
      if(isset($_GET['action']) && ($_GET['action']== "logout")){
        self::StopB();
       }
   }

}

}

?>

cart.php

<?php
requireonce("../dataAccessLayer/dalSession.php");
require
once("../dataAccessLayer/dalComment.php");
require_once("../dataAccessLayer/dalProduct.php");
?>

<?php
$pro = new Product();
$pro->productid = $GET['id'];
$r = $pro->SelectById();
?>

addtocart.php

<?php
require_once("../dataAccessLayer/dalSession.php");

Session::Start();

if(isset($_GET['id']))
{

if(isset($_SESSION['cart']))
{
    $c=0;
    for($i=0; $i<count($_SESSION['cart']); $i++)
    {   
        if($_SESSION['cart'][$i] == $_GET['id'])
        {
            $c++;
            break;
        }   
    }
    if($c == 0)
    {
        $_SESSION['cart'][] = $_GET['id'];
        $_SESSION['qty'][] = 1;
    }

}
else
{
    $_SESSION['cart'][] = $_GET['id'];
    $_SESSION['qty'][] = 1;     
}

}
header("Location: details.php?id={$_GET['id']}");

?>