Alternating Rows: Mysql
The 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




