[PHP] Re: One more newbie question. About foreach..
WOW! Thank you very, very much! This is so good! Thanks to all of you for spending time to learning me programming! Karl 2013/6/23 Maciek Sokolewicz maciek.sokolew...@gmail.com 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=1Item B: input type=text name=number_in_stock['ItemB']** value=2 input
[PHP] Re: One more newbie question. About foreach..
On 23 Jun 2013 at 16:11, Karl-Arne Gjersøyen karlar...@gmail.com wrote: // Foreach get all given serialnumbers as I want it foreach($serialnumber as $snr){ // I got the number of serialnumbers given in the array $count = count($serialnumber); Why not do this *before* the foreach loop? // I thought that a for loop would do the trick together with foreach // And repeat the $sql = SELECT below but that did not happen. for($i = 0; $i = $count; $i++){ // Connect and get only those records with given serialnumber $sql = SELECT * FROM explosive WHERE serialnumber = '$snr'; } // End of for loop } // End of foreach loop All this for loop does is overwrite $sql each time. $result = mysql_query($sql, $connect) or die(mysql_error()); $count = mysql_num_rows($result); You need to have your mysql_query *inside* the for loop. Then, what are you doing with the results of the mysql_query? Nothing? Also, why do you need the for loop anyway? What is it supposed to do? -- Cheers -- Tim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: One more newbie question. About foreach..
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=1Item B: input type=text name=number_in_stock['ItemB'] value=2 input type=checkbox name=item_list['ItemC'] value=1Item 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