2017-07-10 19 views
0

eコマースのウェブサイトを作りたいと思います。しかし、私の問題はショッピングカートにあります。ショッピングカートが空の場合、このタイプのメッセージが表示されます。(通知:未定義のインデックス:C:\ xampp \ htdocs \ online shop \ user \ checkout.phpの57行目のカート) どうすれば解決できますかこの問題。私がこの「カート」に0または0を割り当てると、ショッピングカートが正しく機能しません。私は正確な提案が必要です。私のショッピングカートが空の場合、この警告が表示されますNotice:未定義のインデックス:C: xampp htdocs online shop user checkout.phpの57行目

は、これはこれは私のdalSession.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']}"); 

    ?> 

私checkout.phpページ(ここで57数ライン)

<?php session_start(); 
require_once("../dataAccessLayer/dalSession.php"); 
require_once("../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\"><img style=\"width: 50px;height: 50px;\"  src=\"../image/$r[5]\"></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");?> 

がこれは私のadd_to_cart.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(); 
     } 
     } 

     } 


     } 

     ?> 

答えて

0

を設定されているかどうかをチェックするだけでなくarray_key_exists('cart',$_SESSION)を使用することができます
空の場合checkout.php

<?php session_start(); 
require_once("../dataAccessLayer/dalSession.php"); 
require_once("../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\"><img style=\"width: 50px;height: 50px;\" 
    src=\"../image/$r[5]\"></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");?> 

ただ、このコードを追加します!あなたは `使用している場合、空の

<?php session_start(); 
require_once("../dataAccessLayer/dalSession.php"); 
require_once("../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'])){ 
     if(!empty($_SESSION['cart']) && array_key_exists('cart',$_SESSION)) { 

     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\"><img style=\"width: 50px;height: 50px;\" 
    src=\"../image/$r[5]\"></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");?> 
0

あなたは、セッションがあなたに

if(isset($_SESSION['cart']) && !empty($_SESSION['cart'])) { 
    echo 'not empty'; 
    //do what you want to with your session value here 
} 

OR

if(!empty($_SESSION['cart']) && array_key_exists('cart',$_SESSION)) { 
    echo 'not empty'; 
    //do what you want to with your session value here 
} 
+0

' ISSET() ')(完全に無意味である' – CD001

+0

はい、それらは代替案です。 'array_key_exists( 'cart'、$ _ SESSION)&&!empty($ _ SESSION ['cart'])'で十分です – Regolith

+0

変更したい部分を教えてください。 –

関連する問題