dongshuo9350 2017-12-15 15:45
浏览 153
已采纳

php读写文件

function readWrite() {
    $fileReader = fopen('file.txt', 'r') or die("ERROR: File not found");
    $fileWriter = fopen('filewr.txt', 'w') or die ("ERROR: Write File not 
    found");
    $totalDaysArr= array();
    $monthNumArr= array();
    $monthArr= array();
    $row= file("file.txt");

    while($row = fgets($fileReader, 4096)) {
      list($monthNumArr[], $monthArr[], $totalDaysArr[]) = explode(",", $row);
    }

    for($x = 11; $x >= 0; $x--)
    {
      $table = explode(",", $monthNumArr[$x]);
      fwrite($fileWriter, $row[$x]);
    }
    fclose($fileWriter);
}

I'm a beginner at PHP, so this is what I have so far. My objective is to make a function that will read a file called file.txt and then write it in reverse using arrays. I'm not sure what I am doing wrong here.

These are the requirements:

This function should use PHP array to store records (each line is a "record").

This function should use a PHP Loop to walk through the months names array and generate HTML5/CSS to display the table.

Create a function that writes the text file filewr.txt (reverse order).

This function should use PHP for loop to walk through the array in reverse order and write each array entry (line/record) to the filewr.txt file.

and also the txt file looks like this:

1,January,31

2,February,28

3,March,31

4,April,30

5,May,31

6,June,30

7,July,31

8,August,31

9,September,30

10,October,31

11,November,30

12,December,31
  • 写回答

1条回答 默认 最新

  • dongying2112 2017-12-15 15:57
    关注
    1. Why use fileReader when you decide to read the data with file("file.txt"); in the end?
    2. The writing process is quite messed up fwrite($fileWriter, $row[$x]) is quite possibly the place where the error comes from: don't you aim to write that table to your file instead of a row from the original input file?

    Addition

    @Aureliux: What you want to to is generating a HTML string. Therefore, lets start with $table='<table>' and put this definition before the loop. After the loop you add the tables closing tag: $table.='</table>' The magic happens in between: For each record you create the corresponding HTML representation. $table.='<tr><td>'.$monthNumArr.'</td><td>'.$monthArr.'</td><td>'.$totalDaysArr.'</td></tr>'. Finaly you move the write command after the loop and write the generated Table Markup to your output file.

    What you will end up with looks like

    function readWrite() {
        $fileWriter = fopen('filewr.txt', 'w') or die ("ERROR: Write File not 
        found");
        $totalDaysArr= array();
        $monthNumArr= array();
        $monthArr= array();
        $row= file("file.txt");
    
        while($row = fgets($fileReader, 4096)) {
          list($monthNumArr[], $monthArr[], $totalDaysArr[]) = explode(",", $row);
        }
    
        $table='<table>';
        for($x = 11; $x >= 0; $x--)
        {
          $table.='<tr><td>'.$monthNumArr.'</td><td>'.$monthArr.'</td><td>'.$totalDaysArr.'</td></tr>';
        }
        $table.='</table>';
        fwrite($fileWriter, $table);
        fclose($fileWriter);
    }
    

    I have no idea why you would need the reverse loop, but actually, you could achieve the same result with less effort:

    function readWrite() {
        $row= file("file.txt");
    
        $table='<table>';
        for($x = 11; $x >= 0; $x--)
        {
          $table.='<tr><td>'.implode('</td><td>', explode(',',$row[$x])).'</td></tr>';
        }
        $table.='</table>';
        file_put_contents('file.txt', $table);
    }
    

    I did not test it. But the idea should be clear. Personally, I would agree with @Alive to Die. His approach is the most straight forward.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题