第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法在我的數(shù)據(jù)庫中刪除逗號?代碼點火器 PHP

有沒有辦法在我的數(shù)據(jù)庫中刪除逗號?代碼點火器 PHP

PHP
Helenr 2023-04-23 17:41:56
如何從我的數(shù)據(jù)庫中刪除逗號?我需要根據(jù)數(shù)據(jù)刪除它們。所以我有這個數(shù)據(jù)Product 1,Product 2,,,,, - 我需要刪除每個逗號所以,我需要刪除每個逗號。這是我的觀點<?php echo form_open('subjects/remove_core'); ?> <?php  $core_subjs = explode(',', $subjects['core_subjects']); ?>    <?php foreach($core_subjs as $key => $subject): ?>        <input type="hidden" name="remove_core" class="form-control" value="<?php echo $subjects['core_subjects'].'' ?>" /><br>        <input type="text" name="coresubjs[]" class="form-control" value="<?php echo $subject ?>" /><br>        <button type="submit" class="btn btn-success btn-sm">Save</button>  <?php endforeach ?></form>我的控制器public function remove_core(){    $this->subject_model->remove_core();    redirect('subjects/edit/'.$_SESSION['editID']);}我的模型 - 所以在這里我不確定我是否做對了,因為結(jié)果不正確。public function remove_core(){        $data = array(            'core_subjects' => " "        );        $this->db->where('id', $this->input->post('coresubjID'));        return $this->db->update('subjects', $data);}我如何爆破數(shù)據(jù)public function update_core(){        $data = array(            'core_subjects' => implode(',',$this->input->post('coresubjs[]'))        );        $this->db->where('id', $this->input->post('coresubjID'));        return $this->db->update('subjects', $data);}
查看完整描述

2 回答

?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗 獲得超8個贊

要刪除進(jìn)入數(shù)據(jù)庫的無關(guān)逗號,您可以根據(jù)自己的喜好使用array_filter, trim, 或。preg_replace


$this->input->post('coresubjs[]')理論值為


array(

? ?'',

? ?'',

? ?'0',

? ?'A',

? ?'B',

? ?'',

? ?'D',

? ?'',

? ?'',

);

僅使用implode(',')會導(dǎo)致

,,0,A,B,,D,,

免責(zé)聲明

但是請記住,post 值中的任何逗號都不會被正確解釋,應(yīng)該使用其他方式進(jìn)行轉(zhuǎn)義。例如:1,000將被分解為array("1", "000").?出于這個原因,我建議重構(gòu)以支持使用json_encode()andjson_decode()serialize()and?unserialize(),而不是implode(',')andexplode(',')


數(shù)組過濾器

從數(shù)組參數(shù)中刪除“空”值 (?0, "", false, null)?implode。這將適用于任何空的輸入值,包括數(shù)組的中間值。

implode(',',?array_filter($this->input->post('coresubjs[]')))

結(jié)果
請注意,所有無關(guān)的逗號和0值都已刪除

A,B,D

要避免諸如 之類的“空”值的問題0, false,您可以改用自定義回調(diào)。

implode(',',?array_filter($this->input->post('coresubjs[]'),?function($value)?{????return?null?!==?$value?&&?''?!==?$value;
}))

結(jié)果
通知所有無關(guān)的逗號都被刪除

0,A,B,D

修剪

僅從值中刪除前導(dǎo)和尾隨逗號implode。

trim(implode(',',?$this->input->post('coresubjs[]')),?',')

結(jié)果
請注意,前導(dǎo)逗號和尾隨逗號已被刪除,但中間的額外逗號保留了下來。

0,A,B,,D

preg_replace

類似于trim,從值中刪除前導(dǎo)和尾隨逗號implode,并用單個逗號替換中間的任何 2 個或更多逗號。

preg_replace(['/,{2,}/',?'/^,+/',?'/,+$/'],?[',',?'',?''],?implode(',',?$this->input->post('coresubjs[]')))

圖案說明:

  • ,{2,}任何 2 個或更多逗號

  • ^,+以1個或多個逗號開頭

  • ,+$以 1 個或多個逗號結(jié)尾

結(jié)果
通知所有無關(guān)的逗號已被刪除

0,A,B,D


查看完整回答
反對 回復(fù) 2023-04-23
?
喵喵時光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗 獲得超7個贊

在你的update_core function,你需要trim額外的','。你可以這樣做 -


public function update_core(){

        $data = array(

            'core_subjects' => rtrim(implode(',', $this->input->post('coresubjs[]')), ','); // remove the extra ',' to the right

        );


        $this->db->where('id', $this->input->post('coresubjID'));

        return $this->db->update('subjects', $data);

}

另外,請記住,您不必在每次更新時都刪除數(shù)據(jù)。更新時它會自動覆蓋以前的值。所以你的remove_core方法是多余的,應(yīng)該刪除


查看完整回答
反對 回復(fù) 2023-04-23
  • 2 回答
  • 0 關(guān)注
  • 131 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號