简体   繁体   中英

How can I pull items from database select with session array?

I have code that adds an array to a session like this:

array_push($_SESSION['cart']['1'] = 3);
array_push($_SESSION['cart']['18'] = 1);

This would add item id "1" with quantity "3" and add item id "18" with quantity "1". I want to show these items from database on cart page. I'm not good in php or sql, but something like:

while (list ($id, $quantity) = each ($_SESSION['cart'])) { 
$results = $conn->query("SELECT * FROM products ORDER BY id ASC");
}

and do something like find $id(from session) = $id(from database) so I could show session as information from database. With item name, item desc., item price, and quantity.

Here is a quick example highlighting what u're trying to retrieve (id_item) from $_SESSION :

http://www.tehplayground.com/#Iyf7c0LTM

$arr = array();
$arr['cart']['1'] = 3;
$arr['cart']['18'] = 5;

// This loop will display every id_item that had be added to the "cart"
foreach($arr['cart'] as $row)
{
    print "id_item : " . $row . "\n";
}

U can use now make ur sql query :

$sql = "SELECT * FROM products WHERE id =". $row;

EDIT - Since it was unclear for you, I made u the direct answer :

<?php
session_start();

// Examples of shopped items (added to $_SESSION['cart'])
$_SESSION['cart']['1'] = 3;
$_SESSION['cart']['18'] = 5;

// This loop will display every id_item that had be added to the "cart"
// Further informations how foreach works with array 
// https://stackoverflow.com/questions/10057671/how-foreach-actually-works
foreach($_SESSION['cart'] as $row)
{
    print "id_item : " . $row . "\n";
}

// Full Display to help understanding the process
print "<pre>";
print_r($_SESSION['cart']);
print "</pre>";

?>

Advanced explanations about "how foreach interacts with array" : here

EDIT 2 : Fill db variables + column names of your table

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "";

$mysqli = new mysqli($servername, $username, $password, $dbname);

// check connection
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

// Query the DB - Get itemID & quantity added in the cart table
$query = "SELECT itemID, itemQuantity FROM cart";
$result = $mysqli->query($query);

// Return the result into an array
$res_array = $result->fetch_array();

// Add the items into $_SESSION
// Pattern : $_SESSION['cart']['$itemId'] = $itemQuantity

foreach($res_array as $row)
    $_SESSION['cart'][($row['itemID'])] = $row['itemQuantity']

print "<pre>";
print_r($_SESSION['cart']);
print "</pre>";
?>

Example : here

Rather than getting everything from the database and then comparing in php you can do something like this to only get the records that you need:

"SELECT * FROM products WHERE id IN(1,18) ORDER BY id ASC"

The PHP might be as simple as: $in = implode(",", $_SESSION['cart']); although you should also make sure to protect against SQL injection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM