Search code examples
phpjquerycodeignitersearch-engine

CodeIgniter: Basic search engine using JQuery


I am new in CodeIgniter and I am trying to build a basic search engine using JQuery.

My controler is

class Welcome extends CI_Controller {

    public function index()
    {
                $this->load->view('searchPeople');
                $this->load->view('css/format');
    }
        public function searchPeopleResults(){
            $theCity=$_POST['theCity'];
            $this->load->model('MSearchPeople');
            $data=$this->MSearchPeople->provideSearchPeopleResults($theCity);
            $this->load->view('searchPeople',$data);
            $this->load->view('css/format');            
        }
}

The relevant part of my model is

Class MSearchPeople extends Model {

    function MSearchPeople() {
        parent::Model();
    }

    function provideSearchPeopleResults($theCity){
    // ... There is a query to the database that I dinamically generate HTML data.        

        return $data;
    }

The relevant part of my view is

<html>



    <head>


        <script type="text/javascript" src="http://code.jquery.com/jquery-1.3.2.js"></script>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("form#searchForm").submit(function() {
                    var theCity = $("select#chooseCity").val(); 
                    $.post("welcome/searchPeopleResults/", {theCity: theCity}, function(data) {
                        $("div#searchResults").html(data);
                    });
                  return false
                });
            });
        </script>
    </head>

    <body>


        <FORM id="searchForm">
            <h2>Selecione uma cidade: </h2>            
            <select id="chooseCity">
                <?php
                $theCitiesOptionsHTML = "cityOptions.html"; <!-- A large list of cities -->
                require($thePathDataFiles.$theCitiesOptionsHTML);                
                ?> 
            </select>
        </FORM>
        <div id="searchResults">
            <!-- Search results should arise here -->
        </div>        

     </body>

</html>      

It is important to stress that the version without the MVC design in PHP is working. However, after translating this, it is not working anymore.

}

The netbeans output is:

[Mon Feb 10 01:06:19 2014] 127.0.0.1:52977 [200]: /

[Mon Feb 10 01:06:29 2014] 127.0.0.1:52980 [500]: /welcome/searchPeopleResults/

[Mon Feb 10 01:06:29 2014] 127.0.0.1:52981 [500]: /welcome/searchPeopleResults/

**1) Are there necessary two controllers "index" and "searchPeopleResults"?

2) Is the data correctly transfered to the necessary classes?

3) Is there anyway to echo the variables inside these class?**

4) Should I load something besides the scripts in the above-mentioned head?

Thank you!


Solution

  • After trying again and again I found a solution. I would like to state that I realy appreciate the help of @Sudhir that helped me see the correct path.

    1) There was a serious error in my code. I think it is related to the version of the codeIgniter, but I only realized that after changing the controller. "I was extending Model instead of CI_Model.

    2) I dont need to load the view again in searchPeopleResults, I only need to echo the results of provideSearchPeopleResults.

    Here I show the correct code:

       class Welcome extends CI_Controller {
    
            public function index()
            {
                        $this->load->view('searchPeople');
                        $this->load->view('css/format');
            }
                public function searchPeopleResults(){
                    $theCity=$this->input->post('theCity');
                    $this->load->model('MSearchPeople');
                    $data=$this->MSearchPeople->provideSearchPeopleResults($theCity);
                    echo $data;
                }
        }
    Class MSearchPeople extends CI_Model {
    
        function provideSearchPeopleResults($theCity) {
    
        // Here I got my database query and save as $theHtml such as $theHtml='<p> Hello </p>'.       
    
         return $theHtml;
    
        }
    }
    

    My view is exactly my previous view with the modification suggested by @Sudhir [the inclusion of e.preventDefault].

    Finally, let me answer the questions above:

    1) Yes, in my solution I need two controllers. However, I dont need to load the view twice.

    2) Now, the data is correctly transferred.

    3) Yes, I did this.

    4) No, the scripts are ok. But the best solution is to download them and keep it in a script directory.