CornerPixel Blog RSS 2.0

# Wednesday, September 10, 2008

I was having fun with PHP the other day while working for a customer.  The customer had a web page that displayed a list of 15 people who were area representatives for their company.  Each row of the table included four columns.  The fields were name, email, area of responsibility, and phone extension.  Well the customer sent me an excel sheet with the new changes for me to change, and told me he wanted me to create an easier method for changing this list out.

Well the site is written in PHP and I am not a PHP expert but I was told to give it my best.  My thought was that we would have a CSV file for the data and the customer could edit that file via a PHP web application. 

The first step was to create a CSV file.  That was easy since it was already in Microsoft Excel.

After that it was time to create some PHP code to extract that CSV data and place it in a table.  Below is the PHP code I used.  I will explain it after.

<?php
            echo "<table class='repTable'>";
            echo "<tr><th>Name</th><th>E-Mail Address</th><th>Territory</th><th>Ext</th></tr>";
            ?>
            <?php
            $linenumber = 0;
            $handle = fopen("reps2.csv", "r");
            while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
                if( $linenumber & 1)
                echo "<tr class=\"oddColumn\">";
                else
                echo "<tr>";
                echo "<td>$data[0]</td><td><a href=\"mailto:$data[1]\">$data[1]</a></td><td>$data[2]</td><td>$data[3]</td></tr>";
                $linenumber++;
            }
            fclose($handle);
            ?>
            <?php echo "</table>";?>

As you can see the first PHP code is just to display the first part of the table.  The second part of the code is to display the CSV data. 

I started with a line number variable $linenumber in order to make the odd number rows a different CSS class so that the background of those rows would be blue.  I have the $linenumber variable increase each pass through a line of data.  I will get back to that.

Next the code that reads the CSV file and how it works.

$handle = fopen("reps2.csv", "r");
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

This opens the reps2.csv file and starts the loop.  Notice that we have created another two variables, $handle and  $data.  $handle is to handle the opening of the file and $data is the variable that stores the data for each loop.

The second line is the start of the loop and it is where the $data variable is created.  The 1000 is the size of the line and the comma is what we have chosen for the delimiter. (CSV files are comma separated as the name states)

if( $linenumber & 1)
                echo "<tr class=\"oddColumn\">";
                else
                echo "<tr>";

The next lines are using the $linenumber variable to determine if the line number is odd or even.  If it is odd then <tr class="oddColumn"> is used in the markup, and if it is even it uses <tr> in the markup creation of the table row.

echo "<td>$data[0]</td><td><a href=\"mailto:$data[1]\">$data[1]</a></td><td>$data[2]</td><td>$data[3]</td></tr>";
                $linenumber++;
            }
            fclose($handle);
            ?>

The next part is where the PHP code generates the markup for the data within the table row.  Notice that I am using the $data variable that is holding the data taken from the CSV file. 

When the table row is written the next line of code then adds 1 to the $linenumber variable and starts the loop again.

Lastly the fclose($handle); effectively releases the CSV file.

Okay I hope all that above makes sense.  I may have used some wrong terminology but I tried my best to explain this code.  If I wrote something wrong, or you want to add something please comment below.

Happy Coding,
~Michael

Wednesday, September 10, 2008 5:49:47 PM (Pacific Standard Time, UTC-08:00)

#       Comments [2] - Trackback

Code | Code - PHP

Archive

<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Email Subscription

Sign up to get the CornerPixel Blog delivered to your email.

About the Author

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Sign In

Statistics

Total Posts: 51
This Year: 2
This Month: 0
This Week: 0
Comments: 26