2016-08-18 10 views
2

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 ""; }

>

+0

$ _SESSION [ ‚cart_items‘] [ ‚productId‘] Ihre Einkaufswagen ID –

+0

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 –

+0

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

Antwort

0

Sie Produkte zum Array hinzufügen könnten, so dass Sie nicht direkt auf den „productId“ Wert speichern haben müssen.

// 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

die meisten meiner erfolge sind fluke, ich habe diese antwort verwendet und statt echo'n der sitzung habe ich die ids + gesetzt = ID. id line und es hat funktioniert! –

1

Verwenden $ id als Schlüsselwert, wenn das Produkt in den Warenkorb gelegt Lagerung:

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

Dann können Sie die ID in der foreach-Schleife verwenden:

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

    } 
} 
0

Sie können die folgenden Nummern verwenden:

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

oder (dies zuvor hinzugefügt Produkt-ID im Warenkorb aktualisieren)

$_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 . ","; 

} 
}