Display One-Dimensional Array
May 17, 2002
The next function in chapter5_common.php displays a one-dimensional array. This function is simpler than the two-dimensional in that it doesn't have to output multiple elements per row in the table. Each element is its own row, which makes it much easier to iterate through the array. The code used to do this is as follows:
//################################################################# //## Function: displayArray ## //## Parameters: 1D array ## //## Description: Outputs a 1 dimensional array as an HTML ## //## table. The first row is the heading for ## //## the array. ## //################################################################# function displayArray($heading,$resultset) { //Indicate if the resultset is empty if (empty($resultset)) { echo "<CENTER>\n"; echo "<BR>\n"; echo "Action Returned No Rows!"; echo "</CENTER>\n"; return ; } //The first row names the results echo "<BR>\n"; echo "<CENTER>\n"; echo "<TABLE CELLSPACING=5 CELLPADDING=5 BORDER=3 "; echo "BORDERCOLOR=\"0000FF\" >\n"; echo "<THEAD>\n"; echo "<TR>\n"; echo "<TH>"; echo $heading; echo "</TH>\n"; echo "</TR>\n"; echo "</THEAD>\n";
//Display a new row for each element of the input array $itemcount = sizeof ($resultset); for($rowCounter = 0; $rowCounter < $itemcount; $rowCounter++) { echo "<TR>\n"; echo "<TD>\n"; echo " "; echo $resultset[$rowCounter]; echo " "; echo "</TD>\n"; } echo "</TR>\n"; echo "</TABLE>\n"; echo "</CENTER>\n"; echo "<BR>\n"; }
Single Line Form
Now that you can display both one- and two-dimensional arrays, let's move on to displaying a form to retrieve the user's input into the application. This code is a template for a single line form that takes the user's input and passes it along to another portion of the application:
//################################################################# //## Function: displayOneLineForm ## //## Parameters: formaction,caption,inputname,action ## //## submitvalue ## //## Description: Outputs a single entry HTML form for ## //## gathering input ## //################################################################# function displayOneLineForm ($formaction,$caption,$inputname,$action,$submitvalue) { echo "<CENTER>\n"; echo "<FORM ACTION=\"$formaction\""; echo "METHOD=\"POST\">\n"; echo $caption." <INPUT TYPE=\"TEXT\""; echo "NAME=\"".$inputname."\" SIZE=\"30\">\n"; echo "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\""; echo $action."\" SIZE=\"30\">\n"; echo "<INPUT TYPE=\"SUBMIT\" VALUE=\"".$submitvalue."\">\n"; echo "</FORM>\n"; echo "<CENTER>\n"; } ?>
The code for displayOneLineForm() is fairly straightforward. It outputs a form with a single text input box. The key to utilizing this code is getting the parameters correct when calling the function. Of course, this function could be readily expanded to include things such as field size, spacing, and so on. For now it is left as a very generic bit of code, which allows it to be used throughout the application.
Display Two-Dimensional Arrays
Instant PHP 4
Managing the Login
|