0

The code below works in that it produces the table I want with the correct information. Unfortunately I also get 5 warning ion the page saying: Warning: Undefined array key 1 and then repeated until. key 5. I can't see any problem with the code and I have typed it pout a few times with the same results. Sorry I'm a beginner and I am totally stuck.

<?php //create a short variable name $document_root = $_SERVER ['DOCUMENT_ROOT']; ?> <!DOCTYPE html> <html> <head> <title>Bob's Auto Parts - Customer Orders</title> <style type="text/css"> table, th, td { border-collapse: collapse; border: 1px solid black; padding: 6px; } th { background: #ccccff; } </style> </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.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. <br /> Please try again later</strong></p>"; } echo "<table>\n"; echo "<tr> <th>Order Date</th> <th>Tires</th> <th>Oil</th> <th>Spark Plugs</th> <th>Total</th> <th>Address</th> <tr>"; for ($i=0; $i<$number_of_orders; $i++) { //split up each line $line = explode("\t", $orders[$i]); //keep only the number of itmes 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 style=\"text-align: right;\">".$line[1]."</td> <td style=\"text-align: right;\">".$line[2]."</td> <td style=\"text-align: right;\">".$line[3]."</td> <td style=\"text-align: right;\">".$line[4]."</td> <td>".$line[5]."</td> </tr>"; } echo "</table>"; ?> </body> </html> 
1
  • 1
    I'd suggest using a foreach loop instead of expecting every single order to have 5 lines. Commented Mar 18, 2021 at 19:06

3 Answers 3

1

check if your $line array has 5 elements. something like this

for ($i=0; $i<$number_of_orders; $i++) { //split up each line $line = explode("\t", $orders[$i]); if(count($line)<5){ continue; } //keep only the number of items ordered 
Sign up to request clarification or add additional context in comments.

Comments

1
  1. You are splitting your file order.txt by tabs, are you sure your file have tabs? (i recommend you to split each element with a ";")
  2. Also the explode() parameter gives you an array thats starts from 0 not from 1
  3. Is good practice checking each element before the use, something like:
 if(!isset($line[1])) { $line[1] = "empty line or use what logic you want"; } 

Comments

0

Try to use foreach to iterate $orders array.

Something like that:

foreach ($orders as $order) { $line = explode("\t", $order); //keep only the number of itmes 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 style=\"text-align: right;\">".$line[1]."</td> <td style=\"text-align: right;\">".$line[2]."</td> <td style=\"text-align: right;\">".$line[3]."</td> <td style=\"text-align: right;\">".$line[4]."</td> <td>".$line[5]."</td> </tr>"; } 

But, in this code, you are assuming that each line in the "orders.txt" file always has at least 6 informations separated by a tab (\t). Otherwise, you will receive the "Warning: Undefined array key [number]".

Depending on the case, something like this can help you:

foreach ($orders as $order) { if ($columns = explode("\t", $order)) { $row = "<tr>"; foreach ($columns as $column) { $style = is_numeric($column) ? "style=\"text-align: right;\"" : ""; $row .= "<td $style>$column</td>"; } $row .= "</tr>"; echo $row; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.