views:

230

answers:

3

Order Page: On this page, customers can choose to add business card orders with different foreign languages and other options.

When the user clicks on a button to add an additional card, javaScript adds a series of form fields and in order to give them unique name attributes, I am simply adding a counter variable at the end of the name for each group of fields, so I can save them as a unique group of session variables where each form field has different names but the same counter number at the end so I know they belong together.

These are the session variables I am saving when the user submits the form to go to the next page:

  • $_SESSION[quantity1] = 500
  • $_SESSION[language1] = Korean
  • $_SESSION[quantity2] = 250
  • $_SESSION[language2] = Chinese

Checkout Page: On this page, I want it to echo out the information for each different card ordered.

I am trying to echo out all the session variables with the same number at the end of the session variable on the checkout page. So far I'm using a foreach loop to echo out all the session variables, but I am trying to only echo out the ones with a '1' at the end of the names, or a '2' at the end of the names, etc, and group them together.

So ideally, I would like something like this:

Order #1,
Quantity: 500,
Language on Card: Korean

Order #2,
Quantity: 250,
Language on Card: Chinese

How can I echo and group these dynamic session variables on the checkout page?

+1  A: 

Grouping the fields is relatively easy. Just loop through them all, pull off the number at the end and groups them in another array.

$vars = array();
foreach ($_SESSION as $k => $v) {
  if (preg_match('!\d+$!', $k, $matches)) {
    $number = $matches[0];
    if (array_key_exists($number, $vars)) {
      $vars[$number][$k] = $v;
    } else {
      $vars[$number] = array($k => $v);
    }
  }
}
print_r($vars);

To display them loop through $vars and do what you need to do. You may need to sort $vars if you want them in a particular order. You may need to sort the sub-arrays to put the fields in a particular order.

cletus
+2  A: 

Restructure your session when saving to it instead:

  • $_SESSION[order][1][quantity] = 500
  • $_SESSION[order][1][language] = Korean
  • $_SESSION[order][2][quantity] = 250
  • $_SESSION[order][2][language] = Chinese

That way you can use foreach() on order to get it all cleanly.

Ignacio Vazquez-Abrams
I like this idea. I have managed to save the sessions the way you recommended, but I am not too sure how to write the foreach loop and what you mean by 'order'. I can't find that function on php.net
zeckdude
`foreach($_SESSION['order'] as $order) { ... }`
Ignacio Vazquez-Abrams
I figured out I need to put a foreach loop within that foreach loop. Thank you!
zeckdude
+3  A: 

Why not store in the session in a manner like this?

$_SESSION[orders] => array(
  [0]=>array(
     'quantity'=>1,
     'language'=>'Korean'
   ),
  [1]=>array(
    'quantity'=>2,
    'language'=>'Chinese'
   ),
)

Then your iteration on checkout page will be simple

Mailslut