In this third part of a five-part series on using HTML_Table, you will learn how to create a table from database data and how to leverage subqueries. This article is excerpted from chapter 34 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).
While using arrays as the data source to create tables is great for introducing the basic fundamentals of HTML_Table, chances are you’re going to be retrieving this information from a database. Therefore, let’s build on the previous examples by retrieving employee data from Oracle Database XE and presenting it to the user in a tabular format.
The general process really doesn’t differ much from that presented in Listing 34-1, except this time you’ll be navigating through a result set (from theEMPLOYEEStable, of course) rather than a standard array to populate the page. Listing 34-2 contains the code without the style tag.
Listing 34-2. Displaying Oracle Data in Tabular Format
<?php
// Show an HTML_Table form populated from a database table // containing employee data.
// Include the HTML_Table package
require_once "HTML\Table.php";
// Connect to the server and select the database $c = @oci_connect('hr', 'hr', '//localhost/xe') or die("Could not connect to Oracle server");
Executing Listing 34-2 produces output identical to that in Figure 34-1; notice that the number of rows returned is restricted to five, using theROWNUM variable in theWHEREclause. TheROWNUM column is known as an Oracle pseudo-column: the column does not exist in the table itself but instead functions as a counter containing the row number of the result set. If the database query returns hundreds of rows, you don’t want to display them all on one page. Later in this chapter in the section “Creating Paged Output” we show you how to create paged output.