Page 1 of 2

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

Posted: Wed Apr 02, 2014 3:58 am
by simonmlewis
UPDATE:
Since this was posted today, I've updated it.
The page echos data from a database. Each row then has a text price field with a value entered into it, NOT from the database.
There is a checkbox on the last entry in the row, which has a stockcode in it.

I want to check only the selected boxes, and it passes that romancode AND the price field into the array. So on the following pages it is all processed.

Right now it is passing only the romancode fields selected, but ALL the price fields. How do I make it only pass over the fields, for which I have selected the romancode checkbox??



-----------------


I have a page where I select tick boxes from a list of comments, and hit a button and it passes those IDs over, and for each id in the loop, it processes them.

I now have a page where I have a list of products that are to be copied from one site to another, converting them from £ Pound to € Euro.
In the page I see title, price etc, but also a <input type='text'> box which auto enters the current Euro value of the price found.

I need to tick a box per per. Then submit at the bottom, and it takes me to a page where I choose the category from the current web site, hit submit, then subcategory from that web site, and hit GO.

It then copies all those products, with the Euro prices from that <input> box, along with the Cat and SubCat, into the current web site database.

I already have it to select it per product, but how do I do it with multiple entries, each with the price field, and store it per page, as it turns over??

Sorry if this isn't clear.

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

Posted: Wed Apr 02, 2014 4:51 am
by simonmlewis
I think it needs to be stored in a session - but lost how to put each "foreach" item into a session, to then use in a mysql query.

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

Posted: Wed Apr 02, 2014 5:42 am
by simonmlewis

Code: Select all

<form method='post' action='/a_products&u=copy'>
<input type='text' style='width: 60px; text-align: right' name='price[]' value='";
        $eurovalue = $row->price * 1.20;
        $eurovalueround = round($eurovalue);
        printf ("%.2f", $eurovalueround);      
        echo "' id='";
        printf ("%.2f", $eurovalueround); 
        echo "'> €</td>
        <td nowrap align='center' style='background-color: #efefef; padding: 2px' onMouseOver=\"this.bgColor='#E7E7E7';\" onMouseOut=\"this.bgColor='#efefef';\"><input type='checkbox' name='romancode[]' value='$row->romancode' id='$row->romancode'>Select

<input type='submit' value='submit'>
</form>

Code: Select all

<form method='post' action='/a_products_s&u=copy&uu=run'>
        <input type='hidden' name='romancode' value='";
      print_r($romancode);
      echo "' id='";
      print_r($romancode);
      echo "'>
        <input type='hidden' name='price' value='";
      print_r($price);
      echo "' id='";
      print_r($price);
      echo "'>
<input type='submit' value='submit'>
</form>

Code: Select all

