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
{
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+");
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;
}
}