dongshou6041 2014-05-31 10:42
浏览 18
已采纳

如何通过在codeigniter中选择搜索类别来从db中搜索记录?

I have list of record in my index page. Now i want to search a particular record/records by choosing the category (e.g phone No, email etc).How can i do this? help..

Here is my view:

    <?php
        $attributes = array('class'=>'searchform', 'id'=>'searchform');
        echo form_open('crud/searchCrud', $attributes);?>
        <div class="formelements">
            <div class="formlbl">
                Search:
            </div>
            <div class="forminput">
                <input type="text" name="searchon" id ="searchon"/>
            </div>
            <div class="searchtyp">
                <select class="searchby" name="searchby">
                    <option value="0">---Select--- </option>
                    <option value="name">Name</option>
                    <option value="email">Email</option>
                    <option value="phone">Phone</option>
                </select>
            </div>
            <div class="searchinput">
                <input type="submit" name="search" value="Search" />
            </div>
        </div>          
        <?php echo form_close();?>

Here is My controller:

    public function searchCrud()
    {
        $records =  $this->crud_mdl->searchCrud();
        foreach ($records as $record) {
            echo $record->name;
        }
    }

Here is My Model:

    public function searchCrud()
    {
        $searchby = $this->input->post('searchby');
        $searchon = $this->input->post('searchon');

        $this->db->get('test')->result();
        return $this->db->like($searchon, $searchby);
    }
  • 写回答

1条回答 默认 最新

  • dongma0722 2014-06-01 07:16
    关注

    1) You should never access POST data directly in the Model.
    2) Always collect them in Controller and then pass it to the Model.
    3) Also always try to create your Model functions, re-usable.

    Modified your code a bit. Try running it.


    Controller:

    public function searchCrud()
    {
        $searchby = $this->input->post('searchby');
        $searchon = $this->input->post('searchon');
    
        $records =  $this->crud_mdl->searchCrud($searchby, $searchon);
        foreach ($records as $record) 
        {
            echo $record->name;
        }
    }
    


    Model:

    public function searchCrud($searchby, $searchon)
    {
        $this->db->like($searchon, $searchby);
        $query = $this->db->get('test');
    
        if($query->num_rows() > 0)
             return $query->result();
        else
             return FALSE;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端