[英]while and foreach loop with array
我正在为我的项目制作推车。 首先,访客(未登录)将从存储在SESSIONS中的类别中选择产品。 其次,访客将登录。我希望将SESSION中的所有项目存储在用户数据库中,或者如果存在,则进行更新。
这是我的代码:
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
//Log
$core->testLog("Database Product: " . $row['product_id']);
foreach($_SESSION['product_checkout_list'] as $key => $value) {
//Log
$core->testLog("- Array Product: " . $_SESSION['product_checkout_list'][$key]['checkID']);
if($value['checkID'] == $row['product_id']) {
$insert = $core->prepare("UPDATE on_cart SET user_id = :uid, qty = :qty, stamp = :stamp WHERE product_id = :prod_id");
$insert->bindParam(":uid", $_SESSION['user']['id']);
$insert->bindParam(":prod_id", $value['checkID']);
$insert->bindParam(":qty", $value['checkQty']);
$insert->bindparam(":stamp", $timestamp);
$insert->execute();
//Log
$core->testLog("-- " . $value['checkID'] . " is equal to " . $row['product_id']);
} else {
$insert = $core->prepare("INSERT INTO on_cart VALUES(null, :uid, :prod_id, :qty, :stamp);");
$insert->bindParam(":uid", $_SESSION['user']['id']);
$insert->bindParam(":prod_id", $value['checkID']);
$insert->bindParam(":qty", $value['checkQty']);
$insert->bindparam(":stamp", $timestamp);
$insert->execute();
//Log
$core->testLog("-- " . $value['checkID'] . " is not equal to " . $row['product_id']);
}
unset($_SESSION['product_checkout_list'][$key]);
// Log
foreach($_SESSION['product_checkout_list'] as $key => $value) {
$core->testLog("--- " . $value['checkID']);
}
break;
}
}
我的日志是这样的:
03/20/2018 - 10:36:21 PM : Database Product: 1
03/20/2018 - 10:36:21 PM : - Array Product: 1
03/20/2018 - 10:36:21 PM : -- 1 is equal to 1
03/20/2018 - 10:36:21 PM : --- 2
03/20/2018 - 10:36:21 PM : --- 4
03/20/2018 - 10:36:21 PM : Database Product: 2
03/20/2018 - 10:36:21 PM : - Array Product: 2
03/20/2018 - 10:36:21 PM : -- 2 is equal to 2
03/20/2018 - 10:36:21 PM : --- 4
我的问题是最后一个产品4-由于中断而被跳过。 但是,如果删除中断,则不会刷新$row['product_id']
(它将保持为1,直到所有操作完成)。 像这样:
03/20/2018 - 10:38:57 PM : Database Product: 1
03/20/2018 - 10:38:57 PM : - Array Product: 1
03/20/2018 - 10:38:57 PM : -- 1 is equal to 1
03/20/2018 - 10:38:57 PM : --- 2
03/20/2018 - 10:38:57 PM : --- 4
03/20/2018 - 10:38:57 PM : - Array Product: 2
03/20/2018 - 10:38:57 PM : -- 2 is not equal to 1
03/20/2018 - 10:38:57 PM : --- 4
03/20/2018 - 10:38:57 PM : - Array Product: 4
03/20/2018 - 10:38:57 PM : -- 4 is not equal to 1
03/20/2018 - 10:38:57 PM : Database Product: 2
我想做的是,访客或用户可以在登录之前先选择他们的产品。如果用户已经登录,系统将检查用户是否在SESSION购物车上有物品并将其插入数据库或更新现有的物品。 我两天前一直在考虑解决方案,但无法解决。 谢谢!
尽量避免在开关块以外的任何地方使用break。
此外,您正在以相反的方式进行搜索。 您需要针对阵列中的每个产品,检查它是否在数据库中。 看一看:
// Will only populate an array for easier search
$dbItems = array();
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
$dbItems[] = $row;
}
//Log
foreach($_SESSION['product_checkout_list'] as $key => $value) {
//Log
$core->testLog("- Array Product: " . $_SESSION['product_checkout_list'][$key]['checkID']);
// Flag to mark if the item is on the db or not
$found = false;
foreach($dbItems as $row) {
if($value['checkID'] == $row['product_id']) {
$insert = $core->prepare("UPDATE on_cart SET user_id = :uid, qty = :qty, stamp = :stamp WHERE product_id = :prod_id");
$insert->bindParam(":uid", $_SESSION['user']['id']);
$insert->bindParam(":prod_id", $value['checkID']);
$insert->bindParam(":qty", $value['checkQty']);
$insert->bindparam(":stamp", $timestamp);
$insert->execute();
//Log
$core->testLog("-- Found " . $value['checkID'] . " in db as " . $row['product_id']);
// Flag it as found, so we will not insert it
$found = true;
}
}
// Not found on the db? Lets insert it
if(!$found) {
$insert = $core->prepare("INSERT INTO on_cart VALUES(null, :uid, :prod_id, :qty, :stamp);");
$insert->bindParam(":uid", $_SESSION['user']['id']);
$insert->bindParam(":prod_id", $value['checkID']);
$insert->bindParam(":qty", $value['checkQty']);
$insert->bindparam(":stamp", $timestamp);
$insert->execute();
//Log
$core->testLog("-- Product " . $value['checkID'] . " was not on the db");
}
}
// Empty the session array
$_SESSION['product_checkout_list'] = array();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.