Home > Articles > Data > MySQL

  • Print
  • + Share This
This chapter is from the book

Loading Arrays from Files

In Chapter 2, "Storing and Retrieving Data," we stored customer orders in a file. Each line in the file looks something like:

15:42, 20th April 4 tires 1 oil 
6 spark plugs $434.00 
22 Short St, Smalltown

To process or fulfill this order, we 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 = $HTTP_SERVER_VARS['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.2 in the previous chapter, which is shown in Figure 2.4. This time, we are using 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, we 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 = $HTTP_SERVER_VARS['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 we are using the function explode() to split up each line, so that we can apply some processing and formatting before printing.

The output from this script is shown in Figure 3.6.

The explode function has the following prototype:

array explode(string separator, string string [, int limit])

Figure 3.6Figure 3.6 After splitting order records with explode, we can put each part of an order in a different table cell for better looking output.

In the previous chapter, we used the tab character as a delimiter when storing this data, so here we called:

explode( "\t", $orders[$i] )

This "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.

We have not done very much processing here. Rather than output tires, oil, and spark plugs on every line, we are only displaying the number of each and giving the table a heading row to show what the numbers represent.

There are a number of ways that we could have extracted numbers from these strings. Here we used the function, intval(). As mentioned in Chapter 1, intval() converts a string to an integer. The conversion is reasonably clever and will ignore parts, such as the label in this example, that cannot be converted to an integer. We will cover various ways of processing strings in the next chapter.

  • + Share This
  • 🔖 Save To Your Account