Calc to change cell colour in List View

mamies

Member
Hi All,

I currently have a calc cell which will be used to determine the colour of the cell. I have the colour coding of the cell working as I have used it before and I can get it to work in this case.

My issue is when I use the following formula
Code:
$element = '{data_ponds___next_volume_check}';
$olddate = strtotime("$element");
$new = date('d-m-Y');
$newdate = strtotime("$new");
if ($olddate > $newdate) {return "green";}
elseif ($olddate < $newdate) {return "red";}
elseif ($olddate == $newdate) {return "orange";}

It always returns green. It seems to not like the $element variable and includes colour_code (the element name) before the output.

Any ideas?
 
You won't have 'any idea' until you know what value is being returned by $element.
Your first step in the debugging process would be to just return that.
PHP:
$element = '{data_ponds___next_volume_check}';
return $element;
If data_ponds___next_volume_check is in the form of a mysql date then it will be a date (string) in the format 'Y-m-d H:m:s' - which is already OK to be used for greater and lesser than string comparison and there is no need for the strtotime() conversion.

You could either use the php DateTime() function to create just the 'date' part of the date/time - or use a simple substr() function on it - if all you are worried about is the date part of the date/time.

Also, there is no need to even be concerned about using that final elseif. (If it's not greater than - and it's not less than - it must be equal to.)

So, assuming '{data_ponds___next_volume_check}' is already a date (string) in the form 'Y-m-d H:m:s', this code would work
PHP:
$element = '{data_ponds___next_volume_check}';
$olddate = substr($element,0,10);
$newdate = date('Y-m-d');
if ($olddate > $newdate) {return "green";}
elseif ($olddate < $newdate) {return "red";}
else {return "orange";}
 
I have just tried Bauer code and that works fine for me. I am trying to achieve the same thing although with months. if greater than 2 months show red.
 
if i was to do it for 2 months, will this code work.
$element = '{prbi___TCDate}';
$olddate = substr($element,0,10);
$aftertwomonths = strtotime('60 day',strtotime($olddate));
if ($olddate > $aftertwomonths) {return "Red";}
elseif ($olddate < $aftertwomonths) {return "Green";}
else {return "orange";}
 
I just got it to work for month
here the code if any one needs it.
$element = '{prbi___TCDate}';
$olddate = substr($element,0,10);
$date = date('Y-m-d', strtotime("$date +1 month"));
if ($olddate > $date) {return "red";}
elseif ($olddate < $date) {return "green";}
else {return "orange";}
 
Just FYI, you should get in the habit of using some kind of unique prefix for any variables you create in eval'ed code, to avoid stomping on existing variables inside Fabrik. So for instance, it's entirely likely than we may have a variable called $element, and that your code would reset it, which could have nasty side effects.

So best to get in the habit of using some standard "slug" as a prefix for your variables (kind of like the J! database prefix slug), which is something unique we are unlikely to use. So maybe $k21_element, $k21_date, etc.

We have tried various approaches to avoid this variable "scope" issue when eval'ing code, but it's really tough to work round.

One could argue that we should use our own unique slug on all our variables, to avoid this issue ... but we didn't, and it'd be too dangerous to try and globally change things to do that now. Unfortunately not as easy as just doing wholesale search & replace.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top