Codeigniter中的特定列字段搜索实现

时间:2015-06-20 13:58:14

标签: php mysql codeigniter

过去几周我的代码中出现了一个错误,我尝试了各种各样的方法,但无法让它工作,我正在尝试在codeigniter中实现特定的列字段搜索功能。这是我的场景,用户登陆年龄,并决定按名称,成员关联和位置搜索人员列表。这里分别是我的控制器,模型和视图......

模型:

function get_advanced($address2,$partners,$typeofirm){
            echo $address2,$partners,$typeofirm;
            $query = "SELECT firm_id,address2,partners,typeofirm FROM auditfirms WHERE address2 LIKE '%$address2%' OR partners LIKE '%$partners%' OR typeofirm LIKE '%$typeofirm%' ORDER BY 'firm_id' LIMIT 10";
            // return print_r(json_encode($query->result_array()));
            $adv =  $this->db->query($query)->result_array();
            if ($query->num_rows() > 0){
                $row = $query->row_array();             
                echo $query;
                var_dump($adv); die();
                return $adv;
            }
            else{
                echo 'Error at syntax';
            }

控制器

function advanced(){
            $this->load->library('pagination');
            $config['base_url'] = 'http://localhost/icpak/index.php/firms/search';
            $config['total_rows'] = $this->db->get('auditfirms')->num_rows();
            $query1 = $config['per_page'] = 10;
            $query2 = $config['uri_segment'] = 3;
            $config['num_links'] = 5;
            $config['full_tag_open'] = '';
            $config['full_tag_close'] = '';
            $config['cur_tag_open'] = '<span class="current_page">';
            $config['cur_tag_close'] = '</span>';
            $config['anchor_class'] = 'class="btn btn-default" ';               
            $this->pagination->initialize($config);         
            $address2 = $this->input->post('address2');
            $partners = $this->input->post('partners');
            $typeofirm = $this->input->post('typeofirm');

            $data['advanced'] = $this->firms_model->get_advanced($address2,$partners,$typeofirm);
            $this->load->view('index/advanced', $data);
        }

view:是表格所在的模态窗口......

<!--Modal-1-->
            <div class="modal fade" id="myModal-1">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
                            <h3 class="modal-title">Advanced Search</h3>
                        </div>
                        <div class="modal-body" style="padding:0px; margin:0 auto; ">
                            <div class="col-md-12" style="padding:30px !important; margin:0 auto;">
                                <!-- <form id="contactForm" class="contact"> -->
                                <?php echo form_open_multipart('firms/advanced', array('id' => 'contactForm', 'class' => 'contact'));?>
                                    <div class="row">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">Location</label>
                                            <div class="col-sm-8">
                                                <input class="form-control mb15" type="input" name="address2" placeholder="Search by Location" aria-controls="firms" />
                                            </div>
                                        </div><br><br>
                                    </div>
                                    <div class="row">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">Members</label>
                                            <div class="col-sm-8">
                                                <input class="form-control mb15" type="input" name="partners" placeholder="Search by Member" aria-controls="firms" />
                                            </div>
                                        </div><br><br>
                                    </div>
                                    <div class="row">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">Type of Firm</label>
                                            <div class="col-sm-8">
                                                <select class="form-control mb15" name="typeofirm" placeholder="Search by Type of Firm">
                                                    <option value="Auditing">Auditing</option>
                                                    <option value="NULL">Null</option>
                                                </select>
                                            </div>
                                        </div><br><br>
                                    </div>
                                    <div class="row">
                                        <div class="form-group">
                                            <button class="btn btn-primary" id="submit">Submit</button>
                                            <button type="reset" class="btn btn-reset">Reset</button>
                                        </div>
                                    </div>
                                <!-- </form> -->
                                <?php echo form_close();?>
                            </div>
                        </div> <!-- col-md-12 -->
                    </div> <!-- modal-body -->
                </div> <!-- modal content -->
            </div> <!-- modal-dialog -->
        </div> <!-- modal fade -->
        <!--End -Modal-1-->

提前谢谢你......

1 个答案:

答案 0 :(得分:0)

您的$查询存在问题。请参阅代码中的评论。

function get_advanced($address2,$partners,$typeofirm){
            echo $address2,$partners,$typeofirm;
            $query = "SELECT firm_id,address2,partners,typeofirm FROM auditfirms WHERE address2 LIKE '%$address2%' OR partners LIKE '%$partners%' OR typeofirm LIKE '%$typeofirm%' ORDER BY 'firm_id' LIMIT 10";
            // return print_r(json_encode($query->result_array()));
            $adv =  $this->db->query($query)->result_array(); 
           //  $query is string.. so at this point you must get error.
           // if ($query->num_rows() > 0){
              if (count($adv) > 0){
                // $row variable is unused. but it will give you first record.
                $row = $adv[0];         
                return $adv;
            }
            else{
                echo 'Error at syntax';
            }
 }