2016-08-18 80 views
2

我還是比較新的PHP和本網站,所以我現在道歉!這是讓我瘋狂,我想添加一個數組會議狀態購物車我從不同的拼湊的碼位....php訪問會議陣列

$_SESSION['cart_items'] = 
array(
     'product_name' => $name, 
     'productId' => $id, 
     'quantity' => 1 
); 

^這是它添加到會話狀態的一部分,因爲我printr和它出來像這樣

Array ([product_name] => The Ned Rose [productId] => 1 [quantity] => 1) 

這能正常工作是位我無法工作。如何訪問產品ID的,所以我可以在SQL查詢中使用它們來獲取數據來填充車...

if(count($_SESSION['cart_items'])>0){ 
$ids = ""; 
foreach($_SESSION['cart_items']['productId'] as $id=>$value){ 
    $ids = $ids . $id . ","; 

} 

感謝,

編輯在這裏購物車頁 任何人都可以看到我錯了?

<?php 
session_start(); 
$page_title="Cart"; 
include 'mysql.php'; 
print_r($_SESSION['cart_items']); 
$action = isset($_GET['action']) ? $_GET['action'] : ""; 
$name = isset($_GET['name']) ? $_GET['name'] : ""; 

if($action=='removed'){ 
echo "<div class='alert alert-info'>"; 
echo "<strong>{$name}</strong> was removed from your cart!"; 
echo "</div>"; 
} 

else if($action=='quantity_updated'){ 
echo "<div class='alert alert-info'>"; 
    echo "<strong>{$name}</strong> quantity was updated!"; 
echo "</div>"; 
} 
if(count($_SESSION['cart_items'])>0){ 
$ids = ""; 
$ids = array_keys($_SESSION['cart_items']); 
foreach($_SESSION['cart_items'][$id] as $key=>$value){ 
    $ids = $ids . $id . ","; 
} 


// remove the last comma 
$ids = rtrim($ids, ','); 

//start table 
echo "<table class='table table-hover table-responsive table-bordered'>"; 

    // our table heading 
    echo "<tr>"; 
     echo "<th class='textAlignLeft'>Product Name</th>"; 
     echo "<th>Price (GBP)</th>"; 
     echo "<th>Action</th>"; 
    echo "</tr>"; 

    $query = "SELECT prodID, prodName, prodPrice FROM product_tbl WHERE prodID IN ({$ids}) ORDER BY prodName"; 
    $result = mysqli_query($db, $query); 
    $total_price=0; 
    while ($row = mysqli_fetch_assoc($result)){ 
     extract($row); 

     echo "<tr>"; 
      echo "<td>".$row['prodName']."</td>"; 
      echo "<td>£".$row['prodPrice']."</td>"; 
      echo "<td>"; 
       echo "<a href='remove_from_cart.php?id=".$row['prodID']."&name=".$row['prodName']."' class='btn btn-danger'>"; 
        echo "<span class='glyphicon glyphicon-remove'></span> Remove from cart"; 
       echo "</a>"; 
      echo "</td>"; 
     echo "</tr>"; 

     $total_price+=$row['prodPrice']; 
    } 

    echo "<tr>"; 
      echo "<td><b>Total</b></td>"; 
      echo "<td>£{$total_price}</td>"; 
      echo "<td>"; 
       echo "<a href='#' class='btn btn-success'>"; 
        echo "<span class='glyphicon glyphicon-shopping-cart'></span> Checkout"; 
       echo "</a>"; 
      echo "</td>"; 
     echo "</tr>"; 

echo "</table>"; 

}

否則{ 回聲 「」; 回聲「未找到任何商品您的購物車!點擊此處返回商店」; echo「」; }

>

+0

$ _SESSION [「cart_items」] [「的productId」]會得到你的車ID –

+0

,但我想還可以添加你很可能是最好的添加籃下線到一個數據庫表,而不是一個會話 –

+0

$ _SESSION ['cart_items'] ['productId']的內容似乎不是一個數組,爲什麼使用「foreach」呢? 以及,我認爲你應該看看implode函數(http://php.net/manual/fr/function.implode.php) – Steven

回答

0

您可以將產品添加到陣列,使你不需要直接存放「的productId」值。

// Set data 
$_SESSION['cart_items'][$id] = Array('name'=>$name,'qty'=>1); 

// Show session content 
foreach($_SESSION['cart_items'] as $id=>$props){ 
    echo 'id='.$id.'<br />'; 
    echo 'name='.$props['name'].'<br />'; 
    echo 'qty='.$props['qty']; 
} 

// Collect ids (result is an array of ids) 
$ids = array_keys($_SESSION['cart_items']; 

// Use $ids in a query 
$sql = "SELECT * FROM your_table WHERE id_field IN('".implode("','",$ids)."')"; 
+0

我的大部分成功都是僥倖,我用這個答案,而不是echo'n會話,我把ids + = id。 ID行,它工作! –

1

使用$ id作爲購物車中的存儲產品時的KEY值:

$_SESSION['cart_items'][$id] = 
array(
     'product_name' => $name, 
     'productId' => $id, 
     'quantity' => 1 
); 

然後你就可以在foreach循環使用ID:

if(count($_SESSION['cart_items']) > 0){ 
    foreach($_SESSION['cart_items']['productId'] as $id => $value){ 
     // Get data for this product 

    } 
} 
0

您可以使用以下內容

$_SESSION['cart_items'][] = 
array(
     'product_name' => $name, 
     'productId' => $id, 
     'quantity' => 1 
); 

或(這將更新車先前添加的產品ID)

$_SESSION['cart_items'][$id] = 
array(
     'product_name' => $name, 
     'productId' => $id, 
     'quantity' => 1 
); 


if(count($_SESSION['cart_items'])>0){ 
$ids = ""; 
foreach($_SESSION['cart_items'] as $id=>$value){ 
    $ids = $ids . $id . ","; 

} 
}