Jump to content

Subs associated to Items (Looping)


gabe8496

Recommended Posts

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?
Link to comment
Share on other sites

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">

[code]
$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>';
}
[/code]
Link to comment
Share on other sites

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

name = "size[product id][]"

[code]
<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>[/code]

The processing to show the sizes would be

[code]<?php
if (isset($_POST['submit'])) {
foreach ($_POST['size'] as $id => $sizes)  {
echo "<br>$id ";
foreach ($sizes  as $sz) {
echo "$sz ";
}
}
}
?>[/code]
Link to comment
Share on other sites

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.