Best way to add multiple notes to a database with php dynamically
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)
.