Re: Re: order of elements in $_POST super global

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

 



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:

<snip>
		<input type="checkbox" value="1" name="prefix">Prefix</input><br />
<input type="checkbox" value="1" name="first_name">First Name</ input><br /> <input type="checkbox" value="1" name="middle_name">Middle Name</ input><br /> <input type="checkbox" value="1" name="last_name">Last Name</ input><br />
		<input type="checkbox" value="1" name="suffix">Suffix</input><br />
<input type="checkbox" value="1" name="home_addr1">Home Address (Street)</input><br /> <input type="checkbox" value="1" name="home_addr2">Home Address 2</ input><br />
</snip>

Since the form fields names correspond to mysql field names rather than just a plain index array, I can use the keys thus:

<snip>

$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);

</snip>
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 <barry@xxxxxxxxxxxxxx> 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



Attachment: smime.p7s
Description: S/MIME cryptographic signature


[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