CodeIgniter Forums
Conditional in $this->table->add_row() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Conditional in $this->table->add_row() (/showthread.php?tid=36942)



Conditional in $this->table->add_row() - El Forum - 12-21-2010

[eluser]skribe[/eluser]
Hey folks:

New to codeigniter and I haven't coded much in the last 10 years so my aged brain is rusty. Can I drop a longhand conditional into $this->table->add_row()?
Code:
$this->table->add_row(
                      if($p->gender=='1')
                      {'Male'} else {'Female'},

                      switch($p->age) {
                          case 0:
                             '17 and younger';
                             break;
                          case 1:
                             '18-25';
                              break;
                          default:
                              'Old Fart';
                      }
);

I'm getting Parse error: syntax error, unexpected T_SWITCH and T_IF errors when using longhand. Shorthand if/else work.

If I can't do this can you suggest an alternate solution?

Cheers,

skribe


Conditional in $this->table->add_row() - El Forum - 12-21-2010

[eluser]cahva[/eluser]
No, you cant use the longhand version inside that. Just assign those to variables. Its much cleaner that way too:
Code:
$gender = ($p->gender == 1) ? 'Male' : 'Female';

switch ($p->age)
{
    case 0:
        $age = '17 and younger';
        break;
        
    case 1:
        $age = '18-25';
        break;
        
    default:
        $age = 'Old fart';
        break;
}

$this->table->add_row($gender,$age);



Conditional in $this->table->add_row() - El Forum - 12-22-2010

[eluser]skribe[/eluser]
Thanks, mate. That worked a treat.