PHP table color change

Posted:
in Genius Bar edited January 2014
Ok, this is a good one that i have been playing with for a while. We have a tickett tracking system at work that i developed. all ticketts that are open for a department are displayed in a dynamic table, being displayed is the time due which is enterd when the tickett is open. I want the row colors in the table to change depending on how much time is left in the tichett. so if there is less than an hour left i want the background to be red with the font in white, if there is less than 2 hours left i want the row to have a orange background with white font. all records that have more than 2 hours left are displaying white background with black font.



I have played with this for a few weeks and have gotten no where.



-AG

Comments

  • Reply 1 of 4
    1337_5l4xx0r1337_5l4xx0r Posts: 1,558member
    Sort the db results based on time 'til expiry, then (kludge that immediately comes to mind) create a mammoth if/then function that determines colors based on time until expiry (timestamps are in seconds, so 60 = 1 min, 3600 = 1 hour):



    function tablecolour ($expire) {

    if (time("U") > ($expire - 3600)) {



    if (time("U") > ($expire - (3600 * 2))) {

    $tablecolour = FF6600;

    }



    $tablecolour = FF9900;



    } else {



    $tablecolour = FF3300;



    }

    return $tablecolour;

    }



    Wow, that is F%^&*ing ugly! It doesn't help that I can't format php in ubb forums. But hopefully you get the idea. You obviously know when the ticket expires, so subtract "n" amount of time from that to determine the colour. My code is simplified because I'm too lazy to write the entire function, but figure



    echo("<table color=\\"".tablecolour($expire_timestamp)."\\" cellpadding=\\"2\\" cellspacing=\\"0\\" border=\\"0\\" width=\\"100%\\" align=\\"center\\">"
  • Reply 2 of 4
    faust9faust9 Posts: 1,335member
    A simple if in conjunction with the time function should do the trick.



    Code:






    //pass $deadline_time as a string, or if you pass $deadline_time as a UNIX epoch you can eliminate the mktime call.

    function table_color($deadline_time)

    {

    $current_time =time(); /this reads the time stamp from the server

    $deadline_time=mktime( int hour, int minute, int second, int month, int day, int year, int is_dst); //replace the int's here with the various parts of the ticket time.



    /*deadline_time is the time, converted to epoch time, when the ticket needs to be closed.

    Since epoch time is in seconds then 1 hour is equal to 3600 seconds thus the deadline_time

    minus the current_time will yield the difference*/



    if ($deadline_time-$current_time>7200)

    {



    $some_color="black" //you might have to replace with HEX RGB value

    }



    elseif ($deadline_time-$current_time<=7200 && $deadline_time-$current_time>3600)

    {



    $some_color="orange" //you might have to replace with HEX RGB value



    }



    elseif ($deadline_time-$current_time<=3600)

    {



    $some_color="red" //you might have to replace with HEX RGB value



    }



    return $some_color

    }





  • Reply 3 of 4
    faust9faust9 Posts: 1,335member
    Quote:

    Originally posted by 1337_5L4Xx0R

    Sort the db results based on time 'til expiry, then (kludge that immediately comes to mind) create a mammoth if/then function that determines colors based on time until expiry (timestamps are in seconds, so 60 = 1 min, 3600 = 1 hour):



    function tablecolour ($expire) {

    if (time("U") > ($expire - 3600)) {



    if (time("U") > ($expire - (3600 * 2))) {

    $tablecolour = FF6600;

    }



    $tablecolour = FF9900;



    } else {



    $tablecolour = FF3300;



    }

    return $tablecolour;

    }



    Wow, that is F%^&*ing ugly! It doesn't help that I can't format php in ubb forums. But hopefully you get the idea. You obviously know when the ticket expires, so subtract "n" amount of time from that to determine the colour. My code is simplified because I'm too lazy to write the entire function, but figure



    echo("<table color=\\"".tablecolour($expire_timestamp)."\\" cellpadding=\\"2\\" cellspacing=\\"0\\" border=\\"0\\" width=\\"100%\\" align=\\"center\\">"






    Argh!! You must've posted while I was typing mine in. Here, I'll show yours a little better.



    Code:




    function tablecolour ($expire)

    {

    if (time("U") > ($expire - 3600))

    {

    if (time("U") > ($expire - (3600 * 2)))

    {

    $tablecolour = FF6600;

    }

    $tablecolour = FF9900;

    }

    else

    {

    $tablecolour = FF3300;

    }

    return $tablecolour;

    }









    [edit]Your nested tests are in the wrong order I do believe. Time > (expire-1 hour) only happens when the re is less than one hour. At that point your checking to see if it is less than two hours which it is anywaytablecolour would be set to ff66600. Then it would leave the inner if at which point the color would be set to FF9900. If you swap the time test and put the two hour test first and set the color before testing the one hour condition it would work.
  • Reply 4 of 4
    1337_5l4xx0r1337_5l4xx0r Posts: 1,558member
    Whoopsie, switch > for <
Sign In or Register to comment.