Jump to content

Recommended Posts

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>

";

}

 

Link to comment
https://forums.phpfreaks.com/topic/51165-exploding-array-problem/
Share on other sites

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

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:8) 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

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

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?

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

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.