Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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
<?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;
    }
}
Posted
Updated 17-Feb-18 22:40pm
v2
Comments
Member 13683022 18-Feb-18 3:57am    
I have solved the problem now!

1 solution

Answered only to remove from unanswered queue - solved by OP
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900