用于在codeignitier中进行热门搜索的代码

时间:2016-12-01 06:44:35

标签: php mysql codeigniter

codeignitier中的热门搜索代码。这是我的控制器。

function main_search($offset = 0)
    {
        $term = ($this->input->post("term"))? $this->input->post("term") : "NIL";
        $term = ($this->uri->segment(3)) ? $this->uri->segment(3) : $term;
        $config = array();
        $config["base_url"] = base_url() . "User/main_search/$term";
        $config['total_rows'] = $this->User_model->main_search_count($term);
        $config['total_rows'] = $this->User_model->main_search_hos_count($term);
        $config['per_page'] = 3;
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_link'] = false;
        $config['last_link'] = false;
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['prev_link'] = 'Prev';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = 'Next';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $this->pagination->initialize($config);
        //$data['pagination'] = $this->pagination->create_links();
        $data['search'] = $this->User_model->fetch_data($config['per_page'], $offset,$term);
        $data['search_hos'] = $this->User_model->fetch_hos_data($config['per_page'], $offset,$term);
        $data['get_specs'] = $this->specialisation_model->get_specialisation();

        if($data){

               $this->load->helper(array('form', 'url'));
               $this->load->view('customer/header');
               $this->load->view('customer/side_view',$data);
               $this->load->view('customer/main_search',$data);
               $this->load->view('customer/footer');
               }
          else{
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! No Results available for your search!!!</div>');
                $this->load->view('customer/header');
                $this->load->view('customer/side_view',$data);
                $this->load->view('customer/main_search');
                $this->load->view('customer/footer');
        //var_dump($data);die();
            }
    }

型号

 public function main_search_count($term)
    {
        if ($term == "NIL") $term = "";
        $this->db->select('*');    
        $this->db->from('tbl_doctor');  
        $this->db->join("tbl_specialisation", "tbl_specialisation.spec_id = tbl_doctor.spec_id",'left');
        $this->db->where("(tbl_doctor.dr_name LIKE '$term' OR tbl_doctor.district LIKE '$term' OR tbl_specialisation.spec_specialise LIKE '$term')");
        $query= $this->db->get();
         return $query;

    }
    public function fetch_data($limit, $offset, $term)
    {
        if ($term == "NIL") $term = "";
        $this->db->select('*');    
        $this->db->from('tbl_doctor');  
        $this->db->join("tbl_specialisation", "tbl_specialisation.spec_id = tbl_doctor.spec_id",'left');
        $this->db->where("(tbl_doctor.dr_name LIKE '$term' OR tbl_doctor.district LIKE '$term' OR tbl_specialisation.spec_specialise LIKE '$term')");
         $this->db->limit($limit, $offset);
        $query = $this->db->get();
        //var_dump($query);die();

          return $query;


    }
    public function main_search_hos_count($term)
    {
        if ($term == "NIL") $term = "";
        $this->db->select('*');
        $this->db->from('tbl_hospital');
        $this->db->where("(tbl_hospital.h_name LIKE '$term' OR tbl_hospital.place LIKE '$term')");
        $query= $this->db->get();
         return $query;

    }
     public function fetch_hos_data($limit, $start, $term)
    {
        if ($term == "NIL") $term = "";
        $this->db->select('*');
        $this->db->from('tbl_hospital');
        $this->db->where("(tbl_hospital.h_name LIKE '$term' OR tbl_hospital.place LIKE '$term')");
        $this->db->limit($limit, $start);
        $query= $this->db->get();
        return $query;    
    }

查看

<div class="list">
<ul>
<?php 
if(isset($search)):  
         foreach ($search->result() as $row){
            $dr = $row->dr_name;
            $spec = $row->spec_specialise;
            $dis = $row->district;
            $st = $row->state;
?>
<li><div class="imgt"><img src="<?php echo base_url("./resources/images/");  if($row->dr_img) echo $row->dr_img; else echo "no-img.jpg"; ?>" height="90px" width="82px"></div><div class="text"><h3><b>Dr. <?php echo  $row->dr_name;?></b><br></h3><p><?php echo $row->spec_specialise; ?><br><?php echo  $row->district;?><br><?php echo  $row->state;?></p></div><div class="text"></div><div class="link"><a href="<?php echo site_url('User/doctor_details_view/'.$row->id); ?>"><i class="ace-icon fa fa-eye sym"></i>View</a></div></li>
 <?php } elseif($search_hos):
        foreach ($search_hos->result() as $val){
          ?> 

<li><div class="text"><h3><b><?php echo $row->h_name; ?></b><br></h3><p><?php if($row->type == 0) echo "Private"; else echo "Government"; ?><br>Email:<?php echo $row->email; ?><br>Website: <?php echo $row->website; ?></p></div><div class="link"><a href="<?php echo site_url('User/hospital_details_view/'.$row->h_id);?>"><i class="ace-icon fa fa-eye sym"></i>View</a></div></li>
 <?php } else: echo "<div class='alert alert-danger text-center'>Oops! No Results available for your search!!!</div>";
