Jump to content


Photo

Subs associated to Items (Looping)


  • Please log in to reply
3 replies to this topic

#1 gabe8496

gabe8496
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 18 August 2006 - 06:02 PM

Ok, here is my scenario:

:FORM:
Customer chooses shirts from a combo box (multiple selection):
Shirt 1
Shirt 2
Shirt 3
Shirt 4
Shirt 5

When user submits, it will display the shirts selected plus the subs (Sizes) associated to the item (with checkbox next to sub):
Shirt 1 => SM, MD, LG
Shirt 2 => SM, MD, LG, 1X
Shirt 3 => One Size

Next, the customer checks the sizes they want from the available sizes. Say they select:
Shirt 1: SM, MD
Shirt 2: MD, 1X
Shirt 3: One Size

When they hit submit, it will take them to a final page, displaying the shirt they chose plus the sizes they checked right next to the shirt.

Shirt 1 => SM, MD
Shirt 2 => MD, 1X
Shirt 3 => One Size

I've created the loop that works good for displaying the shirts but it also displays the same subs for all shirts like so:

Shirt 1 => SM, MD
Shirt 2 => SM, MD
Shirt 3 => SM, MD

Its not displaying the checked sizes for the correct shirt.

Can anyone help me out here?

#2 gabe8496

gabe8496
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 18 August 2006 - 06:04 PM

Here is a snippet of the code were running:

$_POST['item'] & $_POST['sub'] are setup as arrays on the previous page, because the checkbox for the subs are like so:

<input type="checkbox" name="sub[]" value="SM">

$item = $_POST['item'];
$sub = $_POST['sub'];
$item_array = count($item);
$sub_array = count($sub);

for ($x=0; $x<$item_array; $x++)
{
echo ''.$item[$x].'';
  for ($i=0; $i<$sub_array; $i++)
  {
  echo ''.$sub[$i].'';
  }
 echo '<br>';
}


#3 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 18 August 2006 - 06:15 PM

Where are the sizes coming from. Database? or are you just typing them in?? Maybe a little more code would help.

Ray

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 18 August 2006 - 10:07 PM

If your form with sizes for the selected products looks like this, where each size checkbox has

name = "size[product id][]"

<FORM method='post'>
 
Shirt 1  <input type="checkbox" name="size[1][]" value="SM">SM 
		 <input type="checkbox" name="size[1][]" value="MD">MD 
		 <input type="checkbox" name="size[1][]" value="LG">LG <BR>
		 
Shirt 2  <input type="checkbox" name="size[2][]" value="SM">SM 
		 <input type="checkbox" name="size[2][]" value="MD">MD 
		 <input type="checkbox" name="size[2][]" value="LG">LG 
         <input type="checkbox" name="size[2][]" value="1X">1X <BR>
         
Shirt 3  <input type="checkbox" name="size[3][]" value="1X">One Size <BR>

<input type="submit" name="submit" value="Submit">
</FORM>

The processing to show the sizes would be

<?php 
if (isset($_POST['submit'])) {
	foreach ($_POST['size'] as $id => $sizes)  {
		echo "<br>$id ";
		foreach ($sizes  as $sz) {
			echo "$sz ";
		}
	}
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users