Database-Live-Search Doesn't Work As Expected

Hello everyone!

I’m following exactly the instructions in this extention post: http://www.yiiframework.com/extension/database-live-search/

  1. copy jquery.js into js folder( the folder is located under my project folder, inside where index.php in located).

  2. add 2 functions to HotelController:




        //this one is added by me. Is used for freeSearch

        public function actionFreeSearch()

        {

            $model=new Hotel('search');

            $this->render('freeSearch', array('model'=>$model));

        }


        // this one is also added by me. Is used for freeSearch

        public function actionSearchEngine($keyword)

        {

            //  echo "THIS IS IAJAXX  ".$keyword;


            $model=new Hotel();

            $model->unsetAttributes();  // clear any default values

            $results=$model->freeSearch($keyword);

            $this->renderPartial('_ajax_search',array(

                    'results'=>$results,

            ));

        }

  1. add the following code to accessRules() of HotelController:



// this is added for freeSearch

array('allow', // allow authenticated user to perform 'create' and 'update' actions                       actions'=>array('create','update','InboundSearch','OutboundSearch','admin','FreeSearch','SearchEngine'),

'users'=>array('@'),

     ),



  1. add new function freeSearch to Hotel model:



        //this one is added by me. Is used for freeSearch

        public function freeSearch($keyword)

        {   


            /*Creating a new criteria for search*/

            $criteria = new CDbCriteria;

  

            $criteria->compare('hotel_name', $keyword, true, 'OR');

            $criteria->compare('hotel_id', $keyword, true, 'OR');

            //$criteria->compare('part_number', $keyword, true, 'OR');


            /*result limit*/

            $criteria->limit = 100;

            /*When we want to return model*/

            return  Hotel::model()->findAll($criteria);


            /*To return active dataprovider uncomment the following code*/

            /*

            return new CActiveDataProvider($this, array(

                'criteria'=>$criteria,

            ));

            */


        }



Here I modified table fields to my fields hotel_name and hotel_id.

5.add two new views freeSearch.php and _ajax_search.php

freeSearch.php




<body onload="document.search_form.query.focus()">

<?php 

    /*To import the client script*/

    $baseUrl = Yii::app()->baseUrl; 

    $cs = Yii::app()->getClientScript();

    $cs->registerScriptFile($baseUrl.'/js/jquery.js');

?>

 

<div class="admin"> 

<script type="text/javascript"> 

    $(document).ready(function() { 

        $("#faq_search_input").keyup(function() 

        {

            var faq_search_input = $(this).val();

            var dataString = 'keyword='+ faq_search_input;


            var ref_id = $('#ref_id').val(); 

            var cust_id = $('#cust_id').val(); 

            var current_url = $('#current_url').val(); 




            /*This is the minimum size of search string. Search will be only done when at-least 3 characters are provided in input*/

            if(faq_search_input.length>3)

            {

                $.ajax({

                    type: "GET",

                    url: current_url+"/SearchEngine",

                    data: dataString,

                    /*Uncomment this if you want to send the additional data*/

                    //data: dataString+"&refid="+ref_id+"&custid="+cust_id,

                    beforeSend:  function(){

                        $('input#faq_search_input').addClass('loading');

                    },

                    success: function(server_response)

                    {

                        $('#searchresultdata').html(server_response).show();

                        $('span#faq_category_title').html(faq_search_input);


                        if ($('input#faq_search_input').hasClass("loading")) {

                            $("input#faq_search_input").removeClass("loading");

                        } 

                    }

                });

            }

            return false;

        });

    });

    </script>

 

<?php 

    /*You need to change the URL as per your requirements, else this will show error page*/

    $model_name=Yii::app()->controller->id;

    $current_url=$baseUrl."/".$model_name;


    /*To Send the additional data if needed*/

    $reference_id = 88;

    $customer_id = 77;

    //echo "Search   :".$current_url;

?>

 

    <input type="hidden" id="current_url" value="<?php echo $current_url;?>"/> 

    <!-- if YOU WANT TO SEND ADDITIONAL HIDDEN VARIABLES-->

    <input type="hidden" id="ref_id" value="<?php echo $reference_id ;?>"/> 

    <input type="hidden" id="cust_id" value="<?php echo $customer_id ;?>"/>  

    Enter Hotel<br><br>

    <!-- The Searchbox Starts Here  -->

    <form  name="search_form">

        <input  name="query" type="text" id="faq_search_input" style="background-color: #FFFFFF" />

    </form>

    <!-- The Searchbox Ends  Here  -->

    <div id="searchresultdata" class="faq-articles"> </div>

</div>



Here every thing still remains

And this is [b]_ajax_search.php


[/b]

<?php

    foreach ($results as $row)

    {

        echo "<br>";

        echo "Name: ".$row['hotel_name']."   ";

        echo "Hotel id ".$row['hotel_id']."   ";

    }

?>



To explain more I’ve attached 2 pics:

First one is when I’m typing “asia” in the text field.

The live search is not working like google does.

3525

database-live-search1.png

Second picture shows that the _ajax_search view doesn’t show up. And we’re back to homepage for some reason.

3526

database-live-search2.png

Very painful!

I’ve searched in the forum, some people have faced this issue but sounds like nothing has been resolved.

This is very handy and powerful tool (in my opinion) but I’m very surprised because the number of posts related to the extension is very few in our forum. On the extension page I see Downloaded: 575 times. Maybe every body has no problem getting this extension to work?

Anyway thank you for your time and look forward to see any reply from you!

Cheers!

Hung

Hello,

I have an additionnal question about this part :

/You need to change the URL as per your requirements, else this will show error page/

$model_name=Yii::app()->controller->id;

$current_url=$baseUrl."/".$model_name;

Could someone please provide an example of what to enter here ?

Right now when I try and reach the freeSearch view, I get a 403 error.

Thanks :)

Jonathan

Hi,

I’m also looking into this extension. Hope we can find a way to make this work.

Thanks,

abc

Anyone plz help me . I can’t fix this issue .