Page 1 of 2

How to turn fld green, red, yellow depending on when due.

Posted: Sat Sep 13, 2014 5:02 pm
by shawnblc
I'm trying to get a fld turn colors depending on when something is due or not due. Example: turn yellow when something is coming due, red when it is due or past due, and green when it's good. Can someone help me out with the below code. Can't seem to get it right.

Code: Select all

   put 8 into t8Day -- the num of days
   put the seconds into tTodaySec
   put 86400 * t8Day into tTimePassed
   put  tTodaySec + tTimePassed into tDaysec
   convert tDaysec to short date
   --answer "In " && t8Day && "day we will be the" && tDaysec
   
   put 7 into t7Day -- the num of days
   put the seconds into tTodaySec
   put 86400 * t7Day into tTimePassed
   put  tTodaySec + tTimePassed into tDaysec
   convert tDaysec to short date
   answer "In " && t7Day && "day we will be the" && tDaysec
   
   put -7 into tM7Day -- the num of days
   put the seconds into tTodaySec
   put 86400 * tM7Day into tTimePassed
   put  tTodaySec + tTimePassed into tDaysec
   convert tDaysec to short date
   answer "In " && tM7Day && "day we will be the" && tDaysec
   
   put 1 into t1Day -- the num of days
   put the seconds into tTodaySec
   put 86400 * t1Day into tTimePassed
   put  tTodaySec + tTimePassed into tDaysec
   convert tDaysec to short date
   --answer "In " && t1Day && "day we will be the" && tDaysec

   if fld "fld1" > t8Day //if the date hasn't arrived yet stay green
   then
      set the backgroundColor of fld "fld1" to green
      exit mouseup
   end if
   
   if fld "fld1" <= t1Day //if date is date or expired turn red
   then
      set the backgroundColor of fld "fld1" to red
      exit mouseUp
   end if
   
   if fld "fld1" >= tM7Day //if date is within 7 days of coming due turn yellow
   then
      set the backgroundColor of fld "fld1" to yellow
      exit mouseUp
   end if
   
   if fld "fld1" <= tM8Day 
      then
      set the backgroundColor of fld "fld1" to red
      exit mouseUp
      
      if fld "fld1" > tM8Day 
         then
         set the backgroundColor of fld "fld1" to green
         exit mouseUp
      end if

Re: How to turn fld green, red, yellow depending on when due

Posted: Sat Sep 13, 2014 6:43 pm
by Simon
Hi shawnblc,
You don't load tM8day but that is something else.
I think the problem is you don't have fld "fld1" set to opaque??

Simon

Re: How to turn fld green, red, yellow depending on when due

Posted: Sat Sep 13, 2014 7:22 pm
by shawnblc
fld "fld1" is set to opaque. I can get it to change green, but no matter what date is entered it will stay green.

Re: How to turn fld green, red, yellow depending on when due

Posted: Sat Sep 13, 2014 7:33 pm
by Simon
OK maybe I'm using it incorrectly.
If I put 1 into fld "fld1" it turns red
put 5 it turns yellow
put 9 it turns green

What sort of values are you using? actual dates?

Simon

Re: How to turn fld green, red, yellow depending on when due

Posted: Sat Sep 13, 2014 7:39 pm
by shawnblc
Simon wrote:OK maybe I'm using it incorrectly.
If I put 1 into fld "fld1" it turns red
put 5 it turns yellow
put 9 it turns green

What sort of values are you using? actual dates?

Simon
I'm using dates. Example: 9/5/14

Re: How to turn fld green, red, yellow depending on when due

Posted: Sat Sep 13, 2014 7:59 pm
by Simon
OK. You are comparing whole number with dates, that isn't going to fly.

Code: Select all

   put fld "fld1" into tCompareDate
   convert tCompareDate from short english date to seconds
Now the fld is in seconds
Then don't convert tDaysec

Code: Select all

put  tTodaySec + tTimePassed into t8Day--note I changed it from tDaysec
then the rest of them as well.
Now all comparisons will be done in seconds.

Code: Select all

if tCompareDate  > t8Day //if the date hasn't arrived yet stay green
   then...
How does that look?

Simon

Re: How to turn fld green, red, yellow depending on when due

