Jump to content

Fatal error: [] operator not supported for strings - pls help


fenix

Recommended Posts

Hello

I 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 loop

for($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 server
like tripod (PHP Version 4.3.2) I get the following error :    Fatal error: [] operator not supported for strings
reffering 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 well

I hope I was explicit enough and I really hope you could help me cause this is dirving me crazy.

Thank you.
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]


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.