douyan4900 2014-09-22 12:28
浏览 40

突出显示从mysql查询Codeigniter返回的视图页面上的搜索关键字。

i'm passing search variable as array to view file where i'm populating description a column from database how would i highlight certain search keywords in my description on view page.

// as my searched keywords are "mobiles in android".

//my returned description row is as // "There are hundreds of mobiles in android."

// how to highlight mobiles and android.

// which are returned from data base what to do with $row['desc'] preg_replace, str_replace tried, imploded the words tried, i replace tried problem not solved.

My Model:

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

My controller :

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

View File

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

1条回答 默认 最新

  • douyun8674 2014-09-29 09:47
    关注

    Could you try this..

    <?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;
    ?>
    

    proof: http://sandbox.onlinephpfunctions.com/code/c28d9dfd718c921ef83a7a2ae3c922d70068d6d2

    EDIT:

    I am working on your code, so the solution above can fit to your code.

    So assuming that $row['desc'] is the phrase then it would be like this:

    <?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;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题