WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Alternating row colors with PHP and mySQL
Categories : PHP, Databases, MySQL, HTML and PHP
codewalkers
codewalkers
Date : 2002-12-21
Grade : 0 of 5 (graded 0 times)
Viewed : 12357
Search : More Articles by codewalkers
Action : Grade This Article
Tools : My Favotite Articles


  Submit your own code examples 
 


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.


<?PHP
$db
= mysql_connect("host","username","password") or die("Problem connecting");
mysql_select_db("dbname") or die("Problem selecting database");
$query = "SELECT * FROM mytable ORDER BY name";
$result = mysql_query($query) or die ("Query failed");
//let's get the number of rows in our result so we can use it in a for loop
$numofrows = mysql_num_rows($result);
?>


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.


<?PHP
echo "<TABLE BORDER=\"1\">\n";
echo
"<TR bgcolor=\"lightblue\"><TD>Name</TD><TD>Phone</TD><TD>Age</TD></TR>\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 "<TR bgcolor=\"yellow\">\n";
} else {
//if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo
"<TD>".$row['name']."</TD><TD>".$row['phonenumber']."</TD><TD>".$row['age']."</TD>\n";
echo
"</TR>\n";
}
//now let's close the table and be done with it
echo "</TABLE>\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!









Multicolumn Output from a Database with PHP
Categories : PHP, Databases, HTML and PHP, MySQL
How To add paging (Pagination) with PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP
PHP, MySQL and Authentication 101
Categories : PHP, Databases, MySQL, Authentication
Building A Persistent Shopping Cart With PHP and MySQL
Categories : PHP, MySQL, Databases, Ecommerce
Miles To Go Before I Sleep...
Categories : PHP, Calendar, Databases, MySQL
Simple Connection to MySQL with PHP
Categories : PHP, MySQL, Databases
Date Arithmetic With MySQL
Categories : PHP, Databases, MySQL, Date Time
Time Is Money Part 1 of 2 - Designing and implementing a Web-based application
Categories : PHP, Databases, MySQL, Complete Programs
tracking where and what on your site people are clicking
Categories : PHP, MySQL, HTML and PHP, HTML
User identification using cookies in PHP and MySQL
Categories : PHP, Databases, MySQL, Cookies
PHP and MySQL News with Comments
Categories : PHP, Databases, MySQL
Custom MySQL-functions
Categories : Databases, MySQL, PHP, PHP Functions
Saving Images in MySQL
Categories : MySQL, PHP, Graphics, Databases
Watching The Web
Categories : PHP, Databases, MySQL, HTTP, MD5
Referer Statistics
Categories : PHP, MySQL, HTTP, Databases