Posted: Sat Sep 13, 2014 8:04 pm
by shawnblc
I'll give it a shot within the next couple of hours. Thank you for the response. I'll update this thread and let ya know. Thanks again.

Re: How to turn fld green, red, yellow depending on when due

Posted: Sun Sep 14, 2014 3:13 pm
by shawnblc
Still not working how I'd expect it to. I'll post my code again here in shortly. Just wanted to update the thread for now. Give me 30 to 60 minutes and I'll post updated code.

Re: How to turn fld green, red, yellow depending on when due

Posted: Sun Sep 14, 2014 3:33 pm
by Klaus
Hi Shawn,
shawnblc wrote:Give me 30 to 60 minutes and I'll post updated code.
take all the time you need, we won't hold our breath! 8)

:D


Best

Klaus

Re: How to turn fld green, red, yellow depending on when due

Posted: Sun Sep 14, 2014 8:54 pm
by jacque
Besides comparing seconds rather than dates, the script also doesn't actually populate the variables t8Dat, t7Day, etc. It calculates the values but doesn't use those values, so the "if" statements won't work.

Also, if you use the if - else if - end if structure you won't need all the "exit mouseup" statements because only one line of the the "if" structure will ever execute. The same thing could also be done using a switch structure, but new users are generally more familiar with "if"s and it's okay to use that.

There is a lot of repetition in the calculation of the "day" variables, you could reduce that to 5 lines. I was able to write the entire handler in 17 lines, but I'm not sure if you want the answer or you would rather work it out yourself. Holler if you want me to post it.

Edit: I just noticed Simon told you about the variables already. He's right, and I should have read the thread more thoroughly.

Re: How to turn fld green, red, yellow depending on when due

Posted: Sun Sep 14, 2014 10:37 pm
by shawnblc
Thanks for the added input. Work got in the way of my livecode learning. :) I'll update once I try some of the suggestions.

Re: How to turn fld green, red, yellow depending on when due

Posted: Mon Sep 15, 2014 12:48 am
by Simon
Hi Shawn,
Yes... I did leave a lot of the work incomplete but I hope you got the idea.
There is one other thing you should be watching out for and that is the "put the seconds..." that records the seconds right now and for a calendar type app it isn't good. You want midnight 00:00:00.
Make sense?

Simon

Re: How to turn fld green, red, yellow depending on when due

Posted: Mon Sep 15, 2014 2:06 am
by shawnblc
I'm getting close, but not as close as I'd like. As you can see I switched it up a bit. If I have a due date that's say 7 days out I'd like yellow. 8 or more days out green. On the day or past the date red.

Code: Select all

on mouseUp 
   put 86400 * fld "fldAlert" into tTheAlert
   convert tTheAlert to seconds
   
   put fld "fldDue" into tDue
   convert tDue to seconds
   
   put the date into tToday
   convert tToday to seconds
   
   put tDue - tTheAlert into tDueDate
   convert tDueDate to seconds
   --answer tDueDate
   
   if tToday - tTheAlert < tDueDate then
      set the backgroundColor of fld "fldDue" to yellow
   else
      if tToday + tTheAlert >= tDueDate then
         set the backgroundColor of fld "fldDue" to red
      else
         if tToday + tTheAlert < tDueDate then
            set the backgroundColor of fld "fldDue" to green
         else
            if tToday = tDueDate then
               set the backgroundColor of fld "fldDue" to red
               end if
         end if 
      end if
      end if
end mouseUp
   

Re: How to turn fld green, red, yellow depending on when due

Posted: Mon Sep 15, 2014 2:24 am
by Simon
Hi Shawn,
Try it without the "else" I think you'll get what you want.
(replace them with "end if")

Simon
edit: Nice!
put the date into tToday
convert tToday to seconds

Re: How to turn fld green, red, yellow depending on when due

Posted: Mon Sep 15, 2014 2:32 am
by shawnblc
Simon wrote:Hi Shawn,
Try it without the "else" I think you'll get what you want.
(replace them with "end if")

Simon
edit: Nice!
put the date into tToday
convert tToday to seconds

hmm. Still working with it, did as you suggested above, now I'm not getting the warning (yellow).