Page 1 of 1

create variable name using current variable name and string

Posted: Sun Dec 06, 2009 8:14 pm
by mattmill30
Hi,

I'm looking to create a variable with the name of a variable currently in use, with the word 'error' appended to the end.

Any ideas how i'd do this?

What i've tried so far

Code: Select all

 
     $username = 1;
     $mandatory_fields = array($username,$password);
     for ($i=0; $i<=2; $i++) {
          if (!(isset($mandatory_fields[$i]))) {
               $mandatory_fields[$i].'error' = TRUE;
          }
     }
 
Basically this will be used as validation, in reality it will check POST variables and check that they have all been filled in.

Looking at the for loop:
First loop: Nothing will happen because it doesn't meet the necessary if statement clause. I.E. the first element in the array exists, because its $username.
Second loop: create a variable by concatenating the variable name, which is $password, with the string "error", and then set the variable to boolean TRUE. So the loop generates $usernameerror=TRUE because $password hasn't been set.

Hope that makes sense.

If anyone can think of a better way of doing it, i'd be greatful for any tips.

Thanks for any help in advance,

Matthew Millar

Re: create variable name using current variable name and string

Posted: Sun Dec 06, 2009 8:43 pm
by AliJ

Code: Select all

#
#      $username = 1;
#      $mandatory_fields = array($username,$password);
#      for ($i=0; $i<=2; $i++) {
#           if (!(isset($mandatory_fields[$i]))) {
#                $mandatory_fields[$i].'error' = TRUE;
#           }
#      }
#  
Hello,
Why don't you make a separate array for '$password' if you get what I mean

Code: Select all

$mandatory_field = array($username,);
and then

Code: Select all

$mandatory_field = array($password,);
Do you understand?Sorry if I'm not very good at explaining.
Hope this helps anyway.

Ali J

Re: create variable name using current variable name and string

Posted: Sun Dec 06, 2009 8:45 pm
by Weiry
You could convert your array into a multidimensional associative array.

Code: Select all

$username = 1;
     $mandatory_fields = array(
          "username" => array( "check" => $username, "error" => false), 
          "password" => array( "check" => $password, "error" => false)
     );
     foreach($mandatory_fields as $field)
          if (!(isset($field['check']))) {
               $field['error'] = TRUE;
          }
     }
Then just check accordingly.

Re: create variable name using current variable name and string

Posted: Mon Dec 07, 2009 2:39 am
by mattmill30
Hi AIIJ,

Thanks for getting back to me, the lack of the password field, was a test query, so that i could explain how i wanted the script to behave, when criteria is/isn't met.

I think Weiry has hit the nail on the head, but thats for the help :D

Hi Weiry,

That certainly looks like the answer, Thanks :D

I'm glad it wasn't something really obvious with my code :wink:

Its several levels more complex than what i was trying :oops:

I'll implement it later,

Thanks,

Matthew Millar