Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working on a college exercise about a shopping cart, I converted the POST values into an array, however when I echo these array values I get the following message:

Notice: Undefined index: item in line 40
Notice: Undefined index: quantity in line 40

Here's line 40:

echo "1. ".$_SESSION['lista']['item']." ".$_SESSION['lista']['quantity']." unidades".", ".$_SESSION['lista']['price']." CRC.";

Here's the full script:

    <?php

session_start();

//Obtengo la lista
$lista[]= $_SESSION['lista'];

//Guardo un valor en la lista
/*
$articulo= $_POST['articulo'];
$cantidad= $_POST['cantidad'];
$codigo= $_POST['codigo'];
*/

//Listado
$articulos = array(

  'Papaya' => 500, 'Banano' => 50, 'Mango' => 150, 
  'Leche' => 500, 'Cafe' => 1200, 'Mantequilla' => 300,
  'Pan' => 450, 'Jugo' => 780, 'Mani' => 800,
  'Yogurt' => 450, 'Cerveza' => 550, 'Vino' => 2500,
  );

$_SESSION['lista'] = array(
'item' => ($_POST['articulo']), 
'quantity' => ($_POST['cantidad']),
'code' => ($_POST['codigo']),
);

//precio
$precio = $articulos[($_SESSION['lista']['item'])] * $_SESSION['lista']['quantity'];

$_SESSION['lista'] = array('price' => $precio,);


//listado
echo  "<b>LISTADO DE COMPRA</b></br>";


echo "1. ".$_SESSION['lista']['item']." ".$_SESSION['lista']['quantity']." unidades".", ".$_SESSION['lista']['price']." CRC.";

/*foreach($_SESSION['lista'] as $key => $item) {
  echo $key, '. ', $item['item'], ' ', $item['quantity'], ' units';
}
*/

//Regreso a la lista
$_SESSION['lista'] = $lista;



var_dump($_SESSION); $lista;

echo "</br> <a href='index.html'>Volver al indice</a>";

//SESSION_DESTROY = Elimina todo

//Imprimo lo que hay en session
var_dump($_SESSION);

?>
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

That means that the $_SESSION["lista"] array does not have an element for "item" or "quantity".

To see the full contents of $_SESSION you can use print_r():

print_r($_SESSION);

Note that in HTML this will not look nice, you'll need to use "View Source" on your browser to see it in a good form.

share|improve this answer
    
You were right, the item is not on the array, nor quantity from what I can tell. I don't understand what I'm doing wrong! –  Gabriel Meono Apr 29 '11 at 21:10
add comment

What your doing wrong is outputting blank content before you call session_start, see the first line that contains <?php, You will notice that there are several spaces before the tag.

    <?php
^^^^

you need to remove them for the session to start correctly!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.