Robert Capistrano
Robert Capistrano

Reputation: 63

CodeIgniter MVC process to return database records LIKE user input

My function in home controller

public function search(){
    $search = $this->input->post('search');
    $data['users'] =  $this->users_model->search($search);
    $this->load-view('index');
}

My function in users_model model

public function search($search){
    $this->db->select('*');
    $this->db->from('users');
    $this->db->like('username', 'fname','lname', 'mname', $search);
    $query = $this->db->get();
    return $query->result();
}

My function in view profile1

<form class="navbar-form" role="search">
    <div class="input-group">
        <input type="text" class="form-control" placeholder="Search">
        <div class="input-group-btn">
            <button class="btn btn-default" type="submit" style="height: 34px;"><i class="glyphicon glyphicon-search"></i></button>
        </div>
    </div>
</form>

it has no method because I do not know how to execute it properly.

Call to a member function search() on a non-object

Upvotes: 2

Views: 5021

Answers (2)

Parvez Ahmed
Parvez Ahmed

Reputation: 650

Try this complete search system

in search form

   <form action="<?php echo base_url(); ?>home/search" method="post"> 
        <div class="input-group"> 
        <input type="text" name="search" class="form-control" placeholder="Search">
        </div>
        <div class="input-group">
        <input type="submit" value="search" name="save"/>
        </div>
        </form>

view data

     <table>
      <tr>
          <td>User Name</td>
          <td>First Name</td>
          <td>Last Name</td>
          <td>Middle Name</td>
      </tr>
<?php foreach($users as $search_show):?>
      <tr>
          <td><?php echo $search_show->username?></td>
          <td><?php echo $search_show->fname?></td>
          <td><?php echo $search_show->lname?></td>
          <td><?php echo $search_show->mname?></td>
      </tr>
<?php endforeach ?>

 </table>

in controller

    public function search()
{
    $this->load->model('users_model');
    $search = $this->input->post('search');
    $data['users'] =  $this->users_model->search($search);
    $this->load-view('index',$data);
}

in 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();
    return $query->result();
}

Upvotes: 2

Jethro Hazelhurst
Jethro Hazelhurst

Reputation: 3285

You are trying to call search() on an undefined property $this->users_model which means you have not loaded your model.

You can either load it manually before you need to call it (easy on resources) by writing:

$this->load->model('users_model');

Or by auto-loading it in the CodeIgniter config (heavy-ish on resources depending on the model).

Upvotes: 0

Related Questions