簡體   English   中英

在mysql查詢Codeigniter返回的視圖頁面上突出顯示搜索關鍵字。

[英]Highlight the search keywords on view page returned from mysql query Codeigniter.

我將搜索變量作為數組傳遞給查看文件,在其中填充數據庫的描述列,我將如何在視圖頁面的描述中突出顯示某些搜索關鍵字。

//因為我搜索的關鍵字是“ Android中的手機”。

//我返回的說明行是// //“ Android中有數百個手機。”

//如何突出顯示手機和android。

//這是從數據庫返回的,與$ row ['desc'] preg_replace關聯,str_replace已嘗試,內含了已嘗試的單詞,我替換了已解決的未解決問題。

我的模特:

public function anything($tbl){
        $this->db->select('*');
        $this->db->from($tbl);
        $query = $this->db->get();
        return $query->result_array();
    }

我的控制器:

$search = $this->input->get('search');
    $tbl = table;
    $data['search'] = $this->my_model->search($tbl, $search); //returned query 
    $this->load->view('search-view', $data);

查看文件

foreach ($search as $row) {
echo = $row['desc'];
}

你可以試試這個嗎

<?php
   $phrase  = "There are hundreds of mobiles in android.";
   $keywords = array("mobiles", "android");
   $replacement = array("<b>MOBILES</b>", "<b>ANDROID</b>");
   $newphrase = str_replace($keywords, $replacement, $phrase);

   echo $newphrase;
?>

證明: http//sandbox.onlinephpfunctions.com/code/c28d9dfd718c921ef83a7a2ae3c922d70068d6d2

編輯:

我正在處理您的代碼,因此上面的解決方案可以適合您的代碼。

因此,假設$ row ['desc']是該短語,則它將像這樣:

<?php
   $phrase  = $row['desc'];
   $keywords = array("mobiles", "android"); // or you could do the following
   $keywords = explode(" ", $this->input->post('keywords'); // Or whatever value it is from POST

   $replacement = array("<b>MOBILES</b>", "<b>ANDROID</b>"); // I over simplify things here.
   $newphrase = str_replace($keywords, $replacement, $phrase);

   echo $newphrase;
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM