robgood Posted July 8, 2007 Share Posted July 8, 2007 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted July 8, 2007 Share Posted July 8, 2007 If you have removed code, is line 24 above the same as your line 24. It would help if you pointed out line 24, I can't count. Quote Link to comment Share on other sites More sharing options...
sasa Posted July 8, 2007 Share Posted July 8, 2007 where is form from which $_POST['addon'] come Quote Link to comment Share on other sites More sharing options...
robgood Posted July 8, 2007 Author Share Posted July 8, 2007 sorry, should be same as i haven't changed the top section foreach($addonarrays as $addonarray) this line 24 -> { $_SESSION['addon'][] = $addonarray; } Quote Link to comment Share on other sites More sharing options...
Barand Posted July 8, 2007 Share Posted July 8, 2007 Can't immediately see it, but you get that error if you try to use a normal variable as an array EG <?php $var = 1; $var[] = 2; // gives same error message about using scalars as an array ?> Quote Link to comment Share on other sites More sharing options...
robgood Posted July 8, 2007 Author Share Posted July 8, 2007 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> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 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. ?> Quote Link to comment Share on other sites More sharing options...
robgood Posted July 8, 2007 Author Share Posted July 8, 2007 i'm sorry cool dude. I'm not sure i follow. could you expand on your last post? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 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(); } Quote Link to comment Share on other sites More sharing options...
robgood Posted July 8, 2007 Author Share Posted July 8, 2007 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? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 what is line 26 that is erroring? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 8, 2007 Share Posted July 8, 2007 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; } Quote Link to comment Share on other sites More sharing options...
robgood Posted July 8, 2007 Author Share Posted July 8, 2007 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted July 8, 2007 Share Posted July 8, 2007 What about $_SESSION['addon'] ? Quote Link to comment Share on other sites More sharing options...
robgood Posted July 8, 2007 Author Share Posted July 8, 2007 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted July 8, 2007 Share Posted July 8, 2007 Did you put the var_dump code on line 2? Quote Link to comment Share on other sites More sharing options...
robgood Posted July 8, 2007 Author Share Posted July 8, 2007 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(); } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 8, 2007 Share Posted July 8, 2007 Looks OK. Want to try again? Quote Link to comment Share on other sites More sharing options...
robgood Posted July 8, 2007 Author Share Posted July 8, 2007 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???? Quote Link to comment Share on other sites More sharing options...
robgood Posted July 9, 2007 Author Share Posted July 9, 2007 bump Quote Link to comment Share on other sites More sharing options...
robgood Posted July 9, 2007 Author Share Posted July 9, 2007 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 Quote Link to comment Share on other sites More sharing options...
robgood Posted July 9, 2007 Author Share Posted July 9, 2007 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(); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.