Please tell me how to fix it. It is necessary to write data in 1 line from inputs (writes in 2 lines, but at first I copied the entire file (a) + new data (b) and added it = (aab). And it is necessary that he could read if several lines in the file. Logically, this is what he should do, but for some reason, if the file has an entry on the 2nd + line, it does not find the 1st and 2nd index.How to fix the code so that it writes data to 1 line, and just in case read with 2+ lines? PS: Debuger still has not learned to use (

For readability, added another screen with IDE for you.

enter image description here


Write inputs

<?php $name3=null; $surname3=null; $age=null; if(isset($_POST['name3'],$_POST['surname3'],$_POST['age3'])){ $file=("table.txt"); // $curent=file_get_contents($file);//make copy file - need off! $curent=";".$_POST['name3']."_"; $curent.=$_POST['surname3']."_"; $curent.=$_POST['age3']; file_put_contents($file,$curent,FILE_APPEND|LOCK_EX);} // file_put_contents($file,$curent,FILE_APPEND|LOCK_EX);} // _______ // read data from table.txt ?> 

Read table.txt

 <?php $string1=file("table.txt"); for($i=0;$i<count($string1);$i++){//why did not working if 2+ strings? $datatxt=explode(";", $string1[$i]); for($j=0;$j<count($datatxt);$j++){ $mass=explode("_",$datatxt[$j]); $name="<tr align='center'><td>".$mass[0]."</td>"; $surname="<td>".$mass[1]."</td>"; $age="<td>".$mass[2]."</td></th>"; echo $name.$surname.$age;}} ?> 

table.txt

 Angela_First_16;Lily_Oto_18;Ann_Girl_31 

    2 answers 2

    this code

     for($i=0;$i<count($string1);$i++){ $datatxt=explode(";", $string1[$i]); 

    nada replace with

     $datatxt = explode(";", $string1); for($i=0; $i < count($datatxt); $i++){ 
    • Finally) Thank you very much @Lesyuk Alexey!)) Tell me more please, what else needs to be fixed (error: explode () expects parameter 2 to be string, array given) - he doesn't like that $ string1 is a string? - ALPHA
    • PS: if you write implode or put [0] index, then it displays all values ​​from the file x3 times. - ALPHA
    • And can you tell me how to record in one line, instead of writing from a new line?) Explode does not like that there is an array and not a string ?! - ALPHA
    • he doesn’t like the fact that the comma at the beginning is contacted with $ curent, but it must be at the end, so that one line should be removed from the code <tr>, <th> - Lesiuk Alexey
    • so after all <tr>, <th> it already when it reads from a file (and it would be desirable still that it wrote in the file in one line). And the semicolon at the beginning is from the program logic (I don’t remember exactly what it was but he didn’t like the last character ";" in the file) for this, I put it in the beginning. =) In general, the program is already working, now I set myself the task - to write a site parser. - ALPHA
      Заработал код! Что бы читал несколько строк в 1 массив написал вместо file - file_get_contents с нужным индексом. В одну строку так и не разобрался как записать, но и так работает)) Пора начинать выполнять следующую задачу на пути изучения php!) 

    Thanks @Lesyuk Alexey, for the answer!