Web Analytics Made Easy -
StatCounter color-coat identical rows - CodingForum

Announcement

Collapse
No announcement yet.

color-coat identical rows

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • color-coat identical rows

    Greetings,

    Lets say we have a MySQL table called Warranty. The table could look something like this:

    +---------+---------------+-------------+-----------+
    | serial# | description | startdate | enddate |
    +---------+---------------+-------------+-----------+
    | 1234 | blahblahblah | 2000.1.1 | 2000.12.30|
    +---------+---------------+-------------+-----------+
    | 1234 | uh huh yeah | 2001.1.1 | 2001.12.30|
    +---------+---------------+-------------+-----------+
    | 5678 | something | 2006.1.1 | 2006.12.30|
    +---------+---------------+-------------+-----------+
    | 5678 | some more | 2007.1.1 | 2007.12.30|
    +---------+---------------+-------------+-----------+

    As the table shows, it is possible for one serial# to have multiple warranties, this is fine. Now lets say we want to display all of the information in the warranty table to the web browser. Doing so is quite easy via the standard methods. However, to make things easier on the users eyes, it would be helpful if each group of identical serial#'s were colored-coated with the same background color. For example, all serial#'s which are "1234" would have a background color of gray. The next group of serial#'s would have a background color of white. The next group of serial#'s would have a
    background color of gray. And so on and so forth, alternating between gray and white backgrounds.

    It is easy to make every other row gray, but I am having a difficult time figuring out how to make the background color of identical serial#'s the same. Any input would be greatly appreciated.

    Thank you for your time,

    *Nick*

  • #2
    Before your query set variables called bgcolor and lastserial.

    PHP Code:
    $bgcolor "1";
    $lastserial "0"
    Set up a function to check if a number is odd or even: (im not sure if this works or not)

    PHP Code:
    function oddCheck($number) {
         return(
    $number 1); 

    put something like this in your while statement:

    PHP Code:
    if($row['serial'] == $lastserial){
    //Same serial as previous

    //If odd make the bgcolor white if not make it grey
    if(oddCheck($bgcolor)){
         
    $background "FFFFFF";
    } else {
         
    $background "CCCCCC";
    }

    //Echo Row with background as the bgcolor

    } else {
    //Different serial number

    //Set a new number for lastserial for the next check
    $lastserial $row['serial'];

    //Add 1 to the bgcolor number
    $bgcolor $bgcolor 1;

    //If odd make the bgcolor white if not make it grey
    if(oddCheck($bgcolor)){
         
    $background "FFFFFF";
    } else {
         
    $background "CCCCCC";
    }

    //Echo Row with background as the bgcolor


    Hope this helps

    e39m5

    Comment

    Working...
    X