i'm trying to make a shopping cart on PHP, but it just doesn't work, the page won't show a thing, i've reviewed my code and even redone it but it simply won't work... if someone can help me that would be nice, i'll try to explain my logic the best way i can.
<?php
session_start(); // I use sessions!
include('librerias/conexion.php'); // This calls the DB connection which is ok!
if (isset($_SESSION['carrito'])){ //If the shopping cart is set...
$arreglo=$_SESSION['carrito']; //an array will be set on the session variable
$encontro=false; //this is a flag to see if the added item is already on the cart
$numero = 0; // This will be used for index on the array
for($i=0; $i<count($arreglo); $i++){ // This loop searchs the array to see if there's a matching ID, if there is, then the quantity goes up by one
if($arreglo[$i]['Id']==$_GET['id']){
$numero=$i;
$encontro=true;
}
if($encontro){
$arreglo[$numero]['cantidad']+=1;
}
}
}else{ //If there's no session shopping cart...
if(isset($_GET['id'])){ //it will check if it comes with an id from an item
$nombre="";
$precio="";
$id = $_GET['id'];
$sql="SELECT * FROM productos WHERE id=$id"; // This is my query to get data from that item
print $sql;
$res= mysql_query($sql); //Query...
print $res;
while($fila= mysql_fetch_array($res)){ //This will set the name of the item, the price, and the image
$nombre=$fila['nombre'];
$precio=$fila['precio'];
$imagen = $fila['imagen'];
}
$arreglo[]= array('Id'=>$id, //when all that is set, this array is created with all those values
'Nombre'=>$nombre,
'precio'=>$precio,
'imagen'=>$imagen,
'cantidad'=>1);
$_SESSION['carrito']=$arreglo; // Finally the Session shopping cart s set to be the array
}
}
?>
<?php
include("librerias/header.php");
?>
<?php if(isset($_SESSION['carrito'])){ //Now it checks if it's set again and creates a table
$total=0;
?>
<table class="table"><th>Nombre</th><th>Precio</th><th>Imagen</th><th>Cantidad</th><th>Subtotal</th></table>
<?php
for($i=0; $i<count($_SESSION['carrito']); $i++){
?>
<tr>
<td><?php echo $_SESSION['carrito']['Nombre'];?></td>
<td><?php echo $_SESSION['carrito']['precio'];?></td>
<td><img src=" <?php echo $_SESSION['carrito']['imagen'];?>"</td>
<td><?php echo $_SESSION['carrito']['cantidad'];?></td>
<td><?php echo $_SESSION['carrito']['precio']*$_SESSION['carrito']['cantidad'];?></td>
</tr>
<?php
}
}else{
echo "<center>El carrito está vacío</center>";
}
?>
<?php
include("librerias/footer.php");
?>
Done..
But my results are:
Notice: Undefined index: Nombre in C:\xampp\htdocs\TiendaJavier\carrito.php on line 52
Notice: Undefined index: precio in C:\xampp\htdocs\TiendaJavier\carrito.php on line 53
Notice: Undefined index: cantidad in C:\xampp\htdocs\TiendaJavier\carrito.php on line 55
Notice: Undefined index: precio in C:\xampp\htdocs\TiendaJavier\carrito.php on line 56
Notice: Undefined index: cantidad in C:\xampp\htdocs\TiendaJavier\carrito.php on line 56
And i really can't work what's wrong... thanks in advance