Harley1979 Posted May 13, 2007 Share Posted May 13, 2007 Hello, I'm having a bit of trouble here with getting an array to explode. When I echo it, its is simply writing "Array" 49 times. Basically I just need to seperate my arrays with a coma so I can select those ids from my table. Anyone got any ideas? I'm new to php so I'm stumped. --------------------------------------------------------------------------------------------------------------- foreach ($_SESSION['items'] as $k => $p) { $IDs = explode(", ", $_SESSION['qty'][$k]); $mysqlProd = mysql_query("SELECT * FROM products WHERE id IN ($IDs)") or die(mysql_error()); } while($prodRow = mysql_fetch_array($mysqlProd)){ $price = $prodRow['price']; $totalPrice += $price; echo " <tbody> <tr> <td>{$prodRow['item']}</td> <td>{$prodRow['size']}</td> <td>£{$price}</td> </tr> </tbody> "; } Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/ Share on other sites More sharing options...
chigley Posted May 13, 2007 Share Posted May 13, 2007 Is $_SESSION['items'] an array or a single value? Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/#findComment-251933 Share on other sites More sharing options...
Harley1979 Posted May 13, 2007 Author Share Posted May 13, 2007 Yes, it is an array. Basically what is going on, there is a form where a user can add to their shopping cart, quantities of items all at once. This form submits once only rather than having a button next to each item which will submit many times. Also I am deliberately not using javascript hence the peculiar code. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/#findComment-251970 Share on other sites More sharing options...
Barand Posted May 13, 2007 Share Posted May 13, 2007 Can you post the output from var_export($_SESSION['items']); Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/#findComment-252004 Share on other sites More sharing options...
Harley1979 Posted May 13, 2007 Author Share Posted May 13, 2007 Sorry for the lateness in reply. I was experiencing some other strange problems. Anyway back to the story: This is what I am getting when writing what you suggested. There are three pages by the way, the first where items are displayed, a second where users enter shipping info and a third where they will confirm all information given. On the second page this is the response given: array ( 1 => '5', 2 => '', 3 => '', 4 => '', 5 => '', 6 => '', 7 => '', 8 => '', 9 => '', 10 => '', 11 => '', 12 => '', 13 => '', 14 => '', 15 => '', 16 => '', 17 => '', 18 => '', 19 => '', 20 => '', 21 => '', 22 => '', 23 => '', 50 => '', 51 => '', 52 => '', 53 => '', 54 => '', 24 => '', 25 => '', 26 => '', 27 => '', 28 => '', 29 => '', 30 => '', 31 => '', 32 => '', 33 => '', 34 => '', 35 => '', 36 => '', 37 => '', 38 => '', 39 => '', 40 => '', 41 => '', 42 => '', 43 => '', 44 => '', 45 => '', 46 => '', 47 => '', 48 => '', 49 => '', ) and on the third: NULL Warning: Cannot modify header information - headers already sent by (output started at /usr/home/wwworga/public_html/wholefoods-step2.php: in /usr/home/wwworga/public_html/wholefoods-step2.php on line 24 It seems the session is being lost after submitting the second form of shipping info. Still no idea why though. Help Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/#findComment-252076 Share on other sites More sharing options...
Harley1979 Posted May 13, 2007 Author Share Posted May 13, 2007 Will page redirects have any bearing on this at all? I think that could be another factor as that is how the user goes from page to page after form validation has taken place. Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/#findComment-252078 Share on other sites More sharing options...
Barand Posted May 13, 2007 Share Posted May 13, 2007 The items array only contains a single value, the other 48 are all blank. It may be an idea to only process those with a value. Can you do same with var_export($_SESSION['qty']) Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/#findComment-252281 Share on other sites More sharing options...
Harley1979 Posted May 13, 2007 Author Share Posted May 13, 2007 array ( 1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '8', 6 => '9', 7 => '7', 8 => '6', 9 => '5', 10 => '4', 11 => '8', 12 => '3', 13 => '1', 14 => '2', 15 => '', 16 => '', 17 => '', 18 => '', 19 => '', 20 => '', 21 => '', 22 => '', 23 => '', 50 => '', 51 => '', 52 => '', 53 => '', 54 => '', 24 => '', 25 => '', 26 => '', 27 => '', 28 => '', 29 => '', 30 => '', 31 => '', 32 => '', 33 => '', 34 => '', 35 => '', 36 => '', 37 => '', 38 => '', 39 => '', 40 => '', 41 => '', 42 => '', 43 => '', 44 => '', 45 => '', 46 => '', 47 => '', 48 => '', 49 => '', ) The first bunch have values, the rest were not set as I did not input them in the form. It picks it up ok, after much reading on the net I have concluded the problem is simply the fact that I am using a redirect which apparently can cause major problems with sessions that hold an array. I have tried setting sessions to a default 0 first, then applying desired values to them but no joy. Also I have tried the session_write_close(); function to be sure the data is written into the session before redirecting. There doesn't seem to be much out there on it and I'm running out of ideas. Hope someone can help Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/#findComment-252328 Share on other sites More sharing options...
Harley1979 Posted May 13, 2007 Author Share Posted May 13, 2007 Ok this much I have discovered today: 1. On redirecting to another page, session data is reset. 2. On resubmiting the same page, session data is reset. Is there something glaringly obvious I am not doing here? Does anyone else have the same issue as I do? I am new to php, I come from an ASP background. My understanding of a session variable has been that once you set one, as long as you are in the same browser, the session is active until destryed, reset or browser is closed. What's the deal with php sessions, I know there just must be something I've missed although I've done tons of reading today? Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/#findComment-252342 Share on other sites More sharing options...
boo_lolly Posted May 13, 2007 Share Posted May 13, 2007 are you putting session_start() at the top of every page? Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/#findComment-252351 Share on other sites More sharing options...
Harley1979 Posted May 13, 2007 Author Share Posted May 13, 2007 Yeah, I just can't figure it out. I'm guessing people don't normally come across this problem. My head is sore from scratching Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/#findComment-252353 Share on other sites More sharing options...
boo_lolly Posted May 13, 2007 Share Posted May 13, 2007 right underneath session_start(), put this line on all your pages: echo "<pre>". print_r($_SESSION, true) ."</pre>\n"; see if your session data differs from page to page. Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/#findComment-252358 Share on other sites More sharing options...
Barand Posted May 13, 2007 Share Posted May 13, 2007 $_SESSION['qty'] contains 49 elements (as with items array) but this time the first 14 each have just a single value. No sub-arrays or lists of comma-delimited items to explode. Must admit I'm lost on how these two arrays (items and qty) should work together. Quote Link to comment https://forums.phpfreaks.com/topic/51165-exploding-array-problem/#findComment-252377 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.