如何在更新某些记录的同时仍保留 Codeigniter 中的其他记录?
How to Update some records with still maintain other records in Codeignitter?
Table 信息 :
id-data
id-note
note
1
5
I went home with John Harrison
2
5
I had dinner with John Harrison
3
5
I went shopping with John Harrison
4
3
I had dinner with John Harrison
我想更新“John Harrison” note where id-note (5), with "Sebastian" 但仍然 maintain另一句,使用codeigniter-3 model-controller
Table Info 更新后应该是:
id-data
id-note
note
1
5
I went home with Sebastian
2
5
I had dinner with Sebastian
3
5
I went shopping with Sebastian
4
3
I had dinner with John Harrison
所有的建议都会很有帮助。
我为你做了一个我认为适合你的例子。
您需要查找并使用 str_replace
来更改您的备注。参见示例:
$findText = 'John Harrison';
$replaceText = 'Sebastian';
$this->db->select('*');
$this->db->from('test');
$this->db->where('id-note',5);
$query = $this->db->get();
$result = $query->result_array();
foreach($result as $val) {
$youText = $val['note'];
$replace_1 = ["$findText"];
$replace_2 = ["$replaceText"];
$result = str_replace($replace_1, $replace_2, $youText);
$updateText = array(
'note'=>$result
);
$this->db->where('id-data',$val['id']);
$this->db->update('info',$updateText);
}
Table 信息 :
id-data | id-note | note |
---|---|---|
1 | 5 | I went home with John Harrison |
2 | 5 | I had dinner with John Harrison |
3 | 5 | I went shopping with John Harrison |
4 | 3 | I had dinner with John Harrison |
我想更新“John Harrison” note where id-note (5), with "Sebastian" 但仍然 maintain另一句,使用codeigniter-3 model-controller
Table Info 更新后应该是:
id-data | id-note | note |
---|---|---|
1 | 5 | I went home with Sebastian |
2 | 5 | I had dinner with Sebastian |
3 | 5 | I went shopping with Sebastian |
4 | 3 | I had dinner with John Harrison |
所有的建议都会很有帮助。
我为你做了一个我认为适合你的例子。
您需要查找并使用 str_replace
来更改您的备注。参见示例:
$findText = 'John Harrison';
$replaceText = 'Sebastian';
$this->db->select('*');
$this->db->from('test');
$this->db->where('id-note',5);
$query = $this->db->get();
$result = $query->result_array();
foreach($result as $val) {
$youText = $val['note'];
$replace_1 = ["$findText"];
$replace_2 = ["$replaceText"];
$result = str_replace($replace_1, $replace_2, $youText);
$updateText = array(
'note'=>$result
);
$this->db->where('id-data',$val['id']);
$this->db->update('info',$updateText);
}