|
The code, please
First we need to read the contents of our pricelist (pricelist.txt) into an array. php has a very simple command for that:
$fileArray=file("pricelist.txt");
Since we're going to construct a single string of text from the contents of the file, we'll set a variable named $tex equal to null, and then proceed to read the array elements one by one:
$tex=""; for($i=0;$i<count($fileArray);$i++)
As each element is read, it gets wrapped into a <td> container (we're displaying it in a table) and the counter gets incremented by a php line $i++;. And every three elements, we close the table row and start a new row using the </tr> and <tr> tags. Depending on how you want your information displayed, you can play with the overall format/layout of the information.
The complete code
price-show.php is a pretty simple piece of code to do all that. It gets integrated into each page where we want to show the information by including it:
<?php include("price-show.php"); ?>
And here is the entire code used:
<?php
$fileArray=file("pricelist.txt");
$tex="";
echo "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
for($i=0;$i<count($fileArray);$i++)
{
$tex.="<tr>\n";
$tex.="<td>$fileArray[$i]</td>\n";//product name
$i++;
$tex.="<td align=\"right\">$fileArray[$i]</td>\n";//price
$i++;
$tex.="<td>$fileArray[$i]</td>\n";//unit
$i++;
$tex.="</tr>\n";
}
echo $tex;
echo "</table>";
?>
Have fun with those text files!
And remember to pick up some of this weeks' special offers!
| Meat Pies
|
$12.00
|
per dozen
|
| Strawberry Ice Cream
|
$4.99
|
per quart
|
| Fine Wine
|
$1.99
|
bucket |
        
Average Rating: 9.5 out of 10 from 4 voters.
« previous
|