CodeIgniter Forums
IF $class = ($value->status == "done") HOW? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: IF $class = ($value->status == "done") HOW? (/showthread.php?tid=67236)



IF $class = ($value->status == "done") HOW? - Fraps900 - 01-30-2017

Guys, I would like to use this to show three different colors on my gantt chart:

Code:
$class = ($value->status == "done") ? "#336699" : "";

Currently I have one color, that shows done tasks
Is this possible to use something like:


Code:
if $class = ($value->status == "open") ? "#536699" : "";
if $class = ($value->status == "in_progress") ? "#796699" : "";
if $class = ($value->status == "done") ? "#336699" : "";

But of course above code doesn't work. Can You please help me to create some "if" listing for that?


RE: IF $class = ($value->status == "done") HOW? - neuron - 01-30-2017

https://davidwalsh.name/php-shorthand-if-else-ternary-operators


RE: IF $class = ($value->status == "done") HOW? - ivantcholakov - 01-30-2017

I would propose another way. Make the following helper function, PHP:

Code:
function get_task_status_class($status) {

    $status = strtolower(str_replace('_', '-', $status));

    if (!in_array($status, array(
        'open',
        'in-progress',
        'done'
    ))) {

        return '';
    }

    return $status;
}

It is to be used within the views, for example:

Code:
<span class="task-name <?php echo get_task_status_class($status); ?>"><?php echo html_escape($task_name); ?></span>

And add to your CSS:

Code:
.task-name.open {
    color: #536699;
}

.task-name.in-progress {
    color: #796699;
}

.task-name.done {
    color: #336699;
}



RE: IF $class = ($value-&gt;status == &quot;done&quot;) HOW? - Wouter60 - 01-30-2017

You can use the switch conditional statement:

PHP Code:
switch ($value->status) {
  case 
"open":
     
$class "#536699";
     break;
  case 
"in_progress" :
     
$class "#796699";
     break;
   case 
"done" :
     
$class "#336699";
     break;
   default : 
      
//do nothing




RE: IF $class = ($value->status == "done") HOW? - matt... - 01-30-2017

Hello,

Why not that :
$class = ($value->status == 'open') ? "#536699" : (($value->status == 'in_progress') ? "#796699" : (($value->status == 'done') ? "#336699" : ""));

Matt.


RE: IF $class = ($value-&gt;status == &quot;done&quot;) HOW? - dave friend - 01-31-2017

This will also work.

PHP Code:
if($value->status === "open")
 
 {
 
   $class "#536699" 
  
}
elseif(
$value->status === "in_progress")
 
 {
 
   $class "#796699";
 
 }
else
 
 {
 
   $class "#336699";
 
 

I think that @ivantcholakov suggestion of using a CSS file instead of hardcoded color values is a very good idea. Not that I set a good example of that here  Angel


RE: IF $class = ($value-&gt;status == &quot;done&quot;) HOW? - Wouter60 - 01-31-2017

@Fraps900, what's your feedback on the suggestions we gave you?