Jump to content


Photo

*SOLVED* Can't Use Scalar Value As Array


  • Please log in to reply
5 replies to this topic

#1 Thingee

Thingee
  • Members
  • Pip
  • Newbie
  • 4 posts
  • LocationCalifornia

Posted 01 April 2006 - 05:41 PM

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])

// 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']);
            
        }

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?

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 01 April 2006 - 06:53 PM

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...
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 Thingee

Thingee
  • Members
  • Pip
  • Newbie
  • 4 posts
  • LocationCalifornia

Posted 01 April 2006 - 10:18 PM

[!--quoteo(post=360666:date=Apr 1 2006, 10:53 AM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 1 2006, 10:53 AM) View Post[/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?

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 01 April 2006 - 10:51 PM

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.

follow me on twitter @PHPsycho

#5 Thingee

Thingee
  • Members
  • Pip
  • Newbie
  • 4 posts
  • LocationCalifornia

Posted 02 April 2006 - 12:59 AM

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

<?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"]);    
    }
}
?>

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

<?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']);
            
        }
    }
?>

Thanks for helping me :).

#6 Thingee

Thingee
  • Members
  • Pip
  • Newbie
  • 4 posts
  • LocationCalifornia

Posted 02 April 2006 - 03:58 PM

Alright I got it figured out. I don't know why but when I first wrote the code I used the following...

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

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users