Let's Do It First off, let's get some data from our database. Let's assume we have a table called mytable with 3 fields: name, phonenumber, and age. Ok, we have our data from our database. Now we just need to open up our table and run a for loop for as many rows as we have and display the data. Then, we close the table. \n"; echo "
Thursday, October 11, 2007
Alternating row colors with PHP and mySQL
First Things First 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.
Let's Do It First off, let's get some data from our database. Let's assume we have a table called mytable with 3 fields: name, phonenumber, and age. Ok, we have our data from our database. Now we just need to open up our table and run a for loop for as many rows as we have and display the data. Then, we close the table. \n"; echo "Name Phone Age \n"; for($i = 0; $i < $numofrows; $i++) { $row = mysql_fetch_array($result); //get a row from our result set if($i % 2) { //this means if there is a remainder echo "\n"; } else { //if there isn't a remainder we will do the else echo " \n"; } echo "".$row['name']." ".$row['phonenumber']." ".$row['age']." \n"; echo " \n"; } //now let's close the table and be done with it echo "\n"; ?> That's it. Really. That's all there is to it. It's so simple that it is amazing. Now, get out there and fancy your tables up!
Let's Do It First off, let's get some data from our database. Let's assume we have a table called mytable with 3 fields: name, phonenumber, and age. Ok, we have our data from our database. Now we just need to open up our table and run a for loop for as many rows as we have and display the data. Then, we close the table. \n"; echo "
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment