justinh Posted January 22, 2009 Share Posted January 22, 2009 <?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 More sharing options...
gevans Posted January 22, 2009 Share Posted January 22, 2009 $_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; Link to comment https://forums.phpfreaks.com/topic/141974-could-someone-please-help/#findComment-743377 Share on other sites More sharing options...
justinh Posted January 22, 2009 Author Share Posted January 22, 2009 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!"; } ?> Link to comment https://forums.phpfreaks.com/topic/141974-could-someone-please-help/#findComment-743383 Share on other sites More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 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. Link to comment https://forums.phpfreaks.com/topic/141974-could-someone-please-help/#findComment-743397 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.