2013-02-18 22 views
-1

我希望通過存儲來自ORDER頁面的訂單值並將其顯示在CART頁面上來保存產品。這裏是兩個頁面編碼。任何人都可以幫助我嗎?這是爲wordpress主題。在PHP會話中存儲乘數變量並使用循環顯示全部

訂購頁面

<?php 
session_start(); 
/* 
TEMPLATE NAME: Order Page 
*/ 


$git_product_id = $_GET['git_product_id']; 
$git_required = $_GET['git_required']; 
$git_action = $_GET['git_action']; 

if ($git_product_id != "" && $git_required != "" && $git_action != ""){    
    ?> 
     <form action="" method="POST" class="add-form"> 
      <label> 
       <?php 

        switch($git_required){ 

         case "name" : 
          // showing Name 
          echo "Account Name:"; 
         break; 

         case "url" : 
          // showing url 
          echo "Web Url:"; 
         break; 

        }; 
       ?> 
      </label> 
      <input type="text" name="git_required" value="" /> 
      <input type="hidden" name="git_product_id" value="<?php echo $git_product_id; ?>" /> 
      <input type="hidden" name="git_action" value="add" /> 
      <input type="submit" value="Add to cart" class="add-to-cart-button" /> 
     </form> 
    <?php 

} else { 

    echo "Sorry an error took place."; 
} 

?>

=============================== ============================

車頁面

<?php 
session_start(); 
/* 
TEMPLATE NAME: Cart Page 
*/ 

?>

<?php 
    $git_product_id = $_POST['git_product_id']; 
    $git_required = $_POST['git_required']; 
    $git_action = $_POST['git_action']; 

    if ($git_product_id != "" && $git_action != ""){ 

     switch($git_action){ 

      case "add" : 
       // adding product 
        // checking first if the product is already added 
        if (isset($_SESSION['cart'][$git_product_id])){ 
         echo "You have already added this product";      
        } else { 
         // I AM NOT SURE IF THIS CODING IS OKAY. PLEASE CHECK THIS 
         $_SESSION['cart'][$git_product_id] = array('product_id' => $git_product_id, 'git_required' => $git_required); 
        } 
      break; 

      case "remove": 
       // removing product 
       unset($_SESSION['cart'][$git_product_id]); 
      break; 

      case "empty" : 
       // empty cart 
       unset($_SESSION['cart']); 
      break;    

     } 
    } 
?> 

<?php 

    if ($_SESSION['cart'] != ""): 
     foreach($_SESSION['cart'] as $product => $qty) : ?> 
     <tr> 
      <td> 
       <?php // I WANT TO SHOW HERE EACH PRODUCT ID and RESPECTIVE REQUIRED INFO ?> 
       <?php // BUT I DON'T KNOW HOW TO DO IT ?> 
      </td> 

      <td> 
       <form action="" method="post"> 
        <input type="hidden" name="git_product_id" value="<?php echo $product; ?>" /> 
        <input type="hidden" name="git_required" value="<?php echo $qty; ?>" /> 
        <input type="hidden" name="git_action" value="remove" /> 
        <input type="submit" value="Remove" /> 
       </form> 
      </td> 

     <?php endforeach; ?> 
    <?php endif; ?> 

    <form action="" method="POST"> 
     <input type="hidden" name="git_product_id" value="<?php echo $product; ?>" /> 
     <input type="hidden" name="git_required" value="<?php echo $qty; ?>" /> 
     <input type="hidden" name="git_action" value="empty" /> 
     <input type="submit" value="empty" /> 
    </form>    
+2

當您回顯產品詳細信息時,刪除$ product周圍的單引號 – Raad 2013-02-18 12:58:26

+0

您需要向帖子中添加更多詳細信息。 - 什麼不工作?它顯示什麼或什麼都沒有? - 是否顯示任何錯誤? - 您是否使用現有的購物車組件?添加這個名字可以幫助那些有經驗的人幫助你。 – youcantseeme 2013-02-18 12:59:09

+0

@ youcantseeme:請檢查主張。我已經共享了訂單頁面和購物車頁面的編碼。 – Nur 2013-02-18 14:06:12

回答

3

你寫$_SESSION['cart']['$product']...而是你應該寫$_SESSION['cart'][$product]...沒有「輪$產品...

說明:'$product'不是$產品的價值,但只有字符串$產品

$product = 5; 
var_dump($product);  // -> int(5) 
var_dump("$product");  // -> string(1) "5" 
var_dump('$product');  // -> string(8) "$product"