Newbie here. Just started learning PHP from a book titled "PHP and MySQL
Web Development" by Welling. In this exercise, the goal is to transfer a file's contents into array. I am reading tab delimited text from file orders.txt and displaying it on vieworders.php page. Here is the copy/paste of the php code:

<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 or 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>";
//echo $number_of_orders;
for($i=0;$i<$number_of_orders;$i++){
//split up each line
$line = explode("\t",$orders[$i]);
//keep only the number of items ordered
$line[1]=intval($line[1]);
$line[2]=intval($line[2]);
$line[3]=intval($line[3]);
//output each order
echo "<tr>
<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>

This is how the vieworders2.php shows up:
[ATTACH]5289[/ATTACH]

Capture.PNG

    Welcome to the forums. You can help us to help you by enclosing your source code samples in [noparse]

    ...

    [/noparse] bbcode tags, which I will now do for you using my awesome moderator super-powers.

      I generally prefer to use [man]fgetcsv/man for something like this.

      <?php
      $orders = file("$DOCUMENT_ROOT/../orders/orders.txt"); // where do you define $DOCUMENT_ROOT???
      $count = 0;
      while(($line = fgetcsv($order, "\t")) !== false)
      {
          if(count($row) <= 1)
          {
              continue; // empty line
          }
          if($count == 0) // first time we got a real row
          {
              echo '<table border = "1">;
      <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>
      ';
          }
          echo "<tr>
      <td>{$line[0]}</td>
      <td align = \"right\">".intval($line[1])."</td>
      <td align = \"right\">".intval($line[2])."</td>
      <td align = \"right\">".intval($line[3])."</td>
      <td align = \"right\">{$line[4]}</td>
      <td>{$line[5]}</td>
      </tr>";
          $count++;
      }
      if($count > 0)
      {
          echo "</table>\n";
      }
      else
      {
          echo "<p><strong>No orders pending.
      Please try again later. </strong></p>";
      }
      

      (Yes, I could not resist a few other changes, as well. 😉 Warning: entirely untested!)

        PS: Do you mean $_SERVER['DOCUMENT_ROOT'], instead of $DOCUMENT_ROOT ?

          Write a Reply...