所以我需要显示在卖方页面上添加的项目,现在在购物车数组中显示在列表中的购物车页面上
Array
(
[cart] => Array
(
[BRB] => 1
)
)
以下是我正在处理的购物车页面的代码:
if (isset($_SESSION['cart'])) {
foreach ($vend as $vendID => $items) {
//if (array_search($vendID, $_SESSION['cart'])) {
echo "<article class ='cart' id='cart-$vendID'>";
echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>";
echo "<div class ='item-no'>";
echo "<p class = 'pro-id'><b>Product ID: </b>{$vendID}</p></div>";
echo "<div class ='img-div'>";
echo "<img src =../images/{$items['img']} alt='' height='196' width='200'></div>";
echo "<div class='pricing'>";
echo "<p><b>Price: $</b>{$items['price']}</p></div>";
echo "</article>";
//}
}
}
我评论了我尝试过但没有工作的代码。所以我需要一个IF语句,如果vendID在cart数组中,则显示在列表中。
由于我被卡住了,所以有些人会感激不尽。
答案 0 :(得分:0)
你试过in_array()吗? 它检查数组中是否存在值。
答案 1 :(得分:0)
您的购物车应如下:
$cart = array(
[0] => Array
(
[item_id] => 1,
[title] => "your title",
[price] => 100,
[img] => "name of the file",
),
[1] => Array
(
[item_id] => 2,
[title] => "your title",
[price] => 200,
[img] => "name of the file",
)
);
然后尝试如下:
if (isset($_SESSION['cart'])) {
foreach ($cart as $key => $items) { //$key = [0], [1] ....
echo "<article class ='cart' id='cart-$key'>";
echo "<h1 class = 'item-h1' id = 'h1'>".$items['title']."</h1>";
echo "<div class ='item-no'>";
echo "<p class = 'pro-id'><b>Product ID: </b>".$items['item_id']."</p></div>";
echo "<div class ='img-div'>";
echo "<img src =../images/".$items['img']." alt='' height='196' width='200'></div>";
echo "<div class='pricing'>";
echo "<p><b>Price: $</b>".$items['price']."</p></div>";
echo "</article>";
}
}
马克,它接受了它是否有效。