Jump to content

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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.