0

我正在做一个笔记本,登录,写笔记。所有信息都必须存储到文本文件中(我知道使用 DB 更简单,但存储到文件是项目要求)。

到目前为止,我已经完成了登录、创建新成员、添加新注释。我现在需要使这些注释可编辑。

当我在视图中显示所有笔记(然后用户登录)时,我向这些属于已登录用户的笔记添加了一个锚点,即“编辑”。

        foreach ($notes as $item)
    {
        if ($item['user'] == $name) // if post belongs to logged in user, I add "edit"
        {
             echo "<h3>", $item['user'], " " ,$item['date'], "</h3>";
             echo "<p>", $item['content'], " ", anchor('site/edit_note', 'Edit'), "</p>";                
        } 
        //if posts belongs to other users, notes are just posted
              else { 
                   echo "<h3>", $item['user'], " " ,$item['date'], "</h3>";
                   echo "<p>", $item['content'], "</p>";
               }   
    }

我的文本文件结构:

some user : some user post : date

我想我需要通过这些锚传递一些信息,以使它们独一无二,并知道在文件中的何处进行编辑,并使该帖子以文本区域的形式显示。我已阅读有关 URI 类和 URL 助手的信息,但不确定这是我需要的吗?

后来我想我会做一些文件信息数组,在数组中重写我需要的帖子,然后将数组存储在文件或其他东西中。我只是想知道这是正确的方法吗?

4

1 回答 1

1

我认为您应该将文件结构更改为每行/帖子也有一个唯一的 ID:

unique id : some user : some user post : date

然后,您可以像这样设置网址:

echo "<p>", $item['content'], " ", anchor('site/edit_note/'.$item['id'], 'Edit'), "</p>";

并且您的 edit_note 方法将需要接受 ID 参数

function edit_note($requested_id = null)
{
    if (!$requested_id) { return ""; }
    // get the requested id item from your file, that is below

    // The [`file()` function][1] will return the contents of a file as an array. 
    // Each array item will be a line of the file. So if each of your posts are a 
    // line, then you can just do:

    $rows = file('file/path/here');

    //Filter the $rows array to view just the ID needed
    $selected_row = array_filter($rows, function($row) use ($requested_id) {
        $row_items = explode(' : ', $row);
        return ($row_items[0] == $requested_id); 
    });

    $row_items = explode(' : ', $selected_row);

    // now you'll have the contents of the requested post in the $row_items array
    // and can call a view and pass it that data
    $data['row_items'] = $row_items;
    $this->load->view('edit_view', $data);
}
于 2012-08-15T19:29:37.597 回答