0
votes

I am having a problem while creating a search form in CodeIgniter - it's showing errors like:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Login::$membership_model

Filename: controllers/login.php

Line Number: 12

Backtrace:

File: C:\xampp\htdocs\Project5_1\CodeIgniter\application\controllers\login.php Line: 12 Function: _error_handler

File: C:\xampp\htdocs\Project5_1\CodeIgniter\index.php Line: 315 Function: require_once

Fatal error: Call to a member function getSearchBook() on null in C:\xampp\htdocs\Project5_1\CodeIgniter\application\controllers\login.php on line 12 A PHP Error was encountered

Severity: Error

Message: Call to a member function getSearchBook() on null

Filename: controllers/login.php

Line Number: 12

Backtrace:

My Controller Page is:

<?php 
    class Login extends CI_Controller{
        function search(){
            $searchBook = $this->input->post('searchBook');
            $data = $this->membership_model->getSearchBook($seachBook);
            $this->load->view('main_content',$data);    
        }
?>

My Model page is:

    class Membership_model extends CI_Model{
    function getSearchBook($searchBook) {
            $this->db->select('*');
            $this->db->from('book');
            $this->db->like('author',$searchBook);
            return $query->result();
    }
}

My main_content.php is:

    <?php
        echo form_open('login/search');
        echo form_input('searchBook','Search Book', 'id="searchBook"');
        echo form_submit('submit', 'Search');
        echo form_close();
    ?>

    <table><th>Title</th></table>
    <div>
        <?php
            // List up all results.
            foreach ($results as $val)
            {
                echo $val['title'];
            }
        ?>
    </div>
    </body>
1

1 Answers

0
votes

It's been a long time since I used CodeIgniter, but from what I remember, you're not loading the model in your Login class constructor. That's why you're getting these two parts of the error:

Message: Undefined property: Login::$membership_model

Message: Call to a member function getSearchBook() on null

Neither the property or the function exist in the Login class if you haven't loaded the membership_model model.

Off the top of my head, I think it needs to be something like this in your Login controller:

<?php 
    class Login extends CI_Controller{

        function __construct()
        {
            parent::__construct();
            $this->load->model('membership_model');
        }

        function search(){
            $searchBook = $this->input->post('searchBook');
            $data = $this->membership_model->getSearchBook($seachBook);
            $this->load->view('main_content',$data);    
        }
?>