gpt4 book ai didi

用于存储和仅显示最后 5 个 View 产品的 php session

转载 作者:行者123 更新时间:2023-11-29 06:04:26 25 4
gpt4 key购买 nike

我想建立一个 session 来存储然后显示用户看到的最后 5 个产品......但是不能正常工作

如果我引用并且我只想存储不同的 ID(产品),首先是存储相同的 ID 产品2. 其次,即使我有更多的 ID 存储(用户看到的不同产品,如 id:3、5、5、6、2),也不会向我显示超过 1 行(1 个查询),只向我显示最后一个 ID在这种情况下存储2....我想显示所有查询..

.

if (isset($_GET['id'])) {
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
$id = preg_replace('#[^0-9]#i', '', $_GET['id']);
$_SESSION['lastViewProducts'][] = $id;
if(count ($_SESSION['lastViewProducts']) > 5 ){
array_shift($_SESSION['lastViewProducts']);
}
foreach( $_SESSION['lastViewProducts'] as $keyLASTview=>$valueLASTview) {
$stmtLastVIEW = $con->prepare('SELECT id, product_name, price, details, category, subcategory, size, date_added, image, brand_name, product_color, vizualizari FROM products WHERE id=?');
$stmtLastVIEW->bind_param('i', $valueLASTview);
$stmtLastVIEW->execute();
$stmtLastVIEW->bind_result($idSelectDetalii, $produsNumeDetalii, $priceDetalii, $produsDetalii, $produsCategory, $produsSubcategory, $produsSize, $produsDate_added, $imageLocationDetalii, $brandProdusSelectat, $produsColor, $produsVizualizari);
$lastVIEWproduct = '';
while ($stmtLastVIEW->fetch()) {
$lastVIEWproduct .='titlu: '.$idSelectDetalii.' <img src="'.$imageLocationDetalii.'" class="img-responsive">';
}
$stmtLastVIEW->free_result();
}
}

我真的被卡住了...我想不出解决这个问题也许其他变体?另外 3. 第三,如果用户在同一页面上 products.php?id=3 并且它存储在 session 中的 id=3 我不想显示这个... WHERE id=?(for the showing last 5 products views) and id is not (the $id)? mysql的select语句和where子句怎么写?如果我也想要一个否定

最佳答案

你的逻辑有一些错误。您可以尝试使用此代码:

if (isset($_GET['id'])) {

// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
$id = preg_replace('#[^0-9]#i', '', $_GET['id']);

//1. Store only differents ids in the session by creating an associative array
$_SESSION['lastViewProducts'][$id] = $id;

//3. Don't show the $_GET['id'] in the list
$ids = array_filter($_SESSION['lastViewProducts'], function($currentID) use($id) {
return $id != $currentId;
});


//Keep only 5 distincts articles
$ids = array_slice(array_unique($ids), 0, 5);

//2. declare your string OUTSIDE the foreach
$lastVIEWproduct = '';
foreach($ids as $valueLASTview) {
$stmtLastVIEW = $con->prepare('SELECT id, product_name, price, details, category, subcategory, size, date_added, image, brand_name, product_color, vizualizari FROM products WHERE id=?');
$stmtLastVIEW->bind_param('i', $valueLASTview);
$stmtLastVIEW->execute();
$stmtLastVIEW->bind_result($idSelectDetalii, $produsNumeDetalii, $priceDetalii, $produsDetalii, $produsCategory, $produsSubcategory, $produsSize, $produsDate_added, $imageLocationDetalii, $brandProdusSelectat, $produsColor, $produsVizualizari);
while ($stmtLastVIEW->fetch()) {
$lastVIEWproduct .='titlu: '.$idSelectDetalii.' <img src="'.$imageLocationDetalii.'" class="img-responsive">';
}

$stmtLastVIEW->free_result();
}
}

关于用于存储和仅显示最后 5 个 View 产品的 php session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42635082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com