-
The issue is the results from local host are not matching up with the live site when pushed by FTP. The
below code seems make me believe the array is not be created
properly on the server. In addition, it is doing some funky stuff, as seen below. The error is “Cannot use a scalar value as an array” on line 97, or $_SESSION[“count”][$i] +=1. Do you see the reason why? Or is there a different mistake I made? -
To summarize the code, the following session arrays make up the core of the shopping cart: $_SESSION[‘item_id’][$i], $_SESSION[‘size’][$i],
$_SESSION[‘price’][$i], $_SESSION[‘count’][$i] … and they all correlate to one another. Therefore a check is done to see if the combination exists, if so it appends the count, if not, adds the item combination for the first time.
Local Host
In Stock: 3
Distinct Item Count: 3
Array ( [0] => 1 [1] => 2 [2] => 2 ) Item Array: 1
Array ( [0] => S [1] => M [2] => L ) Size Array: 1
Array ( [0] => 15 [1] => 20 [2] => 20 ) Price Array: 1
Array ( [0] => 15 [1] => 35 [2] => 17 ) Count Array: 1
Quantity Increased to 18
1
S
15
15
2
M
20
35
2
L
20
18
|3 ($1285)
Live Site – First Add
In Stock: 5
0
1
M
20
1
|1 ($20)
Live Site – Second Add
In Stock: 5
Distinct Item Count: 1
1Item Array: 1
MSize Array: 1
300Price Array: 1
1Count Array: 1
Warning: Cannot use a scalar value as an array in /public_html/shop/helper/addtocart.php on line 97
Quantity Increased To
1
M
2
|1 ($0)
Code
if (isset($_SESSION["item_id"]) && is_array($_SESSION["item_id"])){
echo "Distinct Item Count: " . count($_SESSION["item_id"]);
echo "<br/>";
echo "<br/>";
echo "Item Array: ";
print_r($_SESSION["item_id"]);
echo "<br/>";
echo "Size Array: ";
print_r($_SESSION["size"]);
echo "<br/>";
echo "Price Array: ";
print_r($_SESSION["price"]);
echo "<br/>";
echo "Count Array: ";
print_r($_SESSION["count"]);
echo "<br/>";
echo "<br/>";
}
//check for current product combination in visitor's shopping cart content
//find count in cart
if (!isset($_SESSION["item_id"])){
$count = 0;
}else{
$count = count($_SESSION["item_id"]);
}
echo $count;
echo "<br/>";
if ($count == 0){
//declare arrays
$_SESSION["item_id"] = array();
$_SESSION["size"] = array();
$_SESSION["price"] = array();
$_SESSION["count"] = array();
$_SESSION["total"] = array();
//add first item to cart
$_SESSION["item_id"][] = $item_id;
$_SESSION["size"][] = $size;
$_SESSION["price"][] = $price;
//fundraiser and corporate
if (($_SESSION["acctype"] == 2 || $_SESSION["acctype"] == 3) && isset($_SESSION['userid'])){
//update count, add 100
$_SESSION["count"][] = 100;
}
else{
$_SESSION["count"][] = 1;
}
}else{
$flag=0;
$i=0;
while ($i <= $count){
if( ($_SESSION["item_id"][$i] == $item_id) && ($_SESSION["size"][$i] == $size) ){
//fundraiser and corporate
if(!isset($_SESSION["acctype"]) || $_SESSION["acctype"] == 1){
//update by one
$_SESSION["count"][$i] +=1;
}
elseif(isset($_SESSION['userid']) && ($_SESSION["acctype"] == 2 || $_SESSION["acctype"] == 3)){
//update count, add 100
$_SESSION["count"][$i] +=100;
}
else{
echo "Hmm";
}
//update cart stats
echo notify('Quantity Increased To' . ' ' . $_SESSION["count"][$i]);
//was there combination match?
$flag = 1;
}
$i++;
}
if($flag == 0){
$_SESSION["item_id"][] = $item_id;
$_SESSION["size"][] = $size;
$_SESSION["price"][] = $price;
//fundraiser and corporate
if (($_SESSION["acctype"] == 2 || $_SESSION["acctype"] == 3) && isset($_SESSION['userid'])){
//update count, add 100
$_SESSION["count"][] = 100;
}else{
$_SESSION["count"][] = 1;
}
}
}
Short answer: Check to see if register_globals is enabled on your live server using
var_dump(ini_get('register_globals'))orphpinfo(). If register_globals is enabled, the preferable solution is to disable register globals—it leads to security problems and as of PHP 5.3 is deprecated.Longer answer: ‘Cannot use a scalar value as an array’ means that a scalar value (e.g. number) exists in the variable that you are referencing as an array. I do not see anywhere that you assign a scalar directly to
$_SESSION["count"].However, if register_globals is enabled
$countwould become a reference to$_SESSION["count"], which means$count = 0would be setting$_SESSION["count"]to a scalar. Again, the preferable solution would be to disable register_globals, but a workaround would be to rename the$countvariable to something else.