CodeIgniter Forums
Ternary usage - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Ternary usage (/thread-37567.html)



Ternary usage - El Forum - 01-13-2011

[eluser]Todlerone[/eluser]
Hello everyone and TY in advance. I'm trying to use a ternary condition to establish some output in a view. However, I keep getting errors. I have three possible prefixes to a word. What I'm trying to do is determine if each one is set in the database then append them together with a space between them. Example: Lt Sup Occipital or Lt Ant Occipital.

My variables are sagittal, axial or coronal. Here is a sample of what I have tried in my view.

Code:
<?php $location = ($plan['sagittal']) ? $plan['sagittal']." " : //nothing ?>
<?php $location .= ($plan['axial']) ? $plan['axial']." " : //nothing ?>
<?php $location .= ($plan['coronal']) ? $plan['coronal']." " : //nothing ?>
....then echo out $location

TY for any help


Ternary usage - El Forum - 01-13-2011

[eluser]Johan André[/eluser]
try ''; after the :


Ternary usage - El Forum - 01-13-2011

[eluser]cideveloper[/eluser]
I think you have to put the else statement after the ":"


Code:
<?php
$location = ($plan['sagittal']) ? $plan['sagittal']." " : "";
$location .= ($plan['axial']) ? $plan['axial']." " : "";
$location .= ($plan['coronal']) ? $plan['coronal']." " : "";
?>