Click here to Skip to main content
13,631,882 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:

<?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;
    }
}
Posted 17-Feb-18 6:33am
Updated 17-Feb-18 22:40pm
v2
Comments
18-Feb-18 3:57am
   
I have solved the problem now!

1 solution

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

Solution 2

Answered only to remove from unanswered queue - solved by OP
  Permalink  

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
Web02 | 2.8.180712.1 | Last Updated 18 Feb 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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