Jump to content

insert IF into an array


AlexTesler

Recommended Posts

Hi people I am new to php and I need your help.

 

I am usig libchart and want to make the bars colored based on value:

 

Original code:

 

 $this->setBarColor(array(
                    new Color(42, 71, 1),          
            ));

 

I want to make something like this:

 

 $this->setBarColor(array(

 

if ( (    condition1     ))

 

new Color(42, 171, 1),  

 

if ( (    condition2     ))

 

new Color(42, 71, 1),

 

if ( (    condition3     ))

 

new Color(42, 71, 211),

       
            ));

 

I do not know php syntax yet, so how can I do this?

 

Thank you,

Alex

Link to comment
https://forums.phpfreaks.com/topic/291953-insert-if-into-an-array/
Share on other sites

try

if (condition1) {
    $this->setBarColor(array(new Color(42, 171, 1)));
}
elseif (condition2) {
    $this->setBarColor(array(new Color(42, 71, 1)));
}
elseif (condition3) {
    $this->setBarColor(array(new Color(42, 71, 211)));
}

No not quite. The if statements will be outside of the array. You save the new color to a variable and pass that variable to setBarColor(). Example

if(condition 1)
{
   $color = new Color(42, 121, 66); // if condition 1 is met set color to this
}
elseif(condition 2)
{
   $color = new Color(98, 88, 161); // if condition 2 is met set color to this
}
elseif(condition 3)
{
   $color = new Color(24, 66, 201); // if condition 3 is met set color to this
}
else
{
   $color = new Color(42, 71, 1); // the default bar color
}

$this->setBarColor(array($color)); // pass the color to setBarColor method.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.