Ich bin immer noch relativ neu zu PHP und dieser Website, so entschuldige ich mich jetzt! Dies macht mich verrückt, ich versuche, ein Array zum Sitzungsstatus für einen Warenkorb hinzufügen ich bin zusammen von verschiedenen Bit-Code ....PHP Zugriff Sitzungen Array
$_SESSION['cart_items'] =
array(
'product_name' => $name,
'productId' => $id,
'quantity' => 1
);
^das ist der Teil, in dem es um die Sitzungszustand hinzufügt, das funktioniert gut, wie ich printr und es kommt wie diese
Array ([product_name] => The Ned Rose [productId] => 1 [quantity] => 1)
dies ist das Bit dass ich nicht arbeiten kann. Wie kann ich die Produkt-IDs zugreifen, so dass ich sie in einer SQL-Abfrage verwenden kann, um die Daten zu holen um den Warenkorb zu füllen ...
if(count($_SESSION['cart_items'])>0){
$ids = "";
foreach($_SESSION['cart_items']['productId'] as $id=>$value){
$ids = $ids . $id . ",";
}
Danke,
EDIT HIER DIE WAGEN PAGE kann jeder sehen, wo Ich gehe falsch?
<?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>";
}
else { echo ""; echo "Keine Produkte gefunden in Ihrem Warenkorb! Klicken Sie hier, um zum Shop zurückzukehren"; Echo ""; }
>
$ _SESSION [ ‚cart_items‘] [ ‚productId‘] Ihre Einkaufswagen ID –
bekommen, aber ich möchte auch Sie wahrscheinlich am besten wäre hinzuzufügen Zugabe der Korb Linie auf eine DB-Tabelle statt einer Sitzung –
der Inhalt von $ _SESSION ['cart_items'] ['productId'] scheint kein Array zu sein, warum benutzt du eine "foreach"? Auch ich denke, Sie sollten einen Blick auf implode Funktion haben (http://php.net/manual/fr/function.implode.php) – Steven