Jump to content

Is there a quicker way to do this?


music_lp90

Recommended Posts

Hi, I just wrote this code and it seemed like there would probably be a less repetetive way of doing this, it works fine, but for future purposes I would like to know if I could have saved repeating basically the same thing.Here it is:

if ($_POST[lastName] > "") $lastname = $_POST[lastName];else $lastname = " ";if ($_POST[firstName] > "") $firstname = $_POST[firstName];else $firstname = " ";if ($_POST[otherNames] > "") $othernames = $_POST[otherNames];else $othernames = " ";if ($_POST[phone] > "")$phone = $_POST[phone];else $phone = " ";if ($_POST[cell] > "")$cell = $_POST[cell];else $cell = " ";if ($_POST[address] > "")$address = $_POST[address];else $address = " ";if ($_POST[email] > "")$email = $_POST[email];else $email = " ";

Thanks for taking a look at this! I'm sorry it doesn't seem to be formatted very well when I post it on here. I do have it a little easier to read, but for some reason it doesn't include the line breaks in the code when I post it on here.

Link to comment
Share on other sites

$checkVars = array("lastName","firstName","otherNames","phone","cell","address","email");foreach ($checkVars as $checkVar) {if ($_POST[$checkVar] != "") eval("\$" . $checkVar . " = \"" . $_POST[$checkVar] . "\";");else eval("\$" . $checkVar . " = \" \";");}

Or, if all variables in the $_POST superglobal need to be checked:

foreach ($_POST as $checkKey => $checkVar) {if ($_POST[$checkKey] != "") eval("\$" . $checkKey . " = \"" . $checkVar . "\";");else eval("\$" . $checkKey . " = \" \";");}

That should work, though I didn't test it.Hope it helps :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...