Reputation: 180
I want to show indifferent result that the user have added in the shopping cart.
I get the ID
from the product by method GET
, by click a url.
<a href=http:./cart/$id/$url>
My example
The products
table have only 6 records, that is to say 6 products to buy.
Now, if the user add 3 products to buy, with id5
id1
id3
now my question is, how can i show others products from table products
, that have not been purchased by user, in this case id2
id4
id6
, as I mentioned before id5
id1
id3
is purchased.
The query that i will generated for the indifferent results of the shopping cart is like this:
$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");
I´m spending a logic !=
or <>
But I don´t know the process must take before working with this query result.
Here is my Shopping cart code 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();
}
}
}
?>
Upvotes: 0
Views: 690
Reputation: 16963
This fiddle will give you a head start on how to construct your SELECT
query,
Suppose $_SESSION['carrito']
contains the products which have already been purchased by the customer and $_SESSION['compras']
keeps the count of total number of products purchased, the SELECT
query to fetch the remaining products would be like this:
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']), ",") . "
)
)
And your code should be like this:
// 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();
}
Upvotes: 1