本文介绍了如何使用foreach / fwrite写入txt文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是removeBlog会逐行删除所有行。

我认为我对foreach / fwrite部分有错误。

我的代码来自:



我的尝试:



 <?php  

// 类消息

class 消息
{
protected $ blogLista = [];
protected $ file = blog_tmp / blogdata .TXT;

函数__construct()
{
if(file_exists($ this-> file)){
$ txt = fopen($ this-> file, r);
while (!feof($ txt)){
$ line = fgets ($ TXT);
if (!empty($ line)){
$ line_arr = explode ( ,$ line);
$ obj = 博客($ line_arr [0],$ line_arr [1], $ line_arr [2]);
$ this-> blogLista [] = $ obj ;
}
}
fclose($ txt);
}

}

函数addBlog($ nam,$ mes,$ dat){
$ txtx = fopen($ this-> file , a);
fwrite($ txtx, $ nam,$ mes,$ dat .PHP_EOL) ;
fclose($ txtx);
}

function removeBlog($ ind){
unset($ this-> blogLista [$ ind]);
$ var = fopen($ this-> file, w +);
// fwrite($ var,$ this-> blogLista);

foreach ($ this-> blogLista as $ key => $ val) {
fwrite($ var, $ val-> getName(),$ val-> getMessage ,$ val-> getDate);
}


fclose($ var);
}


函数getBlogLista(){
return $ this-> blogLista;
}
}



//班级博客



 <?php  

class Blog
{

protected $ name = ' ';
protected $ message = ' ';
protected $ date = ' ';

函数__construct($ nam,$ mes,$ dat)
{
$ this-> name = $ nam;
$ this-> message = $ mes;
$ this-> date = $ dat;
}

函数getName(){
return $ this-> name;
}

函数getMessage(){
return $ this-> message;
}

函数getDate(){
return $ this-> date;
}
}
解决方案




The problem is that removeBlog removes all the lines instead line by line.
I think i have it wrong with foreach/fwrite part.
My code comes here:

What I have tried:

<?php

// Class Message

class Message
{
    protected $blogLista=[];
    protected $file = "blog_tmp/blogdata.txt";

    function __construct()
    {
        if(file_exists($this->file)) {
            $txt = fopen($this->file,"r");
            while (!feof($txt)) {
                $line = fgets($txt);
                if (!empty($line)) {
                    $line_arr = explode(",", $line);
                    $obj = new Blog ($line_arr[0], $line_arr[1], $line_arr[2]);
                    $this->blogLista[] = $obj;
                }
            }
            fclose($txt);
        }

    }

    function addBlog($nam,$mes,$dat){
        $txtx=fopen($this->file,"a");
        fwrite($txtx, "$nam,$mes,$dat".PHP_EOL);
        fclose($txtx);
    }

    function removeBlog($ind){
        unset($this->blogLista[$ind]);
        $var=fopen($this->file,"w+");
        //fwrite($var,$this->blogLista);

        foreach ($this->blogLista as $key=>$val){
            fwrite($var,"$val->getName(),$val->getMessage,$val->getDate") ;
        }


        fclose($var);
    }


    function getBlogLista(){
        return $this->blogLista;
    }
}


//Class Blog

<?php

class Blog
{

    protected $name='';
    protected $message='';
    protected $date='';

    function __construct($nam,$mes,$dat)
    {
        $this->name=$nam;
        $this->message=$mes;
        $this->date=$dat;
    }

    function getName(){
        return $this->name;
    }

    function getMessage(){
        return $this->message;
    }

    function getDate(){
        return $this->date;
    }
}
解决方案




这篇关于如何使用foreach / fwrite写入txt文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 08:39