Edit TXT File with cakephp

Asked

Viewed 82 times

-1

Guys if possible would like a help to edit a file .txt that is formatted with json_decode().

On the controller went 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>

He searches for the information perfectly. I would like a help to make 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 was saving that data in the bank, 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>

I appreciate the help.

1 answer

1

Just for the record, I was able to solve the problem. The function was 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'));
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.