简体   繁体   English

如何从会话数组中选择数据库中的项目?

[英]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". 这将添加数量为“3”的项目ID“1”,并添加数量为“1”的项目ID“18”。 I want to show these items from database on cart page. 我想在购物车页面上显示数据库中的这些项目。 I'm not good in php or sql, but something like: 我不擅长php或sql,但是类似于:

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. 并执行find $id(from session) = $id(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 : 这是一个快速示例,突出显示您要从$ _SESSION中检索的内容(id_item):

http://www.tehplayground.com/#Iyf7c0LTM 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查询:

$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 关于“foreach如何与数组交互”的高级解释: 这里

EDIT 2 : Fill db variables + column names of your table 编辑2:填充表变量的db变量+列名

<?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: 而不是从数据库中获取所有内容然后在PHP中进行比较,您可以执行类似的操作,只获取所需的记录:

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

The PHP might be as simple as: $in = implode(",", $_SESSION['cart']); PHP可能很简单: $in = implode(",", $_SESSION['cart']); although you should also make sure to protect against SQL injection. 虽然你也应该确保防止SQL注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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