Alternating Rows: Mysql

Alternating Rows: MysqlThe first thing we need to do is connect to the database. I always put my db connection in a universal file, that way I can just change the variables once if I need to; but for the sake of the tutorial, im putting it in this file.

//db vars
$db_host = "host"; //change to your host
$db_user = "user"; //change to your user
$db_pass = "pass"; //change to your password
$db = "db"; //change to your db

//set a GLOBALS var to connect to the database
$GLOBALS['connection'] = mysql_connect($db_host, $db_user, $db_pass)
or die( "Unable to connect to SQL server");
mysql_select_db($db)
or die( "Unable to select database");

The first thing we do is set variables for our connection. Then we connect. notice I set my connection to a $GLOBALS variable. A short explaniation of this is, you set the connection to the $GLOBALS variable, then pass that when you do your mysql query (you'll see that shortly). When the mysql is done, it looks to see if a $GLOBALS variable is already set, if it is, it uses that connection, if not, it makes a new one. This save you from having a lot of hung up mysql connections.

Next we echo out some html, really nothing to explain there, but the styles. We have classes, these are the classes we use for the alternating rows. then we have:

//Set the Rows
$class1 = "RowOne";
$class2 = "RowTwo";
$class = $class1;

This is one of the two parts to make the alternating rows. We have three variables, $class1 $class2 and $class (you can name them whatever you want, but i think $class fits what we are using them for). The first variable ($class1) is set to the first css class for the alternating row, $class2 for the second, and $class is set to $class1; you'll see why we set $class to $class1 shortly.

next we have:

//Start the table
$table = "<table>";

I do this to start the table that will contain the rows. Then when I loop throuhg the mysql result, I concatenate each row to the $table variable, creating one variable containing all the html for the table. This is just a preference of mine, I believe the code is cleaner doing it this way rather than echoing all the table html out.

Next we do our mysql query:

//Get data from mysql table
$db_qur = "select * from TABLE";
$db_res = mysql_query($db_qur, $GLOBALS['connection']);
while($db_row = mysql_fetch_assoc($db_res)){

First we set the query in $db_qur. then we run the query with $db_res (notice we call $GLOBALS['connection'] like I went over earlier) finaly we start to loop through the mysql result in the while. The next part of the mysql query is:

$table .= "<tr><td class="$class">$db_row[RowName]</td></tr>";

See, theres the $table variable again, but this time I dont set it = i set it .= this is how you concatenate. Also notice, we set the class = to $class. So as it stands right now, all the classes will be $class1 which is RowOne, which brings me to the next and main part:

if($class == $class1){
$class = $class2;
} else {
$class = $class1;
}
}

This is what alternates the rows (plus closes the while loop with the last }). Ok now for what this actually does. The first time this goes through $class = $class1, so it sets it to $class2 (which is done by the if statement) the next time, $class = $class2, so it sets it to $class1, which is done in the else statement.

Now we end the file:

$table .= "</table>";

//echo the table
echo $table;
?>

Again we concatenate the table, then we echo the table, then end the file, and wah la. you're done; unless you want to do this using arrays not mysql. If thats the case, go on to array alternating rows section


Related Articles

  • Alternating Rows: Arrays
    The first thing we do here is echo out the html, again I wont go over this because theres really nothing to explain, except the css. We have two css classes, one for each of the alternating rows.
  • Alternating Row Colors
    Theres two different ways this can be done. The first is by getting results from a mysql query, the second is going through the values of an array. Ill go over both. First the full files (ill break it up into two files, one using mysql, and one using an array)
  • Alternating Backgrounds
    Alternate the backgrounds of table rows displayed from a MySQL database.
  • Alternating Row Colors
    Learn how to alternate row colors from database output using ASP.
  • MySQL Tables
    Create, insert, update, delete, count and select rows in a database.
  • Design an Online Chat Room with PHP and MySQL
    In this article, you will learn how to design and develop a simple online chat room with PHP and MySQL. This tutorial explains every steps of the development, including both database design and PHP programming. Basic computer skills and knowledge of HTML and PHP are required. Ok, lets begin now. ....
  • PHP User Stats
    In this tutorial I am going to teach you how to make a User Stats menu in PHP and MySQL which displays: Unique hits, Unique hits today, Total hits, Total hits today. If you dont have MySQL you can get one for free here.
  • MySQL Join Tutorial
    What are joins and how do you make them?
  • Php Mailing List with no MySql
    Simple mailing list with admin panel and subscriber unsubscribing function. No MySQL needed.
  • Acquiring PHP MySQL
    PHP and MySQL are already packaged in Linux and Mac OS X OSs. However, most PHP developer are actually using Windows when developing PHP applications. This is also true to myself. I have Mandrake Linux 10 but Ill just use it for testing purposes of my finished scripts...

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.