Jump to content

Archived

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

Thingee

*SOLVED* Can't Use Scalar Value As Array

Recommended Posts

I'm writing a simple cart script right now and for some reason when I moved it to another server I got these errors when I try to add an item to the cart...

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: Cannot use a scalar value as an array in /home/joeystec/public_html/test/php/lib/cart.inc.php on line 42

Warning: Cannot use a scalar value as an array in /home/joeystec/public_html/test/php/lib/cart.inc.php on line 43

Warning: Cannot use a scalar value as an array in /home/joeystec/public_html/test/php/lib/cart.inc.php on line 51

Warning: Cannot use a scalar value as an array in /home/joeystec/public_html/test/php/lib/cart.inc.php on line 53

Warning: Cannot use a scalar value as an array in /home/joeystec/public_html/test/php/lib/cart.inc.php on line 54

Warning: Cannot use a scalar value as an array in /home/joeystec/public_html/test/php/lib/cart.inc.php on line 55

Warning: Cannot use a scalar value as an array in /home/joeystec/public_html/test/php/lib/cart.inc.php on line 56

Warning: Cannot use a scalar value as an array in /home/joeystec/public_html/test/php/lib/cart.inc.php on line 57[/quote]

The code that adds the stuff onto the cart is the following...
(Note: Lines 42-57 are the lines like $_SESSION["cart"][$id][X])

[code]
// set cookie and store value in session
$this->setstp();
db_connect();
        $sel_products = mysql_query("SELECT special_pick, special, price, artist, album FROM products WHERE id=".$id."");
        $item = mysql_fetch_array($sel_products);
        // returns the number of rows in a result, if 1 item exists if 0 item doesn't exists.
        $num_rows = mysql_num_rows($sel_products);

        // if item exists then add item to cart
        if ($num_rows >= 1)
        {

            session_regenerate_id(TRUE);
    
            $_SESSION["cart"][$id][0] = $id;
            $_SESSION["cart"][$id][1] = $quantity;
            $_SESSION["cart"][$id][2] = $item['$special_pick] != 0 ? 'y' : 'n';
            $_SESSION["cart"][$id][3] = $item['special_pick'];
            $_SESSION["cart"][$id][4] = $item['special'] == '0.00' ? '0.00' : $item['special'];
            $_SESSION["cart"][$id][5] = $item['price'];
            $_SESSION["cart"][$id][6] = $item['artist'];
            $_SESSION["cart"][$id][7] = $item['album'];
        header ("location:".$_SERVER['HTTP_REFERER']);
            
        }
[/code]

I don't understand how this code works on one server and not the other. So right now it currently does not add anything on the cart. Any ideas?

Share this post


Link to post
Share on other sites
hi

not 100% sure on the reasons behind your error without checking, but the reason why you get it on one server and not the other is normally down to how strict the error_reporting is in your php.ini file.
i know that doesn't help your problem, but at least you know why one has trouble and the other doesn't...

Share this post


Link to post
Share on other sites
[!--quoteo(post=360666:date=Apr 1 2006, 10:53 AM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 1 2006, 10:53 AM) [snapback]360666[/snapback][/div][div class=\'quotemain\'][!--quotec--]
not 100% sure on the reasons behind your error without checking, but the reason why you get it on one server and not the other is normally down to how strict the error_reporting is in your php.ini file.
[/quote]

True. The server not having problems is a slackware box that I'm running at my house. The one having trouble is an actual hosted server with some company. Is there any information you would need to know in order to really know why I'm having this error?

Share this post


Link to post
Share on other sites
Not really. It simply appears that on your own machine you have error reporting switched off in your php.ini file.

If you go and switch it on and DON'T get the same errors then something is a miss.

Also check you are running the same versions of apache, php and mysql as the 'live' server.

Now to your problem....

How is $id defined prior to these lines of code? I refer to the value of $id used in the mysql query.

Also looking at the relevant lines....

$_SESSION['cart'][$id][0] = $id;.....
.....
$_SESSION['cart'][$id][7] = $foo;

since this is a session variable when you add another item to the cart $id is actually an array of 8 elements (0-8)...

So the next page you use $id on it will take the array $id from the session array['cart'].

the reason fro this.....

I suspect you have register globals on your own computer set to a different value than it is on the server. (perhaps you shoudl clone the php.ini settings of the server too!!!!) If globals is on the $id will be 'assumed' teh $id from the session var. if globasl if off then you must refer to the session['cart'][$id] directly.

Share this post


Link to post
Share on other sites
The code works on localserver. Items get added correctly without any errors. I do have all the error reports turned on too on my localserver. Now the other server is on a certain hosting company so I can't make changes to their php.ini file =/.

Sorry about the little info I gave.

The file that adds items to the cart and uses the function above is manage.php. So the url looks like this... manage.php?act=add&pid=X. Manage.php looks like this...

[code]
<?php
if (isset($_GET["act"]))
{
    if ($_GET["act"] == "add") // add item
    {
        //unserialize($_SESSION["cart"]);
        if (!isset($_SESSION["cart"]))
        {
            // add first item
            $cart->add_item_to_cart($_GET["pid"],1);
        }
        else if (array_key_exists($_GET["pid"], $_SESSION["cart"]))
        {
            // add 1 to quantity if item in cart already
            $cart->add_item_to_cart($_GET["pid"],++$_SESSION["cart"][$_GET["pid"]][1]);
        }
        else
        {
            // add any other items after first item
            $cart->add_item_to_cart($_GET["pid"],1);
        }
    }
    elseif ($_GET["act"] == "del") // delete item
    {
        $cart->del_item($_GET["pid"]);    
    }
}
?>
[/code]

Here is the full function for adding an item to the cart...

[code]
<?php
function add_item_to_cart($id, $quantity)
    {
        // set cookie and store value in session
        $this->setstp();    

        db_connect();
        $sel_products = mysql_query("SELECT special_pick, special, price, artist, album FROM products WHERE id=".$id."");
        $item = mysql_fetch_array($sel_products);
        // returns the number of rows in a result, if 1 item exists if 0 item doesn't exists.
        $num_rows = mysql_num_rows($sel_products);

        // if item exists then add item to cart
        if ($num_rows >= 1)
        {

            session_regenerate_id(TRUE);
    
            $_SESSION["cart"][$id][0] = $id;
            $_SESSION["cart"][$id][1] = $quantity;
            
            $_SESSION["cart"][$id][2] = $item['$special_pick] != 0 ? 'y' : 'n';
            $_SESSION["cart"][$id][3] = $item['special_pick'];
            $_SESSION["cart"][$id][4] = $item['special'] == '0.00' ? '0.00' : $item['special'];
            $_SESSION["cart"][$id][5] = $item['price'];
            $_SESSION["cart"][$id][6] = $item['artist'];
            $_SESSION["cart"][$id][7] = $item['album'];
        header ("location:".$_SERVER['HTTP_REFERER']);
            
        }
    }
?>
[/code]

Thanks for helping me :).

Share this post


Link to post
Share on other sites
Alright I got it figured out. I don't know why but when I first wrote the code I used the following...

[code]
if(!isset($_SESSION["cart"])){
$_SESSION["CART"] = NULL;
}
[/code]

I just changed null to array(); and that fixed the problem :). Thanks for everyone's help!

Share this post


Link to post
Share on other sites

×

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.