Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 取消设置后更新txt文件_Php_String - Fatal编程技术网

Php 取消设置后更新txt文件

Php 取消设置后更新txt文件,php,string,Php,String,问题是removeBlog会删除所有行,而不是逐行删除。 我想我把foreach/fwrite部分搞错了。 下面是课堂信息: class Message { protected $blogLista=[]; protected $file = "blog_tmp/blogdata.txt"; function __construct() { if(file_exists($this->file)) { $txt = fopen($this->file,"r

问题是removeBlog会删除所有行,而不是逐行删除。 我想我把foreach/fwrite部分搞错了。 下面是课堂信息:

 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;
  }
}
问题是removeBlog会删除所有行,而不是逐行删除。 我想我把foreach/fwrite部分搞错了。 下面是课堂博客部分:

 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;
 }
}

当您试图调用方法时,您缺少括号
()
,并且您不能在双引号内调用像这样的方法[1]

尝试替换为:

fwrite($var,$val->getName().','.$val->getMessage().','.$val->getDate()) ;
而不是:

fwrite($var,"$val->getName(),$val->getMessage,$val->getDate") ;

[1] 参见@NigelRen的评论,请描述您遇到的问题。除了
fwrite($var,$val->getName(),$val->getMessage,$val->getDate”)应该是
fwrite($var,$val->getName().,'.$val->getMessage().,'.$val->getDate())我们需要知道哪里出了问题,以及您希望实现的目标。请看这里的说明,您可以在双引号内调用方法,但必须使用大括号<代码>回显“{$val->getName()},{$val->getMessage()},{$val->getDate()}”。反正我还是投了赞成票,因为我要回答几乎相同的问题。@NigelRen你说得对!非常感谢。我已经更新了anwser:)