[PHP] Help on dealing with arrays of HTTP_POST vars
Hello All, I have developed a system to retrieve data from a table. The interface is a dynamic list of orders created from a query on the database that shows all the new orders. Next to each is a checkbox. Each checkbox has as its value the order number and is called PED1, PED2, PED3,PEDn, depending on how many new orders are available. If the user wants to access these orders he has to check the orders he wants and click the submit button. Once it is submited, it searches all the PED# variables on the HTTP_POST_VARS and builds an array called $Orders. The $Orders array contains order numbers and is later used to query a different table. Ths code works almost great. But there is a problem, if the first checkbox, say PED1 field is not checked, and some of the other checkboxes are (say PED2, PED4), the $Orders array ends being empty. So, say that the form has 5 orders and the user selects orders 2, 4 and 5 and submit it. It does not work. But if he selects 1,2, 4 and 5 it works. It works only if the first checkbox is checked too. What I wonder is why this code does not work if PED1 checkbox is not checked. What if the user only wants other stuff but the first order PED1? Could you help me on this? Here's the code: This is a function that searches for variables called PED1, PED2, PED3 that are checkboxes on a submited form and have the order numbers. When this checkbox is checked, the ordernumber is saved on an Array $Orders that is later used to build a SQL statement. function SearchOrders() { global $Orders; global $HTTP_POST_VARS; $index = count($HTTP_POST_VARS); for($i=1; $i = $index; $i++) { if (isset($HTTP_POST_VARS[PED$i])) $Orders[] = $HTTP_POST_VARS[PED$i]; } $index = count($Orders); return $index; } This is a function that retrieves the array values that are order numbers from the array $Orders and assembles a SQL statement $query. This will later retrieve data from a MySQL table; function MontarOrdRel() { global $Orders; $query = SELECT * FROM tbl_Ord ; if (count($Orders) 0) $query .= WHERE ; for($index=0; $index count($Orders); $index++) { if ($index (count($Orders)-1)) { $query .=(NumPedido = '.$Orders[$index].') OR ; } else { $query .=(NumPedido = '.$Orders[$index].') ; } } // for loop $query .= ORDER BY NumOrd; Then $query is used to query a table. Thank you for your help, Carlos Fernando Scheidecker Antunes. Linux User #207984 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Help on dealing with arrays of HTTP_POST vars
-Original Message- From: Carlos Fernando Scheidecker Antunes [mailto:[EMAIL PROTECTED]] function SearchOrders() { global $Orders; global $HTTP_POST_VARS; $index = count($HTTP_POST_VARS); for($i=1; $i = $index; $i++) { The trouble is here . You should be test for the max Number of possible checkboxes, and not the number checked. If 3,4,5 are checked, then $index is 3, bu thte varaiables will have names like PED$3, PED$4, PEF$5 and your code will stop at 3. if (isset($HTTP_POST_VARS[PED$i])) $Orders[] = $HTTP_POST_VARS[PED$i]; } $index = count($Orders); return $index; Why not name the checkboxes as arrays like: input type=checkbox name=PED[] value=18191 input type=checkbox name=PED[] value=18192 Then in php, PED[0] is the first *checked* value (so it could be the 5th checkbox). Test with is_array to see if anything is check at all. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Help on dealing with arrays of HTTP_POST vars
Hello All, I have developed a system to retrieve data from a table. The interface is a dynamic list of orders created from a query on the database that shows all the new orders. Next to each is a checkbox. Each checkbox has as its value the order number and is called PED1, PED2, PED3,PEDn, depending on how many new orders are available. If the user wants to access these orders he has to check the orders he wants and click the submit button. Once it is submited, it searches all the PED# variables on the HTTP_POST_VARS and builds an array called $Orders. The $Orders array contains order numbers and is later used to query a different table. Ths code works almost great. But there is a problem, if the first checkbox, say PED1 field is not checked, and some of the other checkboxes are (say PED2, PED4), the $Orders array ends being empty. So, say that the form has 5 orders and the user selects orders 2, 4 and 5 and submit it. It does not work. But if he selects 1,2, 4 and 5 it works. It works only if the first checkbox is checked too. What I wonder is why this code does not work if PED1 checkbox is not checked. What if the user only wants other stuff but the first order PED1? Could you help me on this? Here's the code: This is a function that searches for variables called PED1, PED2, PED3 that are checkboxes on a submited form and have the order numbers. When this checkbox is checked, the ordernumber is saved on an Array $Orders that is later used to build a SQL statement. function SearchOrders() { global $Orders; global $HTTP_POST_VARS; $index = count($HTTP_POST_VARS); for($i=1; $i = $index; $i++) { if (isset($HTTP_POST_VARS[PED$i])) $Orders[] = $HTTP_POST_VARS[PED$i]; } $index = count($Orders); return $index; } This is a function that retrieves the array values that are order numbers from the array $Orders and assembles a SQL statement $query. This will later retrieve data from a MySQL table; function MontarOrdRel() { global $Orders; $query = SELECT * FROM tbl_Ord ; if (count($Orders) 0) $query .= WHERE ; for($index=0; $index count($Orders); $index++) { if ($index (count($Orders)-1)) { $query .=(NumPedido = '.$Orders[$index].') OR ; } else { $query .=(NumPedido = '.$Orders[$index].') ; } } // for loop $query .= ORDER BY NumOrd; Then $query is used to query a table. Thank you for your help, Carlos Fernando Scheidecker Antunes. Linux User #207984 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Help on dealing with arrays of HTTP_POST vars
in your SearchOrder() function change the for() to a foreach() Jim - Original Message - From: Carlos Fernando Scheidecker Antunes [EMAIL PROTECTED] To: PHP-GENERAL [EMAIL PROTECTED] Sent: Wednesday, December 19, 2001 10:15 AM Subject: [PHP] Help on dealing with arrays of HTTP_POST vars Hello All, I have developed a system to retrieve data from a table. The interface is a dynamic list of orders created from a query on the database that shows all the new orders. Next to each is a checkbox. Each checkbox has as its value the order number and is called PED1, PED2, PED3,PEDn, depending on how many new orders are available. If the user wants to access these orders he has to check the orders he wants and click the submit button. Once it is submited, it searches all the PED# variables on the HTTP_POST_VARS and builds an array called $Orders. The $Orders array contains order numbers and is later used to query a different table. Ths code works almost great. But there is a problem, if the first checkbox, say PED1 field is not checked, and some of the other checkboxes are (say PED2, PED4), the $Orders array ends being empty. So, say that the form has 5 orders and the user selects orders 2, 4 and 5 and submit it. It does not work. But if he selects 1,2, 4 and 5 it works. It works only if the first checkbox is checked too. What I wonder is why this code does not work if PED1 checkbox is not checked. What if the user only wants other stuff but the first order PED1? Could you help me on this? Here's the code: This is a function that searches for variables called PED1, PED2, PED3 that are checkboxes on a submited form and have the order numbers. When this checkbox is checked, the ordernumber is saved on an Array $Orders that is later used to build a SQL statement. function SearchOrders() { global $Orders; global $HTTP_POST_VARS; $index = count($HTTP_POST_VARS); for($i=1; $i = $index; $i++) { if (isset($HTTP_POST_VARS[PED$i])) $Orders[] = $HTTP_POST_VARS[PED$i]; } $index = count($Orders); return $index; } This is a function that retrieves the array values that are order numbers from the array $Orders and assembles a SQL statement $query. This will later retrieve data from a MySQL table; function MontarOrdRel() { global $Orders; $query = SELECT * FROM tbl_Ord ; if (count($Orders) 0) $query .= WHERE ; for($index=0; $index count($Orders); $index++) { if ($index (count($Orders)-1)) { $query .=(NumPedido = '.$Orders[$index].') OR ; } else { $query .=(NumPedido = '.$Orders[$index].') ; } } // for loop $query .= ORDER BY NumOrd; Then $query is used to query a table. Thank you for your help, Carlos Fernando Scheidecker Antunes. Linux User #207984 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]