Jump to content

Recommended Posts

<?php
session_start();



$item_to_add = $_GET['item'];


if ( ! isset ( $_SESSION['items'] ) )
{

   $_SESSION['items'] = array ( $_GET['item'], 1);
   
} else {

if ( ! in_array ( $_GET['item'], $_SESSION['items'] ) )
     {
     $_SESSION['items'][] = $_GET['item'];
     $_SESSION['items'][$_GET['item']][] = 1;

     } else {
     $_SESSION['items']['$item_to_add'][] = $_SESSION['items']['$item_to_add'][] + 1;
     }
     
     
}

if ( isset ( $_SESSION['items'] ) )

{

foreach ( $_SESSION['items'] AS $item){

      foreach( $_SESSION['items'][$item][] AS $qty){
      
        echo $item . "x". $qty;
         }
         
         }
         

} else {


echo "No Items exist!";

     
}

?>

 

 

Could someone please help, I keep getting an error of:

Fatal error: Cannot use [] for reading in C:\xampp\htdocs\hrfinal\inc.additem.php on line 20

Link to comment
https://forums.phpfreaks.com/topic/141974-could-someone-please-help/
Share on other sites

$_SESSION['items']['$item_to_add'][] = $_SESSION['items']['$item_to_add'][] + 1;

 

You cannot assign a variable with no reference;

 

$_SESSION['items']['$item_to_add'][] + 1;

 

this needs to be something along the lines of;

 

$_SESSION['items']['$item_to_add'][0] + 1;

Okay sweet, that error is fixed, now I'm getting

 

 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\hrfinal\inc.additem.php on line 32

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\hrfinal\inc.additem.php on line 32

 

Really don't understand how I would go about displaying each item and its qty.

 

heres the updated code

 


<?php
session_start();

$item_to_add = $_GET['item'];


if ( ! isset ( $_SESSION['items'] ) )
{

   $_SESSION['items'] = array ( $_GET['item'], 1);
   
} else {

if ( ! in_array ( $_GET['item'], $_SESSION['items'] ) )
     {
     $_SESSION['items'][] = $_GET['item'];
     $_SESSION['items'][$_GET['item']][] = 1;

     } else {
     $_SESSION['items']['$item_to_add'][1] = $_SESSION['items']['$item_to_add'][1] + 1;
     }
     
     
}

if ( isset ( $_SESSION['items'] ) )

{

foreach ( $_SESSION['items'] AS $item){

      foreach( $_SESSION['items'][$item][1] AS $qty){
      
        echo $item . "x". $qty;
         }
         
         }
         

} else {


echo "No Items exist!";

     
}

?>


if ( isset ( $_SESSION['items'] ) && is_array($_SESSION['items']))

 

Obviously the $_SESSION['items'] is not an array. So something is messing up somewhere when you are trying to define it as an array. Maybe you are using the old session. close your browser and open a new one and try again.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.