fenix Posted November 14, 2006 Share Posted November 14, 2006 HelloI have a problem with a shopping cart . The following script receives data from the product page and adds it to the basket by putting it in an array.[code]<?session_start();if(isset($_POST['add'])){$_SESSION['prod_id'][] =$_POST['prod_id'];$_SESSION['author'][] =$_POST['autor'];$_SESSION['title'][] =$_POST['title'];$_SESSION['price'][] =$_POST['price'];}// each time a product is added , the values for the product are stored in an array and printed with a for loopfor($i=0; $i<count($_SESSION['prod_id']); $i++){echo $_SESSION['author'][$i];echo $_SESSION['title'][$i];echo $_SESSION['price'][$i];}?>[/code]This works just fine on localhost (PHP Version 5.1.2) on my comp but when i upload it in an outside serverlike tripod (PHP Version 4.3.2) I get the following error : Fatal error: [] operator not supported for stringsreffering to the line where i store the first variable into the array - $_SESSION['prod_id'][] =$_POST['prod_id']; This error occurs when i add a second product to the basket .I've search on google and I understand that the problem is that when i load the page again the variable has some string value instead of being empty. I've put this code at the beggining of the script[code]if ( !is_array($_SESSION['prod_id'] ) ) { unset($_SESSION['prod_id']); }if ( !is_array($_SESSION['author'] ) ) { unset($_SESSION['autor'] ); }if ( !is_array($_SESSION['title'] ) ) { unset($_SESSION['title']); }if ( !is_array($_SESSION['price'] ) ) { unset($_SESSION['price'] ); }[/code]and some strange things happend. The script seems to run corectly for some time (while I add products , delete them , destroy the session etc) and then somehow it crashes - some of the variables are not printed anymore in the for loop.I have done some testing and the results are the following :- author and title crash after a while- strangely price and prod_id seem to work wellI hope I was explicit enough and I really hope you could help me cause this is dirving me crazy. Thank you. Link to comment https://forums.phpfreaks.com/topic/27199-fatal-error-operator-not-supported-for-strings-pls-help/ Share on other sites More sharing options...
printf Posted November 14, 2006 Share Posted November 14, 2006 You should be declaring the array() value for the key names, if they are not set or are not an array. Also if you allow deleting of products, then use foreach ($_SESSION['prod_id'] AS $k => $v ), so if products are removed you don't need to worry about the (int) key order.Maybe something like this will work...Instead of this...[code]if(isset($_POST['add'])){$_SESSION['prod_id'][] =$_POST['prod_id'];$_SESSION['author'][] =$_POST['autor'];$_SESSION['title'][] =$_POST['title'];$_SESSION['price'][] =$_POST['price'];}[/code]Something like this...[code]if( isset ( $_POST['add'] ) ){ if ( ! isset ( $_SESSION['prod_id'] ) || ! is_array ( $_SESSION['prod_id'] ) ) { $_SESSION['prod_id'] = array (); } $_SESSION['prod_id'][] = $_POST['prod_id']; if ( ! isset ( $_SESSION['author'] ) || ! is_array ( $_SESSION['author'] ) ) { $_SESSION['author'] = array (); } $_SESSION['author'][] = $_POST['author']; if ( ! isset ( $_SESSION['title'] ) || ! is_array ( $_SESSION['title'] ) ) { $_SESSION['title'] = array (); } $_SESSION['title'][] = $_POST['title']; if ( ! isset ( $_SESSION['price'] ) || ! is_array ( $_SESSION['price'] ) ) { $_SESSION['price'] = array (); } $_SESSION['price'][] = $_POST['price'];}[/code] Link to comment https://forums.phpfreaks.com/topic/27199-fatal-error-operator-not-supported-for-strings-pls-help/#findComment-124395 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.