I decided for myself to put the first "big" task after "Hello World" in PHP to make a table that will pull the values ​​from the php file (part 2 is writing to the file from the page). I finally launched debager, but have not yet figured it out (


The code should from this data, which are now written in the txt file, build a 3x3 table! The code says that it does not find the 1st and 2nd index in $ mass ! But it displays the values ​​in 1 line without _ and without;. In the code of the page, he does not even add for some reason html table code. Tell me please, where is the error in the code?


file.php

<?php $string1=file("table.txt"); for($i=0;$i<count($string1);$i++){ $datatxt=explode(";", $string1[$i]); for($i=0;$i<count($datatxt);$i++){ $mass=explode("_",$datatxt[$i]); $name=$mass[0]; $surname=$mass[1]; $age=$mass[2]; echo "<tr><td>".$name."</td>"; echo "<td>".$surname."</td>"; echo "<td>".$age."</td></tr>";}} ?> 

table.txt

 Angela_First_16;Lily_Oto_18;Ann_Girl_31; 

    2 answers 2

    You have an error in the index is derived from the fact that when breaking the line Angela_First_16;Lily_Oto_18;Ann_Girl_31; An array of $ datatxt consisting of 4 elements, not 3 elements is created. Delete the last character ; from the file so Angela_First_16;Lily_Oto_18;Ann_Girl_31 .

    You can see for yourself:

     <?php $string1=file("table.txt"); for($i=0;$i<count($string1);$i++) { $datatxt=explode(";", $string1[$i]); for($j=0;$j<count($datatxt);$j++) { $mass=explode("_",$datatxt[$j]); print_r($mass); $name=$mass[0]; $surname=$mass[1]; $age=$mass[2]; echo "<tr><td>".$name."</td>"; echo "<td>".$surname."</td>"; echo "<td>".$age."</td></tr>"; } } 
    • thanks, earned!) - ALPHA

    Change in the second cycle the variable $ i to $ j for example

    • It seems that this is not necessary (because the variable is inside) ?! - ALPHA
    • @ALPHA and what's inside? Visibility is one. Well, in response to DevelOper, if you paid attention, this is taken into account - rjhdby