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

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm

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

Post by shawnblc » Sat Sep 13, 2014 5:02 pm

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

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

Post by Simon » Sat Sep 13, 2014 6:43 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm

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

Post by shawnblc » Sat Sep 13, 2014 7:22 pm

fld "fld1" is set to opaque. I can get it to change green, but no matter what date is entered it will stay green.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

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

Post by Simon » Sat Sep 13, 2014 7:33 pm

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 used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm

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

Post by shawnblc » Sat Sep 13, 2014 7:39 pm

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

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

Post by Simon » Sat Sep 13, 2014 7:59 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm

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

Post by shawnblc » Sat Sep 13, 2014 8:04 pm

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.

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm

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

Post by shawnblc » Sun Sep 14, 2014 3:13 pm

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.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

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

Post by Klaus » Sun Sep 14, 2014 3:33 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

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

Post by jacque » Sun Sep 14, 2014 8:54 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm

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

Post by shawnblc » Sun Sep 14, 2014 10:37 pm

Thanks for the added input. Work got in the way of my livecode learning. :) I'll update once I try some of the suggestions.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

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

Post by Simon » Mon Sep 15, 2014 12:48 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm

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

Post by shawnblc » Mon Sep 15, 2014 2:06 am

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
   

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

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

Post by Simon » Mon Sep 15, 2014 2:24 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm

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

Post by shawnblc » Mon Sep 15, 2014 2:32 am

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).

Post Reply