3

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Max Underbed
  • 55
  • 1
  • 7

2 Answers2

2

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

Community
  • 1
  • 1
Falt4rm
  • 915
  • 6
  • 21
  • I tried this way and couldn't get anywhere. You know another way that would work? Or maybe describe this way better. You like takes to a page with code that does not do anything. Thanks. – Max Underbed Jun 02 '15 at 04:14
  • Ctrl+enter will display it. It.s a php fiddle – Falt4rm Jun 02 '15 at 05:50
  • Ok you aren't understanding my question. I need to show these items in `$_SESSION` from a database. Referencing by item id. I already have a full session and cart system set up. But it holds information like `array_push($_SESSION['cart']['$itemName'] = $itemPrice);` I am trying to do `array_push($_SESSION['cart']['$itemId'] = $itemQuantity);` then compare to database and pull `item1` info from database into cart. Thanks. God Bless. – Max Underbed Jun 02 '15 at 11:40
  • Are u using pdo or mysqli to connect to your db? – Falt4rm Jun 02 '15 at 11:48
  • Question was really unclear - Updated one last time with ur needs - See EDIT 2 – Falt4rm Jun 03 '15 at 11:57
0

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.