2

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

9
  • The error is coming from your controller, correct? You have to post the part of the controller where you call the search() function because that's where your problem is Commented Mar 14, 2017 at 9:44
  • public function search(){ $search = $this->input->post('search'); $data['users'] = $this->users_model->search($search); $this->load-view('index'); } this is my function in controller sir sorry im just a newbie here Commented Mar 14, 2017 at 9:46
  • You already posted that above. Post the rest of the controller. Commented Mar 14, 2017 at 9:49
  • My main problem sir is I do not know where to place my function in view. Commented Mar 14, 2017 at 9:52
  • 1
    Okay sir I will. Thanks to your suggestion. Commented Mar 14, 2017 at 10:17

2 Answers 2

2
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();
}
Sign up to request clarification or add additional context in comments.

13 Comments

Should I post my View where I am trying to put it?
Thanks sir A LOT! It's not yet working but thank you to all your responses.
The same error occurs and I am trying to figure it out. Thanks sir!
i am updating the complete ans check it@RobertCapistrano
THANKS SIR!! HOW TO COMMEND YOU? I am just new here.
|
0

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).

5 Comments

I am actually using users_model. The main problem is I can't call the function search.
Right, is user_model extending CI_Model? Is your user_model all lower-case? Do you have an upper-case letter in a). the file name or b). In the class name?
Yes it extended the CI_Model. Only the first letter of the class name is on upper case.
Okay, try adding $this->load->model('Users_model'); with a capital U
And don't forget to call $this->User_model and not $this->user_model!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.