1

我目前有一个网站,它应该能够从用户输入中记录下来并将它们保存到数据库中。我正在寻找一些关于实现这一目标的最佳方法的指导。

该网站应该只显示最近添加的笔记,但所有其他笔记仍应保存(但隐藏)。我不知道用户会输入多少次笔记。我最初的想法是在每次用户输入笔记时动态地向数据库添加一列,但后来我最终会为每个笔记条目创建一个新列。值得一提的是,笔记与文件名相关联,因此可能是数据库中的许多行,它们都有不同的注释。

dbforge 方法是我打算采用的方法,但它会反复尝试将“注释”添加到数据库(一次就已经存在)。

$fields = array(
            ('notes') => array('type' => 'TEXT','constraints' =>'255')
        );
        $this->dbforge->add_column('mytable', $fields);

有人知道这样做的更好方法吗?我正在使用 php 和 codeigniter 框架。非常感谢所有帮助!

4

1 回答 1

3

我会有一个存储用户 ID、注释和添加日期的注释表。

在您看来,您的表单将在您的控制器中指向此:

public function addNote($user_id)
{
    $this->form_validation->set_rules('note', 'Note', 'required');

    if ($this->form_validation->run() == true) {

        $array = array (
            'user_id'   => $user_id,
            'note'      => $this->input->post('note')
        );

        $this->your_model->addRecord('notes', $array);
    }
}

addRecord()模型中的函数如下所示:

public function addRecord($table, $array)
{
    $this->db   ->insert($table, $array);

    return $this->db->insert_id();
}

然后,您可以执行这样的查询并将结果传递回您的视图:

public function getLatestNoteByUser($user_id) 
{
    $this->db->select('id, note')
             ->from('notes')
             ->where('note_added_by', $user_id)
             ->order_by('date_added', desc)
             ->limit(1);

    return $this->db->get()->row();
}

这将仅返回指定用户添加的最后一条注释。您可以将限制设置为您想要的任何值并返回row_array()而不是row(). 您甚至可以$limit在函数参数中传递 , 并使用->limit($limit).

于 2016-03-03T09:20:42.737 回答