Get two arrays from the form, one is to update the field data, the other is the condition array, I use id here
Source: http://www.liuhai.org/sql-set-key-case-when-then
- function save_category_district($parame,$id){
- if(!is_array($parame) || !is_array($id)) return false;
- $sql="UPDATE ".table.'category_district'.' SET ';
- foreach($parame as $key=>$val){
- $csql.=$key.'=CASE id';
- foreach($parame[$key] as $key=>$val) {
- $csql.=" WHEN {$id[$key]} THEN '{$val}'"; //Note that the key of the id here must be the same as the key of the data to be updated, which is the numeric key from the form
- }
- $csql.=' END,';
- }
- $sql.=rtrim($csql,','); //This step is to remove the comma after the last END
- $id=implode(',', $id); //id becomes a string here
- $sql.=" WHERE id IN ({$id})";
- if(!$this->mydb->query($sql)) return false;
- return true;
- }
Copy code
|