Re: One more newbie question. About foreach..

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 23-6-2013 17:11, Karl-Arne Gjersøyen wrote:
Hello again. I Got the solution for my last mention problem. Now I can
update several rows at once by one single submit action.

[...]
I have tried to search in google and on PHP.net but can't fine anything
that explain my problem.
I like to have new $sql = SELECT queries for every given serialnumber ($snr)

Thanks for your help.

Karl


Ok, Karl, I've seen quite a few messages from you to this list with various questions; from all these questions it becomes clear that you're trying to work with lists in a single form without understanding the basics of a form in the first place. You also seem to not understand what an array is exactly, or how to process it.

So let's answer all these questions at once; I will attempt to explain (in short) how to do all this, using a custom form here. I don't speak Norwegian, and I find your variable names to be horribly long and complex, so let's not use them :)

Say you want a simple form online which gives you a product name and asks you to update the amount of it in stock.
<form action="..." method="post">
<input type="text" name="number_in_stock" value="0">
<input type="submit" name="submit" value="store">
</form>

When you submit this simple form, the PHP script defined in the action property is called, and the $_POST array looks like:
$_POST = array(
   'number_in_stock' = '0'
);

You can then run your simple query
mysql_query("UPDATE some_table SET stock='" . $_POST['number_in_stock']); // note: you should always sanitize these values; i.e. make sure it is EXACTLY what you expect, and CAN NOT possibly be anything else. So if you expect it to be a number, check if it IS a number before running this query!!!

Now, you said you wanted to update multiple items.
Imagine you have a form showing multiple items:
<form action="..." method="post">
Item A: <input type="text" name="number_in_stock" value="8">
Item B: <input type="text" name="number_in_stock" value="2">
Item C: <input type="text" name="number_in_stock" value="258">
<input type="submit" name="submit" value="store">
</form>

If you do this, PHP will have no idea what is what, and it will just keep overwriting the number_in_stock value until it reaches the last one. So you would end up with a $_POST array looking like this:
$_POST = array(
   'number_in_stock' = '258'
);

Obviously, you don't want that. The solution would be to tell PHP to turn all recieved values into an array. This can be done by manually specifying the key for each array item; or letting PHP do it automatically. This automatic way was suggested earlier, like so:
<form action="..." method="post">
Item A: <input type="text" name="number_in_stock[]" value="8">
Item B: <input type="text" name="number_in_stock[]" value="2">
Item C: <input type="text" name="number_in_stock[]" value="258">
<input type="submit" name="submit" value="store">
</form>

This then results in a $_POST array like this:
$_POST = array(
   'number_in_stock' = array(
      0 => '8',
      1 => '2',
      2 => '258'
   )
);

So now, do you have any idea what is what? No. You don't. Why? because you don't supply a link between the value and the meaning. Instead, you could decide to supply a certain unique key per item, like so:
<form action="..." method="post">
Item A: <input type="text" name="number_in_stock['ItemA']" value="8">
Item B: <input type="text" name="number_in_stock['ItemB']" value="2">
Item C: <input type="text" name="number_in_stock['ItemC']" value="258">
<input type="submit" name="submit" value="store">
</form>

This results in:
$_POST = array(
   'number_in_stock' = array(
      'ItemA' => '8',
      'ItemB' => '2',
      'ItemC' => '258'
   )
);

Wow, now you can actually use this info when updating your table in the DB!
foreach($_POST['number_in_stock'] as $item=>$number) {
mysql_query("UPDATE table SET stock='".$number."' WHERE itemId='".$item);
}
This will run over each element in the $_POST['number_in_stock'] array. It will (for that single loop-run) stick the key in $item and the value in $number. For each run, it will run the update query. Then in the next run, a new set of values is supplied, and a new query is ran.

If you want to expand this to give you the ability to define which items should be updated and which should not, you could add checkboxes. When checked, the item will be updated; otherwise it won't. Checkboxes have a great feature where if they are checked, they have the value supplied in their value attribute. If they are not checked, they have no value. So, you could add something like:
<form action="..." method="post">
<input type="checkbox" name="item_list['ItemA'] value="1"> Item A: <input type="text" name="number_in_stock['ItemA']" value="8"> <input type="checkbox" name="item_list['ItemB'] value="1">Item B: <input type="text" name="number_in_stock['ItemB']" value="2"> <input type="checkbox" name="item_list['ItemC'] value="1">Item C: <input type="text" name="number_in_stock['ItemC']" value="258">
<input type="submit" name="submit" value="store">
</form>

When you submit this form, and have only the first checkbox ticked, the $_POST array will look like this:
$_POST = array(
   'item_list' = array(
      'itemA' => '1',
      'itemB' => '',
      'itemC' => ''
   ),
   'number_in_stock' = array(
      'ItemA' => '8',
      'ItemB' => '2',
      'ItemC' => '258'
   )
);

You now know that only itemA should be updated. For this, you could run a foreach on item_list, check which value == 1 and then for those, run the query:

foreach($_POST['item_list'] as $item=>$value) {
   if($value == 1) {
      // apparently we want to update this item
$number = $_POST['number_in_stock'][$item]; // $item = 'ItemA' or 'ItemB' or whatever name was selected mysql_query("UPDATE table SET stock='".$number."' WHERE itemId='".$item);
   }
}

Simple as that :)
Don't start unsetting, and doing I don't know what, just KISS (Keep It Stupidly Simple)

- Tul

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux