...

View Full Version : Loop to catch Post vars???



Loic
03-06-2009, 12:41 AM
Hi all, I just wanted to know if something was possible in php and if so how? What I want to do is have a loop to get post variables and put them into variables. Basically reduce this code below into a few lines. I hate having to have so many almost identical lines when I am coding from what I have found is that there is almost always a way to use a loop to reduce such things.


$returnNumber = isset($_REQUEST["returnNumber"]) ? $_REQUEST["returnNumber"] : "";
$quoted = isset($_REQUEST["quoted"]) ? $_REQUEST["quoted"] : "";
$quotedBy = isset($_REQUEST["quotedBy"]) ? $_REQUEST["quotedBy"] : "";
$company = isset($_REQUEST["company"]) ? $_REQUEST["company"] : "";
$firstName = isset($_REQUEST["firstName"]) ? $_REQUEST["firstName"] : "";
$lastName = isset($_REQUEST["lastName"]) ? $_REQUEST["lastName"] : "";
$address = isset($_REQUEST["address"]) ? $_REQUEST["address"] : "";
$suburb = isset($_REQUEST["suburb"]) ? $_REQUEST["suburb"] : "";
$state = isset($_REQUEST["state"]) ? $_REQUEST["state"] : "";
$postCode = isset($_REQUEST["postCode"]) ? $_REQUEST["postCode"] : "";
$phone = isset($_REQUEST["phone"]) ? $_REQUEST["phone"] : "";
$received = isset($_REQUEST["received"]) ? $_REQUEST["received"] : "";
$returned = isset($_REQUEST["returned"]) ? $_REQUEST["returned"] : "";
$courier = isset($_REQUEST["courier"]) ? $_REQUEST["courier"] : "";
$connote = isset($_REQUEST["connote"]) ? $_REQUEST["connote"] : "";
$notes = isset($_REQUEST["notes"]) ? $_REQUEST["notes"] : "";
$product = isset($_REQUEST["product"]) ? $_REQUEST["product"] : "";
$serial = isset($_REQUEST["serial"]) ? $_REQUEST["serial"] : "";
$reason = isset($_REQUEST["reason"]) ? $_REQUEST["reason"] : "";
$faultyRepaired = isset($_REQUEST["faultyRepaired"]) ? $_REQUEST["faultyRepaired"] : "";
$sentToManf = isset($_REQUEST["sentToManf"]) ? $_REQUEST["sentToManf"] : "";
$testedOk = isset($_REQUEST["testedOk"]) ? $_REQUEST["testedOk"] : "";
$footNotes = isset($_REQUEST["footNotes"]) ? $_REQUEST["footNotes"] : "";

Fou-Lu
03-06-2009, 01:04 AM
extract (http://php.ca/manual/en/function.extract.php)


Oh yeah, make sure you use EXTR_SKIP. No and's, if's or but's.

kbluhm
03-06-2009, 01:19 AM
Also have a look at import_request_variables (http://www.php.net/import_request_variables).

The only "bad" thing I could see happening when using these two functions straight up would be, for instance, not having the variable $footNotes created if $_REQUEST['footNotes'] is not set.

Fou-Lu
03-06-2009, 01:23 AM
Also have a look at {url=http://www.php.net/import_request_variables}import_request_variables[/url].

The only "bad" thing I could see happening when using these two functions straight up would be, for instance, not having the variable $footNotes created if $_REQUEST['footNotes'] is not set.

Agreed. And to add any type of security on this, you'd need to use the skip methods to prevent evil register_globals from restructuring you're code. If you were to predefine you're variables, the skip would leave you with empty variables lol.
Or... maybe this would work:


if (isset($_POST))
{
foreach ($_POST AS $key => &$val)
{
$GLOBALS[$key] = $val;
}
}


Extract to globals. That may work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum