I currently have a website which should be able to take notes from user input and save them into the database. I'm looking for some guidance on the best way to achieve this.
The website should only show the most recent notes that were added, but all other notes should still be saved(but hidden). I don't know how many times the user will enter notes. My initial thought was to dynamically add a column to the database each time the user enters notes, but then I'd end up having a new column for every notes entry.Its worth mentioning that the notes are associated with a filename, so there may be many rows in the database that will all have different notes.
The dbforge method was the way I was going to go about it but that will repeatedly try to add 'notes' to the database (which will already exist after one go).
$fields = array(
('notes') => array('type' => 'TEXT','constraints' =>'255')
);
$this->dbforge->add_column('mytable', $fields);
Would anyone know a better way of doing this? I'm using php and the codeigniter framework. All help is much appreciated!
I would have a notes table which stores User Id, Note and Date Added.
In your view, your form will point to this in your controller:
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);
}
}
The addRecord()
function in your model would look like:
public function addRecord($table, $array)
{
$this->db ->insert($table, $array);
return $this->db->insert_id();
}
You can then do a query like this and pass the results back to your view:
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();
}
This will return only the last note added by a specified user. You could set the limit to whatever value you want and return row_array()
instead of row()
. You could even pass $limit
, in the functions parameters and use ->limit($limit)
.