How can I pull items from the database using a session array?

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

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

      

This will add item ID "1" with count "3" and add item ID "18" with count "1". I want to show these items from the database on the cart page. I am not good at 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 can show the session as information from the database. With product name, product description, product value and quantity.

+3


source to share


2 answers


Here's a quick example to show you are trying to extract (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 now use ur sql query:

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

      

EDIT . Since it was not clear to you, I answered directly:



<?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 
// /questions/831/how-does-php-foreach-actually-work
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>";

?>

      

Extended explanations on "how foreach interacts with an array": here

EDIT 2: Populate 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

+2


source


Instead of fetching everything from the database and then comparing in php, you can do something like this to get the records you want:

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

      



PHP can be as simple as: $in = implode(",", $_SESSION['cart']);

although you should also be sure to protect against SQL injection.

0


source







All Articles