Welcome Guest, Not a member yet? Register   Sign In
Set default value for missed parameter
#1

[eluser]Unknown[/eluser]
Hi!

I was just wondering how to get around the problem of blank or null values. Say I have a function that looks like this:
Code:
function func($var1=1, $var2=2, $var3=3) {
  //some stuff
}

Which I can access at a certain url:
Code:
http://site.com/index.php/[b]controller1/func/value1/value2/value3[/b]

However, when var1 or var 2 is missing,
Code:
http://site.com/index.php/[b]controller1/func//value2/value3[/b]

The function sets $var1 = value2,
instead of setting $var1 = 1 as I have specified in the func(tion).

Is there a way around this?
#2

[eluser]obiron2[/eluser]
The only way around this would be to pass the variables as a single list with either a delimiter or key-value pair, like you would get in a $_GET request. This would make your URL look like
Code:
http://site.com/index.php/controller1/func/value1~value2~value3
or
Code:
http://site.com/index.php/controller1/func/value1~x~value2~y~value3~z

I had to do this once for an Ajax callback that needed the whole of the table data to be passed back to PHP for processing. I serialised the DOM object to JSON and attached it as a value for the 1st parameter then un-serialised in CI so that I could work with it. - If you do this you are limited to the length of the URL

Obiron
#3

[eluser]xwero[/eluser]
Like obiron2 wrote a key-value pair is the best way when all parameter segments are optional. In your controller method you then can do
Code:
function funct() // no optional parameters between the rounded brackets
{
   $pairs = $this->uri->uri_to_assoc(); // gets segments from the third one on by default

   $val1 = (isset($pairs['val1']))?$pairs['val1']:1;
   $val2 = (isset($pairs['val2']))?$pairs['val2']:2;
   $val3 = (isset($pairs['val3']))?$pairs['val3']:3;
   // do your thing
}

If you don't want key-value pairs then you have to do a lot more checking.

Code:
function func()
{
   $val1 = 1;
   $val2 = 2;
   $val3 = 3;

   if($this->uri->total_segments() == 5)
   {
      $val1 = $this->uri->segment(3);
      $val2 = $this->uri->segment(4);
      $val3 = $this->uri->segment(5);
   }
   elseif($this->uri->total_segments() == 4)
   {
     $val1 = $this->uri->segment(3);
     $val2 = $this->uri->segment(4);
   }
   elseif($this->uri->total_segments() == 4)
   {
     $val1 = $this->uri->segment(3);
   }
   // do your thing
}
I didn't add the check to verify if the value for the segment belongs to that segment but you know this would require a lot more code.
#4

[eluser]Unknown[/eluser]
Thanks for the replies. I'm surprised that there isn't any built-in function for this. I was hoping there was an obscure function I didn't know about.
I'm using Ajax too with a work-around but it isn't 'elegant'. =(


Again, thank you for the solutions you provided.
#5

[eluser]frenzal[/eluser]
you could try using $this->uri->uri_to_assoc(n) instead?




Theme © iAndrew 2016 - Forum software by © MyBB