Jump to content

[SOLVED] Cannot use a scalar value as an array ?


robgood

Recommended Posts

hi everyone, fairly new to php and i am trying to complete a shopping basket on my site.  I have got it working locally but as soon as i upload it to the remote version it stops working.

 

here is the error i get:

 

Warning: Cannot use a scalar value as an array in /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php on line 24

 

Warning: Cannot use a scalar value as an array in /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php on line 24

 

Warning: Cannot modify header information - headers already sent by (output started at /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php:24) in /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php on line 26

 

and here is the code for the basket page ( i have removed some of irrelevant code):

 

<?php
session_start();
if (!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
if (!isset($_SESSION['addon']))
{
$_SESSION['addon'] = array();
}
if (isset($_POST['bid']))
{
$_SESSION['cart'][] = $_POST['bid'];
if (isset($_POST['addon']))
{
$addonarrays = $_POST['addon'];
}
else
{
$addonarrays = array();
}

foreach($addonarrays as $addonarray)
{
$_SESSION['addon'][] = $addonarray;
}
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
if (isset($_GET['empty']))
{
unset($_SESSION['cart']);
unset($_SESSION['addon']);
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Basket</title>

<meta name="description" content="basket"/>
<meta name="keywords" content="basket"/>
<link href="styles1.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery-jqir.js"></script>
<script type="text/javascript">
$(document).ready(
function()
{
	$(".jqir").jQIR("png", "pictures/");
}
);
</script>
</head>

<body>

		<h1>Your Basket</h1>
		<p>Your Basket Contains the following items:</p>

		<?php  
			require ('databaseconnect.inc');
			$productids = $_SESSION['cart'];

			foreach ($productids as $productid)
			{

				$products = @mysql_query("SELECT name, refnum, price FROM product, deliveryprice WHERE id='$productid' AND deliveryprice.productid='$productid'"); 

				while ($product = mysql_fetch_array($products))
				{
					$refnum = $product['refnum'];
					$name = $product['name'];
					$price = $product['price'];

					echo "<p>$refnum−$name−$price</p>";
				}


			}

			$addonids = $_SESSION['addon'];

			foreach ($addonids as $addonid)
			{

				$addons = @mysql_query("SELECT name, refnum, price FROM addon, addonprice WHERE id='$addonid' AND addonprice.addonid='$addonid'"); 

				while ($addon = mysql_fetch_array($addons))
				{
					$refnum = $addon['refnum'];
					$name = $addon['name'];
					$price = $addon['price'];

					echo "<p>$refnum−$name−$price</p>";
				}


			}

		?>

		<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?empty=1">Remove all items from your cart</a></p>	




</body>
</html>

 

many thanks for your help

 

Link to comment
Share on other sites

sorry, should be same as i haven't changed the top section

 

 

                    foreach($addonarrays as $addonarray)

this line 24 -> {

            $_SESSION['addon'][] = $addonarray;

            }

 

Link to comment
Share on other sites

here is form for addon: the value of all the variables that the form uses are intergers

 

<table width="98%">


			 <tr>
			 <?php 
				$addon = @mysql_query("SELECT DISTINCT id, refnum, name FROM addon, addonsubmenucat WHERE id=addonid AND catid=2 LIMIT 0, 4");
				while ($addons = mysql_fetch_array($addon))
				{
					$addonid = $addons['id'];
					$addonname = $addons['name'];
					$addonrefnum = $addons['refnum'];

					echo "<td align='center'><label>$addonname</label><input type='checkbox' name='addon[]' value='$addonid'></td>\n";

				}
			 ?> 
			 </tr>

			 <tr>
			 <?php 
				$addon = @mysql_query("SELECT DISTINCT id, refnum FROM addon, addonsubmenucat WHERE id=addonid AND catid=2 LIMIT 0, 4");
				while ($addons = mysql_fetch_array($addon))
				{
					$addonrefnum = $addons['refnum'];

					echo "<td align='center'><img src='pictures/$addonrefnum.jpg'></td>\n";
				}
			 ?> 
			</tr>
			<input type="hidden" name="bid" value="<?php echo "$id" ;?>" />						
		</form>
		</table>

Link to comment
Share on other sites

do not suppress errors with $addon, that is for a finalized script.

 

your array is is that you can't declare it and then make it an arrya

 

<?php
$var = "value";
$var[] = "value1"; //This will scalar error the scalar error is not on the value of the variable, but in the nature of it because its already declared in the same (or higher) block to be a linear variable.
?>

Link to comment
Share on other sites

yes i'm quoting out of you and i'll comment your flaws, its more of a logical error

<?php
if (isset($_POST['bid']))
{
$_SESSION['cart'][] = $_POST['bid'];
if (isset($_POST['addon']))
{
$addonarrays = $_POST['addon'];  //here what you think is an array you are actually declaring as a linear variable thus when you get to foreach it errors
//fix
$addonarrays[] = $_POST['addon']; //Its a single item array but it should fix it for you
}
else
{
$addonarrays = array();
}

Link to comment
Share on other sites

cool dude, made the change you suggested but still no joy, throws up similar error.  I can't understand why it works perfectly locally and not on remote server.  Could be something to do with the php config on remote?

Link to comment
Share on other sites

You could try checking you really do have an array (looks like it should be but worth a check)

 

<?php

var_dump($_SESSION['addon']);                                   // <-- add this

foreach($addonarrays as $addonarray)
{
$_SESSION['addon'][] = $addonarray;
}

Link to comment
Share on other sites

ok i added this:

 

 <?php
echo '<pre>';
var_dump($_POST);
echo '</pre>';
?> 

 

 

and got :

 

array(2) {

  ["addon"]=>

  array(3) {

    [0]=>

    string(1) "1"

    [1]=>

    string(1) "2"

    [2]=>

    string(1) "3"

  }

  ["bid"]=>

  string(2) "43"

}

 

i still can't understand why it would work perfectly on local and not on remote

Link to comment
Share on other sites

just throws up all this mess:

 

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php:1) in /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php on line 2

int(0)

Warning: Cannot use a scalar value as an array in /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php on line 28

 

Warning: Cannot use a scalar value as an array in /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php on line 28

 

Warning: Cannot use a scalar value as an array in /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php on line 28

 

Warning: Cannot use a scalar value as an array in /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php on line 28

 

Warning: Cannot modify header information - headers already sent by (output started at /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php:1) in /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php on line 30

Link to comment
Share on other sites

i put it where you indicated as below, is this right?

 

<?php
session_start();
if (!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
if (!isset($_SESSION['addon']))
{
$_SESSION['addon'] = array();
}
if (isset($_POST['bid']))
{
$_SESSION['cart'][] = $_POST['bid'];
if (isset($_POST['addon']))
{
$addonarrays = $_POST['addon'];
}
else
{
$addonarrays = array();
}


var_dump($_SESSION['addon']); 

foreach($addonarrays as $addonarray)
{
$_SESSION['addon'][] = $addonarray;
}
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
if (isset($_GET['empty']))
{
unset($_SESSION['cart']);
unset($_SESSION['addon']);
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
?>

Link to comment
Share on other sites

ok it gave me this:

 

array(4) { [0]=>  string(1) "1" [1]=>  string(1) "2" [2]=>  string(1) "3" [3]=>  string(1) "4" }

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\My Site\basket.php:23) in C:\wamp\www\My Site\basket.php on line 29

 

have just noticed that after i post the form, (it takes you to basket page and brings up the error above) that when i go to the basket page separately without posting the form, it has saved all the previous selections that i made.  So it seems to be working in terms of adding the information to the session????

 

Link to comment
Share on other sites

well i have added this, but still no joy?  :'(

 

 

<?php
session_start();
if (!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
if (!isset($_SESSION['addon']))
{
$_SESSION['addon'] = array();
}
if (isset($_POST['bid']))
{
$_SESSION['cart'][] = $_POST['bid'];

$addonarrays = array();

if (isset($_POST['addon']) && is_array($_POST['addon'])) 
{
      $addonarrays = $_POST['addon'];
}  

foreach($addonarrays as $addonarray)
{
$_SESSION['addon'][] = $addonarray;
}

header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
if (isset($_GET['empty']))
{
unset($_SESSION['cart']);
unset($_SESSION['addon']);
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
?>

 

get the following error:

 

Warning: Cannot use a scalar value as an array in /home/nas03l/d/mysite..co.uk/user/htdocs/basket.php on line 24

 

Warning: Cannot use a scalar value as an array in /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php on line 24

 

Warning: Cannot modify header information - headers already sent by (output started at /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php:24) in /home/nas03l/d/mysite.co.uk/user/htdocs/basket.php on line 27

 

Its driving me crackers

Link to comment
Share on other sites

somebody on another forum cracked this for me, hope this can solve a few headaches for someone in the future!

 

 

<?php
session_start();
if (!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
if (!isset($_SESSION['addon']) || !is_array($_SESSION['addon']))   //this line changed
{
$_SESSION['addon'] = array();
}
if (isset($_POST['bid']))
{
$_SESSION['cart'][] = $_POST['bid'];

$addonarrays = array();                                                  //this line changed

if (isset($_POST['addon']) && is_array($_POST['addon']))      //this line changed
{                                                                                //this line changed
      $addonarrays = $_POST['addon'];//this line changed
}                                                                                 //this line changed

foreach($addonarrays as $addonarray)
{
$_SESSION['addon'][] = $addonarray;
}

header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
if (isset($_GET['empty']))
{
unset($_SESSION['cart']);
unset($_SESSION['addon']);
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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