Page 2 of 2

Re: 'foreach' for multiple fields - how do I do it?

Posted: Mon Apr 07, 2014 5:53 am
by simonmlewis
Why so many submits?
Because I first need to show the products in the list (the tick boxes).
Then I need to show what category they want to be copied into.
Submit.
Then I need to show what subcategory (which that category) to copy them into.
Then... Complete (Submit) button to do all the magic.

So how do I compile the array with the IDs ready for the first submit?

Re: 'foreach' for multiple fields - how do I do it?

Posted: Mon Apr 07, 2014 6:11 am
by Celauran
Had to go back and re-read everything and still not sure I've got it 100%. Romancode is some kind of ID, I take it, which you're pulling from the DB, and you're assigning a price manually? Is that correct? So every generated row has a set ID, and text input field for the price, and you only want to process those rows where you've checked the romancode field? If so, you want something like this:

Code: Select all

<input type="text" name="price[<?= $row->romancode; ?>]" value="">
<input type="checkbox" name="romancode[]" value="<?= $row->romancode; ?>">
When it comes time to process the form, you can iterate over $_POST['romancode'] and for each value, grab the associated value of price[].

Code: Select all

<?php

$update = [];
foreach ($_POST['romancode'] as $roman) {
	$update[] = [
		'romancode' => $roman,
		'price' => $_POST['price'][$roman],
	];
}
or some such.

Re: 'foreach' for multiple fields - how do I do it?

Posted: Mon Apr 07, 2014 6:55 am
by simonmlewis
Yes you are correct.
So for the first page of categories, this correct?

Code: Select all

if (!isset($catid))
    { 
    $update = [];
    echo "<form method='post' action='/a_products_justbbguns&u=copy'>";
      foreach ($_POST['romancode'] AS $roman)
      {
      $update = [] = [
      'romancode' => $roman,
      'price => $_POST['price'][$roman],
      ];
    }
    else
    {
    $update = [];
    echo "<form method='post' action='/a_products_justbbguns&u=copy&uu=run'>";
      foreach ($_POST['romancode'] AS $roman)
      {
      $update = [] = [
      'romancode' => $roman,
      'price => $_POST['price'][$roman],
      ];
    }

 if (!isset($catid))
  {
  echo "<select name='catid'>";
  
  $result2 = mysql_query ("SELECT id, categories FROM categories ORDER BY categories", $dbjbbgx);
    while ($row2 = mysql_fetch_object($result2))
      {
      echo "<option value='$row2->id'>$row2->categories</option>";
      }
  
  echo "</select>";
  }
  
  if (isset($catid))
  {
    $resultcatname = mysql_query ("SELECT categories FROM categories WHERE id = '$catid'", $dbjbbgx);
    while ($rowcatname = mysql_fetch_object($resultcatname))
      {
      echo "<input type='text' name='catname' value='$rowcatname->categories' readonly style='width: 160px'>";
      }
      
  echo "<br/><input type='hidden' name='catid' value='$catid'>
  <select name='subid'>";
  
  $result2 = mysql_query ("SELECT subid, name FROM subcategories WHERE catid = '$catid' ORDER BY name", $dbjbbgx);
    while ($row2 = mysql_fetch_object($result2))
      {
      echo "<option value='$row2->subid'>$row2->name</option>";
      }
  
  echo "</select>";
  }
  
  
  echo "</td><td><input type='submit' value='";
  if (!isset($catid))
  { echo "Next";}
  else
  {
  echo "Complete";
  }
  echo "' style='font-size: 14px'></td></tr></table><br/>
  </form><br/><br/><br/>
Within this is the section for Sub Categories - I think I have put that in the right place....??

Re: 'foreach' for multiple fields - how do I do it?

Posted: Mon Apr 07, 2014 7:01 am
by Celauran
No, that doesn't look right. You've defined the $update array but don't appear to be doing anything with it.

Re: 'foreach' for multiple fields - how do I do it?

Posted: Mon Apr 07, 2014 7:06 am
by simonmlewis
I've used what you sent me - tho I was missing a }.

Re: 'foreach' for multiple fields - how do I do it?

Posted: Mon Apr 07, 2014 8:33 am
by Celauran
That was just some quick sample code to show how to associate price and ID. As I mentioned, right now you're just creating an array and then doing nothing with it. Given that this is a multi-step form, you could store that information in a hidden field or in session data until you're ready to actually process the form and start inserting into your DB.

Re: 'foreach' for multiple fields - how do I do it?

Posted: Mon Apr 07, 2014 9:45 am
by simonmlewis
How would I store the selected fields I posted, into a session? I know how to store data into a session, but have never done it with an array. specially not when the array contains multiple fields of data. That is what has really thrown me here.

How to store multiple fields of data, and then use them further on.

Re: 'foreach' for multiple fields - how do I do it?

Posted: Mon Apr 07, 2014 10:46 am
by Celauran
Same as you would any other variable.

Code: Select all

$_SESSION['foo'] = $bar;