2015-10-08 4 views
-1

J'essaie d'enregistrer plusieurs commandes du panier mais il semble que le code que j'utilise n'est pas tout à fait correct. Je reçois les valeurs de la session. J'ajoute aussi d'autres aliments à mon panier.Décalage non défini avec la session PHP

Heres mon erreur

Notice: Undefined offset: 0 en C: \ xampp \ htdocs \ projet \ mrpl123 \ chart.php en ligne 53

Notice: Undefined offset: 0 en C: \ xampp \ htdocs \ projet \ mrpl123 \ chart.php sur la ligne 54

Indication: Undefined offset: 0 à C: \ xampp \ htdocs \ projet \ mrpl123 \ chart.php sur la ligne 55

Indication: Undefined offset: 0 à C: \ xampp \ htdocs \ projet \ mrpl123 \ chart.php sur la ligne 56

Indication: Undefined offset: 1 à C: \ xampp \ htdocs \ projet \ mrpl123 \ chart.php sur la ligne 53

Indication: Undefined offset: 1 à C: \ xampp \ htdocs \ projet \ mrpl123 \ chart.php sur la ligne 54

Indication: Undefined offset: 1 à C: \ xampp \ htdocs \ projet \ mrpl123 \ chart.php en ligne 55

Remarque: non défini offset: 1 à C: \ xampp \ htdocs \ projet \ mrpl123 \ chart.php sur la ligne 56

Indication: Undefined offset: 2 in C: \ xampp \ htdocs \ projet \ mrpl123 \ chart.php en ligne 53

Indication: Undefined offset: 2 in C: \ xampp \ htdocs \ projet \ mrpl123 \ chart.php sur la ligne 54

Indication: Undefined offset: 2 in C: \ xampp \ htdocs \ projet \ mrpl123 \ chart.php à la ligne 55

Remarque: Offset non défini: 2 dans C: \ xampp \ htdocs \ project \ mrpl123 \ chart.php en ligne 56 Merci! votre commande a été placée!

Voici mon code

<?php 
    session_start(); 
    //mysql_connect("localhost", "root", "") or die(mysql_error()); 
////select database 
//mysql_select_db("rapal") or die(mysql_error()); 
    require_once("dbcon.php"); 
    $db_handle = new DBController(); 
    if(!isset($_SESSION['user'])) 
    { 
    header("Location: login.php"); 
    } 
    $user = $_SESSION['user']; 
    $res=mysql_query("SELECT * FROM members_account WHERE uid='$user'") or die(mysql_error()); 
    $userRow=mysql_fetch_array($res); 


    if(!empty($_GET["action"])) { 
    switch($_GET["action"]) { 
     case "add": 
      if(!empty($_POST["quantity"])) { 
       $productByCode = $db_handle->runQuery("SELECT * FROM menus WHERE code='" . $_GET["code"] . "'"); 
       $itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["food_name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["food_price"])); 

       if(!empty($_SESSION["cart_item"])) { 
        if(in_array($productByCode[0]["code"],$_SESSION["cart_item"])) { 
         foreach($_SESSION["cart_item"] as $k => $v) { 
           if($productByCode[0]["code"] == $k) 
            $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"]; 
         } 
        } else { 
         $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray); 
        } 
       } else { 
        $_SESSION["cart_item"] = $itemArray; 
       } 
      } 
     break; 
     case "remove": 
      if(!empty($_SESSION["cart_item"])) { 
       foreach($_SESSION["cart_item"] as $k => $v) { 
         if($_GET["code"] == $k) 
          unset($_SESSION["cart_item"][$k]);    
         if(empty($_SESSION["cart_item"])) 
          unset($_SESSION["cart_item"]); 
       } 
      } 
     break; 
     case "checkout": 
     $member=$_POST['namex']; 

      $max=count($_SESSION['cart_item']); 
      for($i=0;$i<$max;$i++){ 
       $pname=$_SESSION['cart_item'][$i]['name']; 
       $pid=$_SESSION['cart_item'][$i]['code']; 
       $q=$_SESSION['cart_item'][$i]['quantity']; 
       $price=$_SESSION['cart_item'][$i]['price']; 
       mysql_query("insert into checkout_records values ('',$member,$pname,$pid,$q,$price,'In process')"); 
      } 
      echo 'Thank You! your order has been placed!'; 
    // $code5 = $_POST['code']; 
    // $food_qty = $_POST['qty']; 
    // $price = $_POST['price']; 
    // $data = $_SESSION['cart_item']; 
    // $result = mysql_query ("INSERT INTO checkout_records(member,food_order,code,food_qty,food_price,status) VALUES ('$member','".$data['name']."','".$data['code']."','".$data['quantity']."','".$data['price']."','In process')"); 
    //  
    // if($result && mysql_num_rows($result)>0) { 
    //// all ok 
    //  echo "inserted"; 
    // } 
    // else { 
    //  echo "not inserted"; 
    // } 
    // $query1 = "INSERT INTO checkout_records (member,food_order,code,food_qty,food_price,status)VALUES('$member','$food_order','$code5','$food_qty','$price','In process')"; 
    // 
    ////if (mysql_query($query1)) { 
    ////  //dislay a message box that the saving is successfully save 
    ////  $uid = mysql_insert_id(); // last inserted id 
    ////  $result = mysql_query("SELECT * FROM checkout_records WHERE id = '$uid'"); 
    ////  echo "<script type=\"text/javascript\"> 
    ////     alert(\"Thank You!\"); 
    ////     window.location = \"chart.php?action=empty\" 
    ////     </script>"; 
    ////  } else { 
    ////   echo "Failed: " . mysql_error(); 
    ////  } 

     break; 
     case "empty": 
      unset($_SESSION["cart_item"]); 
     break; 
    } 
    } 
    ?> 

MISE À JOUR SESSION MATRICE SORTIE

Array ([user] => 55f0497487b335.17059914 [cart_item] => Array ([main1] => Array ([name] => adobo [code] => main1 [quantity] => 200 [price] => 1) [main3] => Array ([name] => asado [code] => main3 [quantity] => 1 [price] => 1) [main2] => Array ([name] => afritada [code] => main2 [quantity] => 123 [price] => 1)) [cart] => Array ([0] => Array ([productid] => 1 [qty] => 1) [1] => Array ([productid] => 2 [qty] => 1) [2] => Array ([productid] => 3 [qty] => 1))) 

me aider quelqu'un peut avec cela?

+1

Quelle est la ligne 53? – Halcyon

+1

ouais^il n'y a que 48 lignes ici. Certains comprendra compte de ceux-ci. –

+0

désolé attendre malade mettre à jour les codes son pas complet –

Répondre

1

Mon erreur est que je utilise for loop au lieu de foreach Merci à Anurag Verma :) son travail maintenant