Loading Arrays from Files
In Chapter 2, "Storing and Retrieving Data," you learned how to store customer orders in a file. Each line in the file looked something like this:
15:42, 20th April 4 tires 1 oil 6 spark plugs $434.00 22 Short St, Smalltown
To process or fulfill this order, you could load it back into an array. Listing 3.2 displays the current order file.
Listing 3.2 vieworders.php Using PHP to Display Orders for Bob
<?php //create short variable name $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; $orders= file("$DOCUMENT_ROOT/../orders/orders.txt"); $number_of_orders = count($orders); if ($number_of_orders == 0) { echo '<p><strong>No orders pending. Please try again later.</strong></p>'; } for ($i=0; $i'; } ?>
This script produces almost exactly the same output as Listing 2.3 in the preceding chapter, which was shown in Figure 2.4. This time, the script uses the function file(), which loads the entire file into an array. Each line in the file becomes one element of an array. This code also uses the count() function to see how many elements are in an array.
Furthermore, you could load each section of the order lines into separate array elements to process the sections separately or to format them more attractively. Listing 3.3 does exactly that.
Listing 3.3 vieworders2.php Using PHP to Separate, Format, and Display Orders for Bob
<?php //create short variable name $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; ?> <html> <head> <title>Bob's Auto Parts - Customer Orders</title> </head> <body> <h1>Bob's Auto Parts</h1> <h2>Customer Orders</h2> <?php //Read in the entire file. //Each order becomes an element in the array $orders= file("$DOCUMENT_ROOT/../orders/orders.txt"); // count the number of orders in the array $number_of_orders = count($orders); if ($number_of_orders == 0) { echo '<p><strong>No orders pending. Please try again later.</strong></p>'; } echo "<table border='1'>\n"; echo '<tr><th bgcolor="#CCCCFF">Order Date</th> <th bgcolor="#CCCCFF">Tires</th> <th bgcolor="#CCCCFF">Oil</th> <th bgcolor="#CCCCFF">Spark Plugs</th> <th bgcolor="#CCCCFF">Total</th> <th bgcolor="#CCCCFF">Address</th> <tr>'; for ($i=0; $i<td>$line[0]</td> <td align='right'>$line[1]</td> <td align='right'>$line[2]</td> <td align='right'>$line[3]</td> <td align='right'>$line[4]</td> <td>$line[5]</td> </tr>"; } echo '</table>'; ?> </body> </html>
The code in Listing 3.3 loads the entire file into an array, but unlike the example in Listing 3.2, here you use the function explode() to split up each line so that you can apply some processing and formatting before printing. The output from this script is shown in Figure 3.6.
Figure 3.6 After splitting order records with explode(), you can put each part of an order in a different table cell for better-looking output.
The explode function has the following prototype:
array explode(string separator, string string [, int limit])
In the preceding chapter, you used the tab character as a delimiter when storing this data, so here you call
explode( "\t", $orders[$i] )
This code "explodes" the passed-in string into parts. Each tab character becomes a break between two elements. For example, the string
"15:42, 20th April\t4 tires\t1 oil\t6 spark plugs\t$434.00\t 22 Short St, Smalltown"
is exploded into the parts "15:42, 20th April", "4 tires", "1 oil", "6 spark plugs", "$434.00", and "22 Short St, Smalltown".
Note that the optional limit parameter can be used to limit the maximum number of parts returned.
This example doesn't do very much processing. Rather than output tires, oil, and spark plugs on every line, this example displays only the number of each and gives the table a heading row to show what the numbers represent.
You could extract numbers from these strings in a number of ways. Here, you use the function intval(). As mentioned in Chapter 1, intval() converts a string to an integer. The conversion is reasonably clever and ignores parts, such as the label in this example, which cannot be converted to an integer. We cover various ways of processing strings in the next chapter.