Jump to content

Could someone please help


justinh

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.