endif;
 ?>
</ul>

<div class="space">
</div>
</div></div>
<div class="pdt_rightt">
<center>

</center>
</div>
<div class="col-xs-12 bottom">
 <ul class="col-sm-12">
  <li class="col-xs-3"><img src="<?php echo base_url();?>resources/images/download.jpg" width="50px" height="50px"><p>112</p>  Emergency</li>
  <li class="col-xs-3"><img src="<?php echo base_url();?>resources/images/amb.png" width="50px" height="50px"><p>108</p> Ambulance</li>
  <li class="col-xs-3"><img src="<?php echo base_url();?>resources/images/redc.png" width="60px" height="100px"><p>105</p> Red Cross</li>
  <li class="col-xs-3"><img src="<?php echo base_url();?>resources/images/blood.jpg" width="50px" height="50px"><p>1910</p> Emergency</li>
 </ul>
</div>
</div>

在此搜索中使用了3个表。部分此代码正常工作,医生相关术语搜索所有正在使用此代码。但是当医院搜索显示数据库错误时。

enter image description here

2 个答案:

答案 0 :(得分:0)

您的问题不在您的代码中,而在于您的数据。

例如,Nair的医院在文本中包含单引号,这搞乱了查询:

SELECT * `tbl_doctor`... WHERE (`tbl_doctor`.`dr_name` LIKE 'Nair's hospital' OR ... 

为了轻松避免它(并且作为一种良好的安全措施),我建议您在将字符串提交到数据库之前将其转义。 CodeIgniter提供了几种方法。 https://www.codeigniter.com/userguide3/database/queries.html#escaping-queries

中的详细信息

在您的情况下,建议使用$ this-&gt; db-&gt; escape_like_str()。

修改您的型号代码,以便

$this->db->where("(tbl_doctor.dr_name LIKE '$term' OR tbl_doctor.district ...

被类似于

的东西取代
$this->db->where("(tbl_doctor.dr_name LIKE '" .
                   $this->db->escape_like_str($term) .
                 "' OR tbl_doctor.district ...

答案 1 :(得分:0)

更改模型函数main_search_count,其中替换为此代码

&#13;
&#13;
$this->db->where("(tbl_doctor.dr_name LIKE '%".$term."%' OR tbl_doctor.district LIKE '%".$term."%' OR tbl_specialisation.spec_specialise LIKE '%".$term."%')");
        $query= $this->db->get();
         return $query->num_rows();
&#13;
&#13;
&#13;

function fetch_data where where this code

&#13;
&#13;
$this->db->where("(tbl_doctor.dr_name LIKE '%".$term."%' OR tbl_doctor.district LIKE '%".$term."%' OR tbl_specialisation.spec_specialise LIKE '%".$term."%')");
         $this->db->limit($limit, $offset);
        $query = $this->db->get();
       if($query->num_rows()>0)
        {
          return $query;
        }
&#13;
&#13;
&#13;

替换main_search_hos_count函数的位置

&#13;
&#13;
$this->db->where("(tbl_hospital.h_name LIKE '%".$term."%' OR tbl_hospital.place LIKE '%".$term."%')");
        $query= $this->db->get();
         return $query->num_rows();
&#13;
&#13;
&#13;

替换fetch_hos_data函数的位置

&#13;
&#13;
$this->db->where("(tbl_hospital.h_name LIKE '%".$term."%' OR tbl_hospital.place LIKE '%".$term."%')");
        $this->db->limit($limit, $start);
        $query= $this->db->get();
        if($query->num_rows()>0)
        {
            return $query; 
        }   
&#13;
&#13;
&#13;