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.

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

报告相同问题?

悬赏问题

  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了