doujingao6210 2017-03-21 06:35
浏览 46

搜索功能使用框架codeigniter,php

my problem is i whenever i input fname and mname i got an error from the database. i just want to search both of fname and mname in search field.

my search controller function

  public function search()
  {
    $li = $this->session->userdata('logged_in');
    $id = $this->session->userdata('idnumber');
    if($li == TRUE)
    {
      $this->load->model('users_model');
      $this->load->helper('smiley');
      $this->load->library('table');
      $image_array = get_clickable_smileys('http://localhost/efg/images/smileys', 'status');
      $col_array = $this->table->make_columns($image_array, 8);
      $image_array2 = get_clickable_smileys('http://localhost/efg/images/smileys', 'status');
      $col_array2 = $this->table->make_columns($image_array2, 8);
      $this->data['smiley_table1'] = $this->table->generate($col_array);
      $this->data['smiley_tables'] = $this->table->generate($col_array2);           
      if($this->input->post())
      {
        $search = $this->input->post('search');
        $memb = $this->users_model->search($search);
        $usersearch =  $this->users_model->search($search);
        $grpsearch =  $this->users_model->searchgrp($search);
        redirect ('home/profile/'.$memb);
      }
    }
 }

My users_model model

 public function search($search)
 {
   $this->db->select('*');
   $this->db->from('users');
   $this->db->like('username',$search);
   $this->db->or_like('fname',$search);
   $this->db->or_like('lname',$search);
   $this->db->or_like('mname',$search);
   $query = $this->db->get();
   foreach ($query->result_array() as $row)
   {
      $memb = $row['idnumber'];
   }
   $error = 'There is no record for the one you searched. Please go Back.';
   $query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'");
   $hehe = $query1->result_array();
   if($hehe==NULL)
   {
     echo $error; exit;  
   }
   else 
   {
    return $memb;
   }
 }
  • 写回答

2条回答 默认 最新

  • doujiangqu2823 2017-03-21 06:49
    关注

    Always check the varibles using in condition are declared before the condition. If it doesnt pass the condition what would be the output

            public function search($search)
             {
               $this->db->select('*');
               $this->db->from('users');
               $this->db->like('username',$search);
               $this->db->or_like('fname',$search);
               $this->db->or_like('lname',$search);
               $this->db->or_like('mname',$search);
               $query = $this->db->get();
               $memb = null;
               foreach ($query->result_array() as $row)
               {
                  $memb = $row['idnumber'];
               }
               $error = 'There is no record for the one you searched. Please go Back.';
               $query1 = $this->db->query("SELECT * FROM users WHERE idnumber =$memb");
               $hehe = $query1->result_array();
               if($hehe==NULL)
               {
                 return $error;
               }
               else 
               {
                return $memb;
               }
             }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题