PHP-built utility scripts provide more flexibility
In “PHP Revisited,” we explained how PHP makes it really easy to do things like consume Web services but we haven’t touched on how the flexibility of the language makes it possible to do things that are all but impossible in RPG.
For example, think about the challenges involved in writing an RPG program that lets the user specify a qualified file name (any file name) and then produce a listing of that file’s content. While dynamic SQL can do some aspects of this, you still have a challenge in how to store, format and output the data.
The first problem is that RPG wants to know the format of the file at compile time. That’s not possible since we’ve already said that we want to be able to process any file. So perhaps we could use a program-described file and simply define the record length as that of the largest record we have.
So far so good, but we now face the problem that all we have is one huge character field and that won’t display very well, particularly if it contains packed-decimal or binary fields. In theory, even this situation is manageable in that we can use system APIs to obtain a list of the fields in the record, their formats and offsets within the record—but how to apply that knowledge? One approach might be to use a set of standard fields, each of which is defined as BASED, and then calculate the value for the individual basing pointers from the address of the start of the record plus each field’s offset. That would let us obtain the values in the individual fields, but we’re still faced with editing them, formatting the resulting output and more.
As you can see, it’s not a task for the faint of heart but as Jon often says, “But it might be fun!” If, like Susan, you don’t see any way in which that amount of work could be construed as “fun” then you’ll like that PHP’s flexible nature makes it a natural choice for utility functions such as this. Would you believe that a basic PHP script to accomplish our original task could consist of as little as 50 lines of code? We’ll prove it to you.
Looking Into the PHP Script
Our PHP script starts out after the user has entered the desired table (file) and schema (library) names on a simple HTML form. If you want to see the following code in action before examining it, check out a short demonstration video.
This first section of the script is pretty standard stuff. It begins by obtaining the table ($dbTable) and library ($dbLib) names from the browser input (i.e., the $_POST array).
<?php
// Get requested table and library names
$dbTable = $_POST['table'];
$dbLib = $_POST['library'];
// Use defaults values if nothing was supplied
if ($dbLib == '') $dbLib = "JONSDEMOS";
if ($dbTable == '') $dbTable = "CUSTOMERS";
$conn = i5_connect("localhost", "username", "password");
if (!$conn) die("No connection");
If no value is retrieved, default values are used. A connection is then made using the i5_connect() function, supplying the relevant user name and password. If we’re unable to make the connection, we simply terminate (die) and issue an error message.
PHP-built utility scripts provide more flexibility
Connecting to IBM i and Creating the Column Headings
The real work begins in the next section of the script. Before we can do anything else, we open the table we want to display, using the i5_open() function. In addition to specifying the table and library names, we also identify the type of open we want—in this case, we simply want to read the table. Next, we obtain the list of all the columns (fields) in the table. We do this with the i5_list_fields function, which returns an array of all of the column names. For those unfamiliar with PHP, many functions return their results as arrays. This is possible in part because, unlike RPG, the elements in a PHP array don’t all have to be of the same data type. In cases where RPG might need to use a data structure and/or multiple parameters, PHP can simply use an array.
// Open the table
$table = i5_open("$dbLib/$dbTable", I5_OPEN_READ | I5_OPEN_SHRRD);
if (!$table)
die("Error while attempting to open table $dbTable in library $dbLib mode=READ $i5_error()");
// Print a the row of column (field) names
$fields = i5_list_fields($table); // get the array of column names
print "<table border=1 cellpadding=3><tr bgcolor=#CCCCCC>";
// And loop through the array to output the names
foreach ($fields as $fieldName)
{
print "<th>$fieldName</th>";
}
print "</tr>";
Once we have our column name array, we create the HTML table to hold the data for output. Then we use a foreach loop to process each element of the array in turn and output the names as a row of the output table. Just as arrays are ubiquitous in PHP, so the foreach construct is one of the most commonly used methods for handling array data. It provides an automatic means of iterating through an array and retrieving each element’s value in turn. In this case, each element in the $fields array is retrieved and placed in the variable $fieldname, which is then added to the HTML table.
Retrieve and Output the Data
Once all of the table headings have been written, we can begin the process of reading through the data and writing the contents of each row to the browser screen. Use the function i5_fetch_row, which—as you’ve no doubt guessed—returns the row data as an array. The first time we call the function, we specify the constant I5_READ_FIRST to ensure that we start reading from the beginning of the file. For each subsequent read in the loop, we specify I5_READ_NEXT. To control the read loop, we’ve used a common PHP approach of using the result of the read (the $row array) as if it were a Boolean (indicator) value. In PHP, an empty variable is treated as false and so can be used to indicate that no data was found (i.e., we’ve reached the end of the file, so the while loop continues until there’s no more data to process).
// Print rows
$row = i5_fetch_row($table, I5_READ_FIRST); // get row
while ($row) // while there is a row...
{
// Output all columns in the row
print "<tr>";
foreach ($row as $column)
{
print "<td>$column</td>";
}
print "</tr>";
// Get next row
$row = i5_fetch_row($table, I5_READ_NEXT);
} // loop
print "</table>"; // finish off the table
?>
The actual output of the row’s individual columns is once again controlled by our friend foreach. If this code were any simpler, it would write itself!
Once the end-of-file condition has been reached and we drop out of the while loop, we simply write out the HTML to close off the table and complete the Web page.
That’s really all there is to it. Hopefully, you can see from this how easy it would be to build a script that would let the user select which columns to see and even add selection criteria. Although this example used Zend’s record-level I/O operations, the procedure would be equally straightforward had we chosen instead to use SQL to retrieve the data. Certainly, it’d be simpler to apply sorting criteria, if we used that approach, since we could let the user specify which column was to be used for the ORDER BY clause. However, we wanted to demonstrate that even without the use of SQL, PHP can be used very simply to build some powerful applications.
By the way, as you worked through this code, you might have noticed that the PHP i5... functions seem a little schizophrenic. Sometimes they use database terminology (i5_fetch_row) and sometimes the traditional DDS terminology i5_list_fields). We have no idea why this inconsistency exists.
We should confess that we’ve added somewhat to the schizophrenia by using the term “library.” We’re working hard at following our own advice and referring to the entities formerly known as “fields” as “columns” and “records” as “rows,” but since even IBM can’t seem to make up its mind about the correct term to use for “library” we decided to leave that as it was.
And speaking of following our own advice, note we used Jing to produce the little demo video of this application as we suggested in “Documentation Made Easy.” Let us know if you found it helpful.
If any of you have been experimenting with PHP, or even have code in production, we’d love to hear from you. In the meantime remember, learning doesn’t need to be boring—have fun!