Yii Framework Forum: Database-Live-Search Doesn't Work As Expected - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Database-Live-Search Doesn't Work As Expected Rate Topic: -----

#1 User is offline   Trinh Duy Hung 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 26-October 12

Posted 26 November 2012 - 02:47 AM

Hello everyone!

I'm following exactly the instructions in this extention post: http://www.yiiframew...se-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,
            ));
        }


3. 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('@'),
     ),


4. 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.
Attached File  database-live-search1.png (19.48K)
Number of downloads: 17
Second picture shows that the _ajax_search view doesn't show up. And we're back to homepage for some reason.
Attached File  database-live-search2.png (32.31K)
Number of downloads: 15
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
0

#2 User is offline   staticblue 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 07-January 13

Posted 11 January 2013 - 04:58 AM

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
0

#3 User is offline   ngii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-March 13

Posted 08 March 2013 - 07:05 PM

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

Thanks,

abc
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users