PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Alternating Row Colors

Alternating Row Colors

Browse Pages: << < 1  2  3 

Alternating Rows: ArraysThe 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.

Now we make the array:

//Build the array
$array = array("value1", "value2", "value3", "value4", "value5");

All this does is creats the array. This is a very basic array, if you dont know too much about arrays, Ill give a short break down. An array is a variable used to store mulitiple values. If you were to break this array apart it would look like this:

$array[value1]
$array[value2]
$array[value3]
$array[value4]
$array[value5]

each key (the value inside the []) can hold a different value, which is a whole different tutorial in itself. Arrays can get very large, and very confusing. A quick example of what I mean. My Stats Page was made by storing values from a mysql query into an array. that array goes 6 levels deep, meaning the array looks something like this $array[][][][][][], 5 of the 6 []s has another array in it, which goes roughly 3 levels deep. Pretty much, arrays can be very intimidating as they get more complex, but they are amazing things to use and you can do a ton of different things with them

Enough about arrays, lets get back to the alternating rows.

Because I dont really want to write what I just wrote in the mysql section, Im copiying what I wrote there. So if you read the mysql section, the next parts are the same exact thing.

//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.

Now we need to loop through the array (like what we did wiht mysql, but its done differently)

//Loop through the array
foreach($array as $arr_value){

What this does is say, for each value in the $array, set this value to $arr_value, and do the following. Which is:

$table .= "<tr><td class="$class">$arr_value</td></tr>";

Again the $table variable, but this time its not set = its set .=, which is how you concatenate the table rows. Here we set the css class = to $class. If we stoped the code right now, we would have 2 things. The first is the obvious, a syntax error because the loop isnt closed, so say we fix the error, we would only have 1 row class, because as it stands right now, $class is set to $class1, which is RowOne. This is where the next part comes in. The part that alternates the rows.

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

This is the main part that alternates the row colors. What this does is, the first time the array is looped through $class is = to $class1, so it sets $class to $class2, which is done in the if statement. The next time it loops through, $class is set to $class2, so it gets set to $class1, and so on.

Finaly we have:

$table .= "</table>";

//echo the table
echo $table;
?>

Again we concatenate the table, then echo the table, and close the file. Ta da, we're done.



Author's URL: Nathanael Mitchell
Browse Pages: << < 1  2  3 
PHP is open source scripting language. It\'s widely used to develop web applications. More PHP Tutorials: Featured Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Reader's comments
comments GabrielleMurphy29 February 02, 2012 says:
Have no enough cash to buy a building? Do not worry, because this is available to receive the <a href=" loans</a> to work out such kind of problems. So take a auto loan to buy everything you need.
Reply
Add comments to "Alternating Row Colors"

Captcha