Sunday, December 12, 2010

PHP: Displaying a Simple Table


After my previous post on Scrolling Tables with Fixed Headings, I realized that some people who are starting out in PHP might be interested in some of the simple PHP code I used to generate the table examples. So in this post I'll list that PHP code and explain it.

One thing I did not want to do was duplicate the table data in multiple files. So I have put all the table data in one PHP file called tabledata.php. Here's an excerpt from that file:

$cruises = array(
    array("Seabourne Sun","Seabourn Cruise Line","1988","","Ended service in 2002,
           currently operating as Prinsendam"),
    array("Adventures of the Seas","Royal Caribbean International","2001","138,000","Operating"),    
    array("Oceanic Independence","American Hawaiian Cruises / American Global Line","1974",
          "23,719","Named formerly (1951—1974) and subsequently renamed 
           (1982—2006) the Independence, renamed the Oceanic (2006), sold 
           for scrap in 2008 but remains in mothballs"),
    array("Cunard Ambassador","Cunard Line","1972","14,160","Burnt 1974, 
           rebuilt into a livestock carrier, renamed Linda Clausen, later Procyon, Raslan. 
           Scrapped 1984 after a second fire."),
    array("Aegean Beauty","Voyages to Antiquity","1973","11,563","Formerly Aegean Dolphin 
           and Aegean I. Operating"),
    array("Serenade of the Seas","Royal Caribbean International","2003","90,090","Operating"),
    array("Queen Elizabeth 2","Cunard Line","1969","70,327","Left fleet in November 2008"),
    array("Queen Elizabeth","Cunard Line","2010","92,000","Operating"),
    array("Regent Sea","Regency Cruises","1957","","Began operation in 1957 as the Gripsholm (II) 
           for Swedish America Line. Ended operation when Regency went bankrupt in 1995. 
           Was to be converted to a casino ship, but later marked for scrap. Prior to scrapping,
           was looted by pirates, then sank in heavy seas in 2001."),
    array("Allure of the Seas","Royal Caribbean International","2010","225,000",
           "Starts service in December 2010"),
);

This is a PHP array of arrays. Each inner array represents one row in the table we will render. In each PHP file where I need to show the table I simply include the file with the array(include "tabledata.php";). Then the only trick is to loop through the outer array and generate the table:

<table class="cruises">
        <tr>
          <th>Name</th>
          <th>Operator</th>
          <th>Began operation</th>
          <th>Tonnage</th>
          <th>Status</th>
        </tr>
        <?php foreach ($cruises as $cruise): ?>
          <tr>
            <td><?=$cruise[0]?></td>
            <td><?=$cruise[1]?></td>
            <td><?=$cruise[2]?></td>
            <td><?=$cruise[3]?></td>
            <td><?=$cruise[4]?></td>                        
          </tr>
        <?php endforeach; ?>
</table>
    

The key here is the "foreach" loop. In PHP you can code this with a colon and an "endforeach", as I did here, or using the traditional curly braces. Inside the loop each inner array gets assigned to the $cruise variable. We then access individual fields by indexing into $cruise with offsets from zero through four.

If I think of something to add, I'll do that tomorrow when I wake up. Good nite. :)

Post a Comment