2016-08-18 79 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 . ","; 

} 
}