Alternating row colors with PHP and mySQL
(Page 1 of 2 )
A quick tutorial explaining how to alternate table row colors using PHP and mySQL.
Why am I writing this? Well, frankly I am tired of answering the same question over and over and would like a place to just point people for an answer. Besides that, I am guessing that there are other people who can benefit from this that just aren't asking the question.
The examples in this tutorial are using PHP and mySQL. But, the theory can easily be applied to any other programming language or database system. Just use your noodle (that's your brain) and I am sure you can figure it out.
Ok, the first thing we are going to do is get our data from our database. I'm not going to go into alot of detail here. I will just show you the code. Next we will display the data. It's as simple as that.
The whole key to displaying the data in the way we want is the modulus operator (%). What does the modulas operator do you ask? It simply gives us the remainder of a division. So, 3 % 2 returns 1, 3 % 3 returns 0. Easy huh? Ok, on with the code to do this.
Next: Let's Do It >>
More Database Articles Articles
More By Matt Wade