Re: [PHP] Re: order of elements in $_POST super global
Hi Richard, I'm basically building a custom mysql table to delimited text file function for a membership site. So I've got a lot of typical demographic info fields like name, address, etc. The site owners would like to dump the membership information for their own purposes, but they don't always want a complete set of all data. So the form lists each field with a checkbox to determine whether it is desired in the exported text file. To simplify things, the names of the checkbox form fields correspond exactly to the names of the related fields in the mysql table. So if there is a field named home_address1, the form checkbox is also name="home_address1". When I capture the form data, I iterate through the $_POST variable and create a custom query based on the checkboxes that are checked. So the form presented looks like: Prefix First Nameinput> Middle Nameinput> Last Nameinput> Suffix Home Address (Street) Home Address 2input> Since the form fields names correspond to mysql field names rather than just a plain index array, I can use the keys thus: $exp_format=array_shift($_POST); if ($exp_format=="tab") {$delimiter="\t"; $filesuffix=".txt";} if ($exp_format=="csv") {$delimiter=","; $filesuffix=".csv";} /* reset three array elements in the POST superglobal, they are flags and should not be part of the custom query */ $mem_activity=array_shift($_POST); $part_pref=$_POST['part_pref']; unset($_POST['part_pref']); $query="SELECT mem_id, "; foreach ($_POST as $key => $data) { $query.="$key, "; } /* chops off the last space and comma */ $length=strlen($query); $query=substr($query, 0, $length-2); So what I really want to know is if there is some kind of best- practice method of rearranging the order of the items in a $_POST array without rearranging the elements on the original form, or post- processing them item by item. Thanks for any insights... - Ben The script that processes the form looks like: On Jun 9, 2006, at 7:12 PM, Richard Lynch wrote: name="bool[0][careers]" might do what you want... it's really quite difficult to say without a concrete example... On Thu, June 8, 2006 10:20 am, Ben Liu wrote: I probably should add some more details to my question: The names of the form checkboxes could be changed from ie: bool_careers, bool_speaking, bool_internship, etc. to a single array bool_questions[], for instance. The problem with that is that I am using the form checkbox names later to process related data. Specifically, I am building a user-input based query. So if the form checkbox names are the same as the names of the variables in the database, it makes for easy creating of the query, since the names become associative keys in the $_POST array. I iterate through the $_POST array, checking each for true/false state. If the checkbox is checked, I add the associative key name to the query. Eliminating the checkbox names in favor of a numerical key only would make this more complicated. - Ben On 6/8/06, Barry <[EMAIL PROTECTED]> wrote: 1. Use Keys in your form like a[1],a[2] 2. order the array by usort (alphabetically or whatever u prefer) that way => 2: yes it is. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Like Music? http://l-i-e.com/artists.htm smime.p7s Description: S/MIME cryptographic signature
Re: [PHP] Re: order of elements in $_POST super global
name="bool[0][careers]" might do what you want... it's really quite difficult to say without a concrete example... On Thu, June 8, 2006 10:20 am, Ben Liu wrote: > I probably should add some more details to my question: > > The names of the form checkboxes could be changed from ie: > bool_careers, bool_speaking, bool_internship, etc. to a single array > bool_questions[], for instance. The problem with that is that I am > using the form checkbox names later to process related data. > Specifically, I am building a user-input based query. So if the form > checkbox names are the same as the names of the variables in the > database, it makes for easy creating of the query, since the names > become associative keys in the $_POST array. I iterate through the > $_POST array, checking each for true/false state. If the checkbox is > checked, I add the associative key name to the query. Eliminating the > checkbox names in favor of a numerical key only would make this more > complicated. > > - Ben > > On 6/8/06, Barry <[EMAIL PROTECTED]> wrote: > >> 1. Use Keys in your form like a[1],a[2] >> 2. order the array by usort (alphabetically or whatever u prefer) >> >> that way => 2: yes it is. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: order of elements in $_POST super global
On Fri, June 9, 2006 7:34 am, Ben Liu wrote: > The basic problem is that the way a $_POST variable gets processed is > in the order it is in on the original form. This is an undocumented behaviour, almost for sure. Your script shouldn't rely on it, even if a skillion other scripts do. :-) -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] RE: order of elements in $_POST super global
On 09 June 2006 13:55, Mindaugas L wrote: > Hello, > " > The basic problem is that the way a $_POST variable gets processed is > in the order it is in on the original form. " > I think it's very natural behaviour. > > But be careful with associative arrays, I think before version > 5.12.there was a bug, if you want that PHP makes an associative array > of form elements. http://bugs.php.net/bug.php?id=37276 That's a strictly 5.1.3 bug -- everything's fine in 5.1.2 and previous, and in 5.1.4 (and subsequent versions!). Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: order of elements in $_POST super global
Hi Mindaugas, I wasn't trying to imply that there was something wrong or illogical about the way the $_POST variable is processed. It is logical that it processes in the order listed in html. Perhaps I should have said, "my problem" rather than "the problem". I'll be sure to read about the bug you mention. Thanks. - BL On Jun 9, 2006, at 8:54 AM, Mindaugas L wrote: Hello, " The basic problem is that the way a $_POST variable gets processed is in the order it is in on the original form. " I think it's very natural behaviour. But be careful with associative arrays, I think before version 5.12.there was a bug, if you want that PHP makes an associative array of form elements. http://bugs.php.net/bug.php?id=37276 Enjoy Mindaugas On 6/9/06, Ben Liu <[EMAIL PROTECTED]> wrote: Hi Mike, Thanks for pointing that out. I hadn't thought of it but you are right. But I still don't think this addresses the issue of ordering. The basic problem is that the way a $_POST variable gets processed is in the order it is in on the original form. If you want to present the fields in one manner and process them in a different order, you have to either use styling tricks to change the presentation order or use some post processing to change the order of the array or use potentially a ton of "if" statements or as Joao suggests, a long "switch" statement. Anyhow, thanks for the tip. - BL On 6/9/06, Ford, Mike <[EMAIL PROTECTED]> wrote: > I know you've found another (perfectly good) solution already, but I just wanted to point out that you could have used the above scenario with the names as the array indexes, like: > >value="yes"> > > The above input shows up in $_POST['bool_questions'] ['first_name'], so you could iterate over $_POST['bool_questions'] to get the result you want. > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Mindaugas smime.p7s Description: S/MIME cryptographic signature
[PHP] Re: order of elements in $_POST super global
Hello, " The basic problem is that the way a $_POST variable gets processed is in the order it is in on the original form. " I think it's very natural behaviour. But be careful with associative arrays, I think before version 5.12.there was a bug, if you want that PHP makes an associative array of form elements. http://bugs.php.net/bug.php?id=37276 Enjoy Mindaugas On 6/9/06, Ben Liu <[EMAIL PROTECTED]> wrote: Hi Mike, Thanks for pointing that out. I hadn't thought of it but you are right. But I still don't think this addresses the issue of ordering. The basic problem is that the way a $_POST variable gets processed is in the order it is in on the original form. If you want to present the fields in one manner and process them in a different order, you have to either use styling tricks to change the presentation order or use some post processing to change the order of the array or use potentially a ton of "if" statements or as Joao suggests, a long "switch" statement. Anyhow, thanks for the tip. - BL On 6/9/06, Ford, Mike <[EMAIL PROTECTED]> wrote: > I know you've found another (perfectly good) solution already, but I just wanted to point out that you could have used the above scenario with the names as the array indexes, like: > > > > The above input shows up in $_POST['bool_questions']['first_name'], so you could iterate over $_POST['bool_questions'] to get the result you want. > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Mindaugas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: order of elements in $_POST super global
Hi Mike, Thanks for pointing that out. I hadn't thought of it but you are right. But I still don't think this addresses the issue of ordering. The basic problem is that the way a $_POST variable gets processed is in the order it is in on the original form. If you want to present the fields in one manner and process them in a different order, you have to either use styling tricks to change the presentation order or use some post processing to change the order of the array or use potentially a ton of "if" statements or as Joao suggests, a long "switch" statement. Anyhow, thanks for the tip. - BL On 6/9/06, Ford, Mike <[EMAIL PROTECTED]> wrote: I know you've found another (perfectly good) solution already, but I just wanted to point out that you could have used the above scenario with the names as the array indexes, like: The above input shows up in $_POST['bool_questions']['first_name'], so you could iterate over $_POST['bool_questions'] to get the result you want. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: order of elements in $_POST super global
On 08 June 2006 16:20, Ben Liu wrote: > I probably should add some more details to my question: > > The names of the form checkboxes could be changed from ie: > bool_careers, bool_speaking, bool_internship, etc. to a single array > bool_questions[], for instance. The problem with that is that I am > using the form checkbox names later to process related data. > Specifically, I am building a user-input based query. So if the form > checkbox names are the same as the names of the variables in the > database, it makes for easy creating of the query, since the names > become associative keys in the $_POST array. I iterate through the > $_POST array, checking each for true/false state. I know you've found another (perfectly good) solution already, but I just wanted to point out that you could have used the above scenario with the names as the array indexes, like: The above input shows up in $_POST['bool_questions']['first_name'], so you could iterate over $_POST['bool_questions'] to get the result you want. To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: order of elements in $_POST super global
yes, as I said in an earlier post, I used to have 30 if ($key) statements, one for each checkbox, in the order I wanted to write them in. This is not optimal because: (a) it is a ton of code, (b) the checkboxes could in the future increase in number, change order, etc. and then I would have to recode this section to match. The solution I used to solve this was to remove the table, put all the checkboxes into elements, float them into columns and now they appear in the same order as before when they were in a table, but they write to the $_POST variable in the order that I want. - Ben On 6/8/06, João Cândido de Souza Neto <[EMAIL PROTECTED]> wrote: I don´t test, but it can works fine: foreach ($_POST as $key => $data) { switch ($key) { case "aaa" $query.="$key, "; break; case "bbb" $query.="$key, "; break; case "ccc" $query.="$key, "; break; } } In switch you can order by ordening the cases. Hope help. - Original Message - From: "Ben Liu" <[EMAIL PROTECTED]> To: "João Cândido de Souza Neto" <[EMAIL PROTECTED]> Cc: Sent: Thursday, June 08, 2006 1:14 PM Subject: Re: [PHP] Re: order of elements in $_POST super global Hello João, You are right that the $_POST variable does not receive anything for unchecked boxes. I didn't realize that. But I still need the foreach loop for other reasons: So it looks like this: foreach ($_POST as $key => $data) { $query.="$key, "; } Instead of this: foreach ($_POST as $key => $data) { if ($data) $query.="$key, "; } Thanks, Ben On 6/8/06, João Cândido de Souza Neto <[EMAIL PROTECTED]> wrote: > Since i remember, when a for POST is sent and a checkbox is not checked, > php > not receive this $_POST variable. > > That is, i think you don´t need to use a foreach to know if checkbox was > checked or not. > > Am i wrong? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: order of elements in $_POST super global
Hello João, You are right that the $_POST variable does not receive anything for unchecked boxes. I didn't realize that. But I still need the foreach loop for other reasons: So it looks like this: foreach ($_POST as $key => $data) { $query.="$key, "; } Instead of this: foreach ($_POST as $key => $data) { if ($data) $query.="$key, "; } Thanks, Ben On 6/8/06, João Cândido de Souza Neto <[EMAIL PROTECTED]> wrote: Since i remember, when a for POST is sent and a checkbox is not checked, php not receive this $_POST variable. That is, i think you don´t need to use a foreach to know if checkbox was checked or not. Am i wrong? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: order of elements in $_POST super global
Since i remember, when a for POST is sent and a checkbox is not checked, php not receive this $_POST variable. That is, i think you don´t need to use a foreach to know if checkbox was checked or not. Am i wrong? ""Ben Liu"" <[EMAIL PROTECTED]> escreveu na mensagem news:[EMAIL PROTECTED] > Hello All, > > I'm using a form (method="POST") to collect 30 boolean values from the > end user using a series of checkboxes in the form. The form is > arranged in a table so that the 30 check boxes are not a long list but > rather three columns (with related items columnized). The problem is > when I iterate through the $_POST results: > > The order in which I wish to present the checkboxes to the end user is > different than the order I want to have in the $_POST super global and > subsequently when I dump that information out to a text file. > > What would be the best way to solve this? > > 1) Is there a way to present the checkboxes in a certain order, yet > have the data transmitted into the $_POST super global in a different > order? > > 2) Does it make more sense to process the $_POST super global array > and reorder the items within the array? > > 3) Some other method? > > Thanks for any advice. > > - Ben -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: order of elements in $_POST super global
Also, I need to re-order the elements in a custom manner, not simply alphabetical or reverse alphabetical or some logical way like that, more like how the original form is presented, by related fields. More specifically, the form is for a membership system, the system collects typical demographic information like home street address, home zip code, business street address, business zip code. So the form presents related fields together: business with business fields and home with home fields (in columns). The problem is the POST method writes these response into the $_POST array in a certain order: not top-to-bottom, then left-to-right as a person would read the form, but left-to right, then top-to-bottom. The simple solution would be to reorder the form checkboxes so that they matched the desired output order of the text file, but then the user is presented with a disorganized, illogical form. I could eliminate the table and just list all the checkboxes in one big list, but again that makes for an ugly, user-unfriendly form. I could perhaps use some divs to make columns and float them, I'm not sure how the $_POST method would order the responses in the $_POST array. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: order of elements in $_POST super global
I probably should add some more details to my question: The names of the form checkboxes could be changed from ie: bool_careers, bool_speaking, bool_internship, etc. to a single array bool_questions[], for instance. The problem with that is that I am using the form checkbox names later to process related data. Specifically, I am building a user-input based query. So if the form checkbox names are the same as the names of the variables in the database, it makes for easy creating of the query, since the names become associative keys in the $_POST array. I iterate through the $_POST array, checking each for true/false state. If the checkbox is checked, I add the associative key name to the query. Eliminating the checkbox names in favor of a numerical key only would make this more complicated. - Ben On 6/8/06, Barry <[EMAIL PROTECTED]> wrote: 1. Use Keys in your form like a[1],a[2] 2. order the array by usort (alphabetically or whatever u prefer) that way => 2: yes it is. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: order of elements in $_POST super global
Ben Liu schrieb: Hello All, I'm using a form (method="POST") to collect 30 boolean values from the end user using a series of checkboxes in the form. The form is arranged in a table so that the 30 check boxes are not a long list but rather three columns (with related items columnized). The problem is when I iterate through the $_POST results: The order in which I wish to present the checkboxes to the end user is different than the order I want to have in the $_POST super global and subsequently when I dump that information out to a text file. What would be the best way to solve this? 1) Is there a way to present the checkboxes in a certain order, yet have the data transmitted into the $_POST super global in a different order? 2) Does it make more sense to process the $_POST super global array and reorder the items within the array? 3) Some other method? Thanks for any advice. - Ben 1. Use Keys in your form like a[1],a[2] 2. order the array by usort (alphabetically or whatever u prefer) that way => 2: yes it is. -- Smileys rule (cX.x)C --o(^_^o) Dance for me! ^(^_^)o (o^_^)o o(^_^)^ o(^_^o) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php