[英]¿How to show related products to the shopping cart products?
我想显示用户添加到购物车中的不同结果。
我通过方法GET
从产品中获取ID
,方法是单击一个URL。
<a href=http:./cart/$id/$url>
我的例子
products
表只有6个记录,也就是说有6个产品要购买。
现在,如果用户添加3种产品购买,使用id5
id1
id3
现在我的问题是,我该怎么让别人的产品表中products
,还没有被用户购买,在这种情况下id2
id4
id6
,正如我前面提到id5
id1
id3
购买。
我将针对购物车的不同结果生成的查询是这样的:
$result = $c->prepare("SELECT id_tutorial,page,titulo,info,icon,bg,vdo,url,precio,duracion,status FROM tutoriales WHERE id_tutorial!=? and status=1 LIMIT 6");
我在花逻辑!=
或<>
但是我不知道在使用此查询结果之前必须执行该过程。
这是我的购物车代码card.php :
<?php //Shopping Cart (./cart.php)
//GET -> id of the product.
if (isset($_GET['articulo'])) {
$id_tutorial = $_GET['articulo'] ?: '';
//If the session is defined for Shop Cart.
if (isset($_SESSION['carrito'])) {
//Get data from session.
$arreglo = $_SESSION['carrito'];
$encontro = false;
for ($i=0; $i<count($arreglo); $i++) {
//checking if product has already been added to the cart.
if ($arreglo[$i]['Id'] == $_GET['articulo']) {
$encontro = true;
}
}
//If find product is false, update de array session (Shop cart).
if ($encontro == false) {
//Reset
$titulo = "";
$precio = 0;
$icon = "";
//Get data from DB.
$stmt = $c->prepare("SELECT titulo,precio,icon FROM tutoriales WHERE page=? and status=1");
$stmt->bind_param("i",$_GET['articulo']);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($titulo,$precio,$icon);
while ($stmt->fetch()) {
//New data for array.
$datosnuevos = ['Id' => $_GET['articulo'], 'Titulo' => $titulo, 'Precio' => $precio, 'Icon' => $icon, 'Cantidad' => 1 ];
//array_push($arreglo, $datosnuevos);
$arreglo[] = $datosnuevos;
$_SESSION['carrito'] = $arreglo;
//Count total of products added to the cart.
$data = $_SESSION['carrito'];
$value_carrito = count($data);
$_SESSION['compras'] = $value_carrito;
} $stmt->close();
} else {
$stmt->close();
}
}
} else { //If session is not defined for Shop Cart, that is to say that the card is in 0 productos.
//Reset.
$titulo = "";
$precio = 0;
$icon = "";
//Get data from DB.
$stmt = $c->prepare("SELECT titulo,precio,icon FROM tutoriales WHERE page=? and status=1");
$stmt->bind_param("i",$_GET['articulo']);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($titulo,$precio,$icon);
while ($stmt->fetch()) {
//Started the array for Shop Cart.
$arreglo[] = ['Id' => $_GET['articulo'], 'Titulo' => $titulo, 'Precio' => $precio, 'Icon' => $icon, 'Cantidad' => 1 ];
$_SESSION['carrito'] = $arreglo;
//Count total of products added to the cart.
$data = $_SESSION['carrito'];
$value_carrito = count($data);
$_SESSION['compras'] = $value_carrito;
} $stmt->close();
} else {
$stmt->close();
}
}
}
?>
这个小提琴将带您了解如何构造SELECT
查询的起点,
假设$_SESSION['carrito']
包含客户已经购买的产品,而$_SESSION['compras']
保留已购买产品的总数,则SELECT
查询将获取剩余产品,如下所示:
SELECT id_tutorial,page,titulo,info,icon,bg,vdo,url,precio,duracion,status,id_nivel,id_estado
FROM tutoriales
WHERE status = 1 AND id_tutorial NOT IN (
SELECT id_tutorial
FROM tutoriales
WHERE id_tutorial IN (" . rtrim(str_repeat("?,", $_SESSION['compras']), ",") . "
)
)
您的代码应如下所示:
// your code
$stmt = $c->prepare("SELECT id_tutorial,page,titulo,info,icon,bg,vdo,url,precio,duracion,status,id_nivel,id_estado FROM tutoriales WHERE status = 1 AND id_tutorial NOT IN (SELECT id_tutorial FROM tutoriales WHERE id_tutorial IN (" . rtrim(str_repeat("?,", $_SESSION['compras']), ",") . "))");
$param = array();
$paramType = '';
for($i = 0; $i < $_SESSION['compras']; ++$i){
switch(gettype($_SESSION['carrito'][$i]['Id'])){
case 'boolean':
case 'NULL':
case 'integer':
$paramType .= 'i';
break;
case 'double':
$paramType .= 'd';
break;
case 'string':
$paramType .= 's';
break;
default:
$paramType .= 'b';
}
$param[] = &$_SESSION['carrito'][$i]['Id'];
}
array_unshift($param, $paramType);
call_user_func_array(array($stmt, 'bind_param'), $param);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$result->bind_result($id_tutorial,$page,$titulo,$info,$icon,$bg,$vdo,$url,$precio,$duracion,$status,$id_nivel,$id_estado);
while ($result->fetch()){
// your code
}
} else {
$stmt->close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.