Edit TXT file with cakephp

-1

Please, if you would like help editing a .txt file that is formatted with json_decode() .

No controller looks like this:

public function index()
{
    $json_str = file_get_contents('files/restaurante.txt','r+');
    $json_arr = json_decode($json_str);
    $this->set('json_arr', $json_arr);
}

A View Line:

<h3><?=$value->{'salada_segunda'}; ?></h3>

It fetches information perfectly. I would like some help to do an editing function of this file via direct input from the browser, so the user does not need to edit directly in the file. Before I saved this data in the database, and my inputs were like this:

<div class="col-sm-6">
    <div class="input-group">
        <?=
            $this->Form->input('salada_segunda',[
                'between'     => '<span class="input-group-addon"><span class="glyphicon glyphicon-grain"></span></span>',
                'type'        => 'text',
                'class'       => 'form-control',
                'placeholder' => 'Salada',
                'required'    => false,
                'label'       => false,
                'div'         => false
            ]);
        ?>
    </div>          
</div>

Thanks for the help.

    
asked by anonymous 09.09.2015 / 21:23

1 answer

1

Just to be registered, I was able to solve the problem. The function looks like this:

public function edit()
{
    if($this->request->is(array('post', 'put')))
    {
        $retornoJson = json_encode($this->request->data);
        file_put_contents('files/restaurante.txt', $retornoJson);
        $this->redirect(array('action' => 'index'));
    }
}
    
10.09.2015 / 19:27