Large Variable Checking - How?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Large Variable Checking - How?

Post by oscardog »

I want to check 42 variables from a submitted from, they are in post data types(not that it matters..). I obvoulsy cant post down the script:

Code: Select all

 
if(isset($_POST['variable1']) {
$variable 1 = $_POST['var1'];
}
 
And then repeat that 42 times! I want it so that i can decide whether the user has put information into that field, and if they have, put the information itno a variable. If they havn't then dont set a variable, im assuming this can be done with some sort of loop? The fields are named in this convention: '4', '430', '5', '530' etc(They correspond to times of the day)

Also the variable has to be naed the same as the field(So say they enter information into '8' i want the variable to be called '8' etc)

Any ideas? :)

Oscardog
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Large Variable Checking - How?

Post by mickeyunderscore »

If the inputs follow a naming convention then you can use a variable as the $_POST index name, an example assuming fields are named "input_1" to "input_50": (will also create variables named $input_1 to $input_50)

Code: Select all

 
for($i=1; $i<=50; $i++){
     $tmp = "input_$i";
     if(isset($_POST[$tmp])){
          //create variable with contents of $tmp
          $$tmp = $_POST[$tmp];
     }
}
 
You can use the same idea to quickly generate the input form too.
Post Reply