0

Dear friends,help me please,i have a php array,how would i display table out of it,something went wrong :

        <?php
          $data = array( 
            array ( "Hartsfield Jackson Atlanta International","Atlanta" , "ATL" ),
            array ( "Chicago O'Hare International", " Chicago", " ORD" ),
            array ("Los Angeles International", " Los Angeles", " LAX" ),
            array ( "Dallas Fort Worth International", " Dallas-Fort Worth", " DFW" ),
            array (" Denver International", " Denver", " DEN" ),
            array ( "John F Kennedy International", " New York", " JFK" ),
            array ( "San Francisco International", " San Francisco", " SFO" ),
            array ("Charlotte Douglas International", " Charlotte"," CLT" ),
            array (" McCarran International", " Las Vegas"," LAS "),
            array ("Phoenix Sky Harbor International", " Phoenix", " PHX" ),



        );
          echo "<table>
          echo <tr><th>Airport</th><th>City</th><th>Code</th></tr>";
          for ($row = 0; $row <10; $row++) {
              echo"<tr>";
                  for ($col = 0; $col < 10; $col++) {
                echo"<td>".$data[$row][$col]."</td>";
}
echo "</tr>\n";

}
          echo "</table>";
?>        
4
Contributors
4
Replies
25
Views
2 Years
Discussion Span
Last Post by jkon
1

If your code example here is the same as your actual code you have several problems.
Line 17:
echo "<table>";
Line 18:
echo "<tr><th>Airport</th><th>City</th><th>Code</th></tr>";
Line 21: your inner arrays only have 3 items so you don't need to loop to 10
for ($col = 0; $col < 10; $col++) {
And, lastly, you don't have a closing </tr> inside you col loop, which is probably making your table present itself in a way you don't intend.

0

Hello AndrisP, although a bad practice , PHP accepts a comma after the last element of an array. Maybe bad code indentation made you miss line 16. That same bad code indentation maybe confused hericles , the “</tr>” has no reason to be inside the “col” loop but inside the “row” loop as it is (I thought it also for a moment). The main reason is line 21 and that there isn't 10 columns , I would strongly suggest using a view layer abstraction for such common things.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.