i am trying to update two rows of a same column in a table named tbl_sub_categories with different where and i did this:
$temp = 4;
$sub_cid = 100;
$rank = 6;
$next_id = 112;
$this->change_rank($temp, $sub_cid);
$this->change_rank($rank, $next_id);
function change_rank($rank,$sub_cid){
// echo $rank.'--'.$sub_cid.'-------';
$query = "UPDATE tbl_sub_categories SET fld_rank = '".$rank."' WHERE fld_id ='".$sub_cid."'";
$this->db->query($query);
}
while updating the tbl it only updates first row butnthe next row doesnt change... i am not understanding why is this so.. Please help...
This may be work ->change your function as below:
function change_rank($rank1,$sub_cid1){
// echo $rank.'--'.$sub_cid.'-------';
$query = "UPDATE tbl_sub_categories SET fld_rank = '".$rank1."' WHERE fld_id ='".$sub_cid1."'";
$this->db->query($query);
}
If you're using CodeIgniter, there's a feature in its Active Record class to update multiple rows:
$data = array(
array(
'fld_rank' => '4',
'fld_id' => '100'
),
array(
'fld_rank' => '6',
'fld_id' => '112'
)
);
$this->db->update_batch('tbl_sub_categories', $data, 'fld_id');
Take a look at $this->db->update_batch();
method in CI user guide.