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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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....??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

No, that doesn't look right. You've defined the $update array but don't appear to be doing anything with it.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

I've used what you sent me - tho I was missing a }.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Same as you would any other variable.

Code: Select all

$_SESSION['foo'] = $bar;
Post Reply