0

Hi everyone,

having a real headache over arrays at the moment. Had a search through the forums but can't find what im after so i'll begin here.

I'm in the process of setting up a shopping cart. I'm pretty much there with it all, however there is one thing which is giving me a really hard time.

My products can have attributes or options (e.g. i'm selling a t-shirt which can have different size and colour options) which are displayed in a drop down list.

My problem is, if I or a customer were to choose more than one option, e.g. a red, medium T-shirt, how would i store the two or more options in a session?

Here is my code for the drop down lists:

<select name="attribute['.$i.']" class="input" id="attribute['.$i.']">

and then some php to loop through the values.

The user would then select their desired option(s) and click on add to cart button.

Now were on add_cart.php, this is where i post the options and other details and then add it to the session.

My code for looping through the options is as follows:

$attribute = $_POST['attribute'];
	$count = $_POST['count'];
	$i = 0;
		for($i=0;$i<$count;$i++){
		$att_ref = $attribute[$i];
	}

And my code to add info to the session.

// Add to the cart:
			$_SESSION['cart'][$pid] = array ('quantity' => $qty, 'price' => $prod_price, 'description' => $desc, 'att_ref' => $att_ref);

My problem lies within the att_ref bit of the session / array.

the above code for looping through the options will pick out the correct attribute references, so if i chose option 1 and 4 $att_ref would return 14.

But when i try to add this to the session / array, it only ever adds 1 and not the 4.

So what i want to be able to do is store multiple values in the 'att_ref' => $att_ref bit and then be able to extract the values from the array and then display what options the customer chose in the view_cart.php page, which i can do, but it only ever pulls out the first option and not the 2nd and 3rd etc.

Any ideas?

Any help would be greatly appreciated!

Regards Dan.

Edited by dan_ord: n/a

4
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by NettSite
0

change line no 5 of second code block to following

$att_ref .= $attribute[$i].",";

separte selected codes with comma. Also you are directly assing you must concat to retain previous loop values.

Edited by urtrivedi: n/a

0

If this si what you want - to have all the attribute values in a comma separated string - then you just have to do an implode (no loop) to $attribute:

$att_ref = implode(",", $attribute);

But id don't understand why you don't just assign the whole $attribute array to the $_SESSION ?!!

0

If this si what you want - to have all the attribute values in a comma separated string - then you just have to do an implode (no loop) to $attribute:

$att_ref = implode(",", $attribute);

But id don't understand why you don't just assign the whole $attribute array to the $_SESSION ?!!

Hi,

Thanks to both of you for the input.

Johny_d, i've tried assigning the $attribute to the session and when i try to pull the references from it, it just says array, however the way you suggested, brings back 1,4

what i need to do now is seperate the 1,4 so that i can loop through a select query and pull out the information on the option from the database.

e.g.

$q2 ="SELECT * FROM attributes WHERE att_ref = '1'";
$q2 ="SELECT * FROM attributes WHERE att_ref = '4'";

Any idea on how to do this, or am i going about it the wrong way?

Regards,
Dan

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.