• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
initialize variables

#1
[eluser]Unknown[/eluser]
Wondering if CI has a function similar to the Coldfusion CFPARAM or even the Joomla mosGetParam function.

Basically it would give an easier syntax than:

$var1 = isset($variable2) ? $variable2 : '';

Maybe something like
$var1 = setVar($nameofvar, 'default value if non-existant');

thanks

#2
[eluser]jedd[/eluser]
The form helper provides something vaguely related, but it's very much a niche case, and doesn't really do what you describe here.

Where, in CF or Joomla, are the defaults for each variable stored? It sounds like it's actually more work to set up the defaults for different variables and have the additional function, but I'd like to know more.

#3
[eluser]Colin Williams[/eluser]
I typically use a very generic Variable library which works like config_item with the addition of the "default if not existent" argument.

Code:
function get_variable(string $variable_key, mixed $default_value)

jedd, the values are in a 'variable' table in the database, and the "defaults" are essentially provided right there in the function call. Like the OP mentioned, the purpose is to avoid unnecessary IF/ELSE blocks everywhere.

#4
[eluser]jedd[/eluser]
Ah, okay. I thought the defaults were being pulled in from elsewhere, rather than the new values - and wasn't sure if they meant for the data to come in from a config file.

I can't think of where / how I'd use such an approach, but I can start to see how it might be useful.

#5
[eluser]Unknown[/eluser]
A perfect example is with a URL variable. Sometimes it may be there, sometimes not so instead of having an if block to test if it exists, you would use this type of function.

getVar($_GET['myvar'], 'default value');

It's a simple function that tests whether the variable exists and assigns it a default value if it doesn't so that you don't get warnings that "this variable does not exists".

#6
[eluser]jedd[/eluser]
Ah, okay. I use POST rather than GET, and use the CI form helper function set_value() as I alluded to above:
Code:
$first_name_spec = array (  "name" => "first_name",
                           "value" => set_value('first_name', $member['first_name']),
                           "maxlength" => "50",
                           "size" => "20"  );


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.