Alternating Rows: Arrays

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.


Related Articles

  • 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.
  • 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.
  • Beginner JS Pt. 2
    This tutorial is a well laid out recipe for basic Javascript. The article is brief and is limited to three topics: Calling the Script, Writing Comments, and Doing Stuff (Basic). Illustrations make it easy.
  • Form Handling
    We are going to be working with the $_Post or $_Get arrays. They basically tell the form if i needs to post something, or get something. In this case we are going to be showing you the basics of a contact email form, so we will be using the $_Post array.
  • 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. ....
  • Inserting Rows into a Database
    This tutorial will show you how to insert a row of data into a database using ASP and an MS Access database. You can do it on one ASP file, but well have two files: one processor page (proc.asp), and one form page (form.asp). Then, well make the MS Access database that corresponds to the ASP.
  • Introduction to Serializing
    Also includes an example that you can use to start building apps with serialize.

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.