if ($uu == "run")
  {
  foreach($_POST['romancode'] as $rcid)
  {
  foreach($_POST['price'] as $price)
  {
}
}
Here is a shortened version of the code.
The area where it has hidden fields seems to be working, as I can echo it on screen to see it showing up.
It's the foreach that is doing nothing at all. I'm querying $rcid and $price to run INSERTs, but it's inserting nothing. Wondering if I am getting something wrong at the start.

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

Posted: Wed Apr 02, 2014 6:00 am
by simonmlewis
Update:
Got it passing through all the data and it works, but because of this:

Code: Select all

  foreach($_POST['romancode'] as $rcid)
  {
  foreach($_POST['price'] as $price)
  {
}
}
It is then running through each "romancode" three times, since there were three prices on the first screen.
So even though I am clicking only two of the romancode[] check boxes, all three of the price fields are passing through.

How do I make sure only those prices, of those rows I have selected and passed over? OR - in the foreach loop, how do I ensure it only does the foreach price, per romancode loop?

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

Posted: Wed Apr 02, 2014 7:51 am
by simonmlewis
A thought - for anyone reading this, can you run thru the romancode loop, and then run the price loop only for as many times as there are romancode loops?

So if there are 2 entries for romancode, and it's passed over all 3 for price, just do the 2?

I can get it to do only the romancode selected fields, and only the amount of fields selected for price. But it just does the top two price, if I select the top and bottom check box.

I need it to only pass through the fields, for the selects I haev checked. I'm sure it's down to the IDs of the field names.

Like this, but I want to be able to select which rows to choose - top and bottom perhaps.
http://stackoverflow.com/questions/1815 ... reach-loop

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

Posted: Wed Apr 02, 2014 10:21 pm
by Christopher
Wow ... that is a lot of explanation. And I am still not exactly clear what you want to do. It seems like for each "romancode" (whatever that is?) you want multiple prices -- each with a checkbox. I'm not clear whether you are asking about generating the form fields or receiving the request data? And you code is wacky with single quotes in the HTML and print_r()ing values. What does the data in the two post variables look like?

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

Posted: Thu Apr 03, 2014 2:23 am
by simonmlewis
romancode is just a field name in the database.
Ok, let me tell you exactly what the purpose is for this.

This is a foreign web site. It uses data from a UK web site. So we copy data from the UK site over to this foreign site.
This page is a collection of data from one subcategory on the UK site, price, title etc, and the euro input box.

The Euro input box calculates the euro value of £89.99 (or whatever the $row->price is).
The 'select' box is purely there to say "I want to copy this one one", but in this case it is contains the $row->romancode value.

Each row I want to "copy from UK to ES for example" is selected.
On the next page is looks for the categories in the ES (Spain) Site, then after a submit it looks for the Subcategories.

When you click Submit, it then uses the array of Euro value, and Romancode value, and inserts them into the ES database. It also adds an "es" to the start of the romancode. For example: SHIRTXL, would because esSHIRTXL. That bit works!

The *only* bit that doesnt' 'work, is selecting individual rows from the HTML <tr> set of rows as explain at the start here.

Sorry for the "wacky" code, was trying everything I could think of to make this darn thing work.

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

Posted: Fri Apr 04, 2014 8:52 am
by Celauran
If I understand you correctly, could you maybe use the existing IDs as array keys? Only the selected checkboxes will be included in the post array, and you could use romancode[2], say, to get the corresponding value for price[2].

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

Posted: Fri Apr 04, 2014 8:56 am
by simonmlewis
I'm really new to this array stuff. Do you mean using some dynamic numeric titling, $count ++ and then romancode[$count], price[$count] ??
But keeping the rest of the code the same?
But how would that only grab those rows?

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

Posted: Fri Apr 04, 2014 10:26 am
by Celauran
No, I mean using the existing IDs as keys. You're already using them as the values for the checkboxes.

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

Posted: Fri Apr 04, 2014 11:20 am
by Christopher
I am still not clear what you are trying to do, but following Celauran's thinking you could do something like:

Code: Select all

echo "<input type=\"checkbox\" name=\"price[{$row->romancode}]\" value=\"{$row->romancode}\" id=\"{$row->romancode}\">";
Not sure it you want to do it that way or do name="romancode[{$row->price}]" ... or both. They will give you an assoc array in $_POST.

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

Posted: Fri Apr 04, 2014 12:19 pm
by Celauran
Right, something along those lines. price[$row->id] then, as you're looping over the romancode array, you can easily access the corresponding entry in the price array since they're both indexed by row ID.

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

Posted: Fri Apr 04, 2014 2:00 pm
by bgpitts
Celauran wrote:If I understand you correctly, could you maybe use the existing IDs as array keys? Only the selected checkboxes will be included in the post array, and you could use romancode[2], say, to get the corresponding value for price[2].



Do you mean using some dynamic numeric titling, $count?

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

Posted: Mon Apr 07, 2014 3:25 am
by simonmlewis
When they click Submit, it takes them to the next page which asks them which Category they wish to put the product(s) in, and then after submitting again, which sub category.

Is this the correct code to use?

Code: Select all

if (!isset($catid))
    { echo "<form method='post' action='/a_products_justbbguns&u=copy'>";
      foreach ($romancode as $array)
      {
      echo "<input type='hidden' name='romancode[]' value='$array'/>";
      }
      foreach ($price as $array)
      {

      echo "<input type='hidden' name='price[]' value='$array'/>";
      }
echo "......... </form>";
}
I know I'm not showing it all here, but is the foreach correct, and method of showing romancode[] and price[] ?

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

Posted: Mon Apr 07, 2014 5:46 am
by Celauran
simonmlewis wrote:When they click Submit, it takes them to the next page which asks them which Category they wish to put the product(s) in, and then after submitting again, which sub category.
Why so many submits?
simonmlewis wrote:Is this the correct code to use?

Code: Select all

if (!isset($catid))
    { echo "<form method='post' action='/a_products_justbbguns&u=copy'>";
      foreach ($romancode as $array)
      {
      echo "<input type='hidden' name='romancode[]' value='$array'/>";
      }
      foreach ($price as $array)
      {

      echo "<input type='hidden' name='price[]' value='$array'/>";
      }
echo "......... </form>";
}
I know I'm not showing it all here, but is the foreach correct, and method of showing romancode[] and price[] ?
Not based on what I had described. You'd want to include the IDs so you know which row is which.