'foreach' for multiple fields - how do I do it?
Moderator: General Moderators
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
'foreach' for multiple fields - how do I do it?
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.
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.
Last edited by simonmlewis on Wed Apr 02, 2014 9:55 am, edited 1 time in total.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
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?
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
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?
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)
{
}
}
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
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?
Update:
Got it passing through all the data and it works, but because of this:
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?
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)
{
}
}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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
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?
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
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
- Attachments
-
- Select each item, the <select> field is the romancode field. But each field's price needs to be passed over too. I'm completely stuck on this bit.
- ss.jpg (11.93 KiB) Viewed 4341 times
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: 'foreach' for multiple fields - how do I do it?
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?
(#10850)
-
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?
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.
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: 'foreach' for multiple fields - how do I do it?
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].
-
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?
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?
But keeping the rest of the code the same?
But how would that only grab those rows?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: 'foreach' for multiple fields - how do I do it?
No, I mean using the existing IDs as keys. You're already using them as the values for the checkboxes.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: 'foreach' for multiple fields - how do I do it?
I am still not clear what you are trying to do, but following Celauran's thinking you could do something like:
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.
Code: Select all
echo "<input type=\"checkbox\" name=\"price[{$row->romancode}]\" value=\"{$row->romancode}\" id=\"{$row->romancode}\">";(#10850)
Re: 'foreach' for multiple fields - how do I do it?
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?
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?
-
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?
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?
I know I'm not showing it all here, but is the foreach correct, and method of showing romancode[] and price[] ?
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>";
}Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: 'foreach' for multiple fields - how do I do it?
Why so many submits?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.
Not based on what I had described. You'd want to include the IDs so you know which row is which.simonmlewis wrote:Is this the correct code to use?
I know I'm not showing it all here, but is the foreach correct, and method of showing romancode[] and price[] ?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>"; }