-1

I understand there are many posts on this issue. I have looked at the many many posts but for some reason I can seem to find an answer!

Any help will be very much appreciated. I am fairly new to PHP so I apologise if I say anything incorrectly.

I am trying to create a basic basket system using an array. I keep getting an error undefined index on line $_SESSION['cart'][$app_ID]++; The funny thing is it all functions correctly! I want to solve the error and not just turn off the error reporting.

    if(isset($_GET['id'])){


$app_ID = $_GET['id'];   //the item id from the URL 
$action = $_GET['action']; //the action from the URL 


$total = 0;


if (isset($app_ID)){

switch($action) {       

    case "add":
        $_SESSION['cart'][$app_ID]++;
    break;

    case "remove":
        $_SESSION['cart'][$app_ID]--; 
        if($_SESSION['cart'][$app_ID] == 0) unset($_SESSION['cart'][$app_ID]); 
    break;

    case "empty":
        unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
    break;

Thanks guys and gals.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
  • You already know how to use `isset`. Just do the same for the `$_SESSION['cart'][$app_ID]` incrementation. Assign `=0` if undefined, use `++` otherwise. – mario Mar 28 '13 at 21:07
  • Oh yes I see! Thanks Mario. Wow what a response from everyone. Thank you all! Edit: Sorry I was unaware that post. My apologises for the duplicate. – Lewis Taylor Mar 28 '13 at 21:47

4 Answers4

0

to use $_SESSION you must call session_start () first before send any header information

I hope this help you,

Cheers,

Smartoop
  • 715
  • 6
  • 13
0
session_start(); // at the top

case "add":
    if (isset( $_SESSION['cart'][$app_ID] )){
        $_SESSION['cart'][$app_ID]++;
    } else {
        $_SESSION['cart'][$app_ID] = 1;
    }
break;
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
0

It's worth to mention, that it's merely a Notice, not an error. You basically must check for array index existence and initialize it before referencing it.

if (isset($app_ID)) {
    switch($action) {       

        case "add":
            if (!isset($_SESSION['cart']) {
                $_SESSION['cart'] = array();
            }

            if (!isset($_SESSION['cart'][$app_ID]) {
                $_SESSION['cart'][$app_ID] = 0;
            }

            $_SESSION['cart'][$app_ID]++;
        break;

        case "remove":
            if (isset($_SESSION['cart'] && isset($_SESSION['cart'][$app_ID]) {
                $_SESSION['cart'][$app_ID]--; 
                if ($_SESSION['cart'][$app_ID] <= 0) {
                   unset($_SESSION['cart'][$app_ID]);
                }
            }
        break;

        case "empty":
            unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
        break;
    }
}

I've also changed == 0 in remove to <= 0 just to be safe.

Furgas
  • 2,729
  • 1
  • 18
  • 27
0

You should use isset($_SESSION['cart'][$app_ID]) and maybe isset( $_SESSION['cart']) before, everywhere.

Generally you must make sure that and array index is present before referencing it. You can do this either with isset(), or writing code where that condition is inevitable (e.g. adding the index somewhere earlier).

The other part of your question, I suppose, is why your code works. The explanation is easy. When you reference a non-existent index, the notice you observed is emitted (in non-production environments) but that does not stop the program. Since there is nothing to be used, null is returned for that array value. So the value is assumed to be null, and ++ takes values, as integers, and null is converted to the integer 0, and then raised by one. Since ++ is an operator that writes, it will create the array item for you. Since $a++ is defined as $a=$a+1 it's easy to see that what you've written is $_SESSION['cart'][$app_ID]=$_SESSION['cart'][$app_ID]+1 which is in turn $_SESSION['cart'][$app_ID]=null+1 where null+1 is executed as 0+1 yielding 0, so 0 is assigned to (the formerly missing) array item. Hope this helps see clear. ;)

Levente Pánczél
  • 1,895
  • 2
  • 14
  • 16
  • I have manage to get it working. It was down to my lack of understanding of PHP and isset. Very informative response, I learnt a a lot. Thank you. – Lewis Taylor Mar 28 '13 at 21:49