php - Codeigniter database delete and insert multiple records -
i have problem different this , this. in model class had separate delete , save 2 different functions follows.
#save advanced preferences function savepreference($preferences){ $this->db->insert('bingo_advanced_preferences', $preferences); echo $this->db->last_query(); } #delete advanced preferences function deletepreference($user_id,$criteria){ return $this->db->delete('bingo_advanced_preferences', array('bingo_user_id' => $user_id,'adv_criteria' =>$criteria)); }
if call these functions controller delete, update works.
//language preferences if($this->input->post('language') && count($this->input->post('language')) > 0): $this->bingo_advanced_preferences->deletepreference($user,'15'); ($i=0; $i < count($this->input->post('language')); $i++) { $language_options = array( 'bingo_user_id' => $user, 'adv_criteria' => 15, //languages 'adv_criteria_value' => $this->input->post('language')[$i], 'adv_date_created' => date('y-m-d h:i:s') ); $this->bingo_advanced_preferences->savepreference($language_options); } endif;
however doesn't cater scenario deselects correct order should this.
$this->bingo_advanced_preferences->deletepreference($user,'15'); if($this->input->post('language') && count($this->input->post('language')) > 0): ($i=0; $i < count($this->input->post('language')); $i++) { $language_options = array( 'bingo_user_id' => $user, 'adv_criteria' => 15, //languages 'adv_criteria_value' => $this->input->post('language')[$i], 'adv_date_created' => date('y-m-d h:i:s') ); $this->bingo_advanced_preferences->savepreference($language_options); } endif;
this deletes records adv_criteria=15 , never updates(do subsequent saving). of course have solved problem if else statement question why doesn't work? how can make work?
for delete , can on view file
<input type='checkbox' name='tableid[]' value='<?php echo $value->tableid; ?>"'>
then on controller this
function delete_multiple() { // retrive multiple id list form $check_id = $this->input->post('tableid[]'); // multiple delete loop id foreach($check_id $checkid) { $this->db->where('tableid', $checkid); $this->db->delete('tablename'); } }
for insert , can on view file same above on controller this
function multiple_add() { $table_id = $this->input->post('tableid[]'); foreach($table_id $tableid) { $data_some = array( 'tableid' => $tableid, 'blah' => $this->input->post('blah') ); // insert data function call $this->db->insert('tablename', $data_some); } }
Comments
Post a Comment