Click here to Skip to main content
13,903,244 members
Rate this:
Please Sign up or sign in to vote.
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:


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


    function addBlog($nam,$mes,$dat){
        fwrite($txtx, "$nam,$mes,$dat".PHP_EOL);

    function removeBlog($ind){

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


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

//Class Blog


class Blog

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

    function __construct($nam,$mes,$dat)

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

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

    function getDate(){
        return $this->date;
Updated 17-Feb-18 22:40pm
Member 13683022 18-Feb-18 3:57am
I have solved the problem now!

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Answered only to remove from unanswered queue - solved by OP

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190306.1 | Last Updated 18 Feb 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100