To show related product, I chose the value by GET method, in this case the value is the id of the product.
It obtained the data through a query, in this case the example is the colors that the product has.
$sql = "SELECT * FROM colors WHERE product_id='$id'";
Now in the shopping cart I want to show a section that indicates you might also be interested in these products.
Suppose in the database named products you have 6 products.
id | producto
------ | ------
1 | Cell
2 | Cell
3 | Cell
4 | Cell
5 | Cell
6 | Cell
And in the shopping cart I add the products with the id 1,3,6
As I show in the section you could also be interested in these products except the products already added to the basket.
I have only tested with a product like the example already mentioned.
In this case, how would it be considered or how would the query be.
To show only the products not added to the shopping cart.
This is my shopping cart card.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 producto 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 products 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 products 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>";
}
}
Update additional details.
The product table.
id_product Producto Marca subcategoría
------------- ---------------- ---------- ------
1 Mini Parlantes 0 1
2 flash Memory 0 1
The category table.
id cat_name categoría
---- ---------- -----------
1 HP 0
2 accesorios 1
3 Samsung 0
Structure:
<!-- language: lang-none -->
CREATE TABLE IF NOT EXISTS categories (
id int(100) NOT NULL,
cat_name text NOT NULL,
categoria int(5) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
- hp
- Accessories
- Samsung
- Accessories
In the HP category we show all HP brand products including accessories among others.
In the accessories subcategory we only show these products: flash memory, speakers, headphones, etc.
Note: Accessories are shown to the corresponding brand.
Now it is a product that does not belong to accessories, for example, a laptop subcategory instead of value 1 is NULL, this product would only be shown in the main category HP.
In the event that there was another subcategory called lapto, this product would be shown in that subcategory.