I am creating a shopping cart system, which works very well, now the problem comes where it shows the results of the cart ( cesta.php
), I want to show results regardless of what the user has inserted in the cart, to make it easier if they want to buy more products.
Let's say if we add 3 products to buy, with id1
, id2
, id3
.
And in the Database ( productos
) there are 6 records.
Well, it should show id4
, id5
, id6
, since as I mentioned before, the user has already entered to buy the id1
, id2
, id3
.
I save the shopping cart in a session as follows:
I create the shopping cart on the pagecard.php
//Carro de la compra
//Si esta definida la ID obtenido por URL
if (isset($_GET['articulo'])) {
$id_tutorial = $_GET['articulo'];//Obtenemos el ID del tutorial añadido, para poder acer comprobaciones a mostrar otros resultados.
//Si esta definido la sesion carro -> es decir si ay algun articulo comprado
if (isset($_SESSION['carrito'])) {
$arreglo = $_SESSION['carrito'];
$encontro = false;
for ($i=0; $i<count($arreglo); $i++) {
if ($arreglo[$i]['Id'] == $_GET['articulo']) {
$encontro = true;
}
}
if ($encontro == false) {
$titulo = "";
$precio = 0;
$precioUSD = 0;
$icon = "";
$stmt = $c->prepare("SELECT titulo,precio,icon,id_autor 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,$id_autor);
while ($stmt->fetch()) {
//Sentencia prepare -> autor proyecto
$stmtN = $c->prepare("SELECT autor FROM autor WHERE id_autor=?");
$stmtN->bind_param("i", $id_autor);
$stmtN->execute();
$stmtN->bind_result($autor);
$stmtN->fetch();
$stmtN->close();
$datosnuevos = array('Id' => $_GET['articulo'], 'Titulo' => $titulo, 'Precio' => $precio, 'Icon' => $icon, 'Cantidad' => 1 );
/*
#Si se utiliza array_push() para añadir un solo elemento al array, es mejor utilizar $array[] = ya que de esta forma no existe la sobrecarga de llamar a una función.
*/
//array_push($arreglo, $datosnuevos);
$arreglo[] = $datosnuevos;
$_SESSION['carrito'] = $arreglo;
$data = $_SESSION['carrito'];
$value_carrito = count($data);
$_SESSION['compras'] = $value_carrito;
} $stmt->close();
} else {
$stmt->close();
}
}
} else { //Caso falso añadimos primer articulo al carro
$titulo = "";
$precio = 0;
$precioUSD = 0;
$icon = "";
$stmt = $c->prepare("SELECT titulo,precio,icon,id_autor 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,$id_autor);
while ($stmt->fetch()) {
//Sentencia prepare -> autor proyecto
$stmtN = $c->prepare("SELECT autor FROM autor WHERE id_autor=?");
$stmtN->bind_param("i", $id_autor);
$stmtN->execute();
$stmtN->bind_result($autor);
$stmtN->fetch();
$stmtN->close();
} $stmt->close();
} else {
$stmt->close();
}
$arreglo[] = array('Id' => $_GET['articulo'], 'Titulo' => $titulo, 'Precio' => $precio, 'Icon' => $icon, 'Cantidad' => 1 );
$_SESSION['carrito'] = $arreglo;
$data = $_SESSION['carrito'];
$value_carrito = count($data);
$_SESSION['compras'] = $value_carrito;
//echo "<script>window.location.reload();</script>";
}
}
I am going to answer my question myself, since they helped me in
Stack Overflow
.This example will give a head start on how to build the query
SELECT
:Suppose
$_SESSION['carrito']
it contains the products that have already been purchased by the customer and$_SESSION[ 'compras']
keeps the count of the total number of products purchased, the querySELECT
to retrieve the remaining products would be like this:And the code should be like this:
Deploy the example to my server and it works fine. I also leave you the entry of
Stack Overflow
, where they answered me: