0

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

user3577419
  • 87
  • 1
  • 7
  • $_session doesn't contain those indexes....you havent set them, id say your if $_SESSION['carrito'] is true, but there is no data in it? – David Aug 21 '15 at 01:02
  • actually i see it $arreglo[]= array() means you access $arreglo[0][key] and so on – David Aug 21 '15 at 01:08

2 Answers2

1

You are setting the value of $_SESSION['carrito'] to an associative Array containing keys of Nombre, precio, etc.

You can then do 1 of 2 things with this.

  • a.) Loop over all of the keys to output the value
  • b.) Output the specific keys you want

It looks like you want option B.

So if you just drop the for loop you have, this should work.

<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>
scunliffe
  • 60,441
  • 24
  • 124
  • 159
1

As you did not specify what is your goal, choose the appropriate one.

Option 1

// from this
$arreglo[]= array('Id'=>$id, 'Nombre'=>$nombre, 'precio'=>$precio, 'imagen'=>$imagen, 'cantidad'=>1);
// to this
$arreglo= array('Id'=>$id, 'Nombre'=>$nombre, 'precio'=>$precio, 'imagen'=>$imagen, 'cantidad'=>1);

Or this:

// from this
$_SESSION['carrito']=$arreglo; 
// to this
$_SESSION['carrito']=$arreglo[0];

Option 2

If I understood what you are trying to do, change your while loop to:

while($fila=  mysql_fetch_array($res)){
    $nombre=$fila['nombre'];
    $precio=$fila['precio'];
    $imagen = $fila['imagen'];
    $arreglo[]= array('Id'=>$id, 'Nombre'=>$nombre, 'precio'=>$precio, 'imagen'=>$imagen, 'cantidad'=>1);
}

And your for loop to:

<?php for($i=0; $i<count($_SESSION['carrito']); $i++){ ?>
        <tr>
            <td><?php echo $_SESSION['carrito'][$i]['Nombre'];?></td>
            <td><?php echo $_SESSION['carrito'][$i]['precio'];?></td>
            <td><img src=" <?php echo $_SESSION['carrito'][$i]['imagen'];?></td>
            <td><?php echo $_SESSION['carrito'][$i]['cantidad'];?></td>
            <td><?php echo $_SESSION['carrito'][$i]['precio']*$_SESSION['carrito'][$i]['cantidad'];?></td>
        </tr>
Berriel
  • 11,226
  • 4
  • 37
  • 59