This is a free search extension to search in database. This extension produces the results as we type and the search narrows down as we type more and more.This extension is incredibly helpful for quick search "Something like google does".
Requirements ¶
Yii 1.1 or above NOTE:- try to keep less number of columns for fast and accurate results
Usage ¶
Firstly create a folder called js in root (where the file index.php is there) and paste the file the file jquery.js (You can download this file from attachment or from jquery site)
Then we need to edit Model Class and add the following function. This will be the engine that will provide free search in database and will return the results. This function is currently returning model (Tested) , but you can also return the CActiveDataProvider.
public function freeSearch($keyword)
{
/*Creating a new criteria for search*/
$criteria = new CDbCriteria;
$criteria->compare('name', $keyword, true, 'OR');
$criteria->compare('barcode', $keyword, true, 'OR');
$criteria->compare('part_number', $keyword, true, 'OR');
/*result limit*/
$criteria->limit = 100;
/*When we want to return model*/
return Items::model()->findAll($criteria);
/*To return active dataprovider uncomment the following code*/
/*
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
*/
}
Then we will be creating two actions (functions) in the controller class to perform this search. The first function (actionFreeSearch()) will be the main static page where we will provide the keyword. This page will send request to next function with the keyword parameter (actionSearchEngine($keyword)). This keyword is then send to model which will perform the search on that keyword and will return the results.
public function actionFreeSearch()
{
$model=new Items('search');
$this->render('freeSearch',array('model'=>$model));
}
public function actionSearchEngine($keyword)
{
// echo "THIS IS IAJAXX ".$keyword;
$model=new Items();
$model->unsetAttributes(); // clear any default values
$results=$model->freeSearch($keyword);
$this->renderPartial('_ajax_search',array(
'results'=>$results,
));
}
Also you need to modify the access rules
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','InboundSearch','OutboundSearch','admin','FreeSearch','SearchEngine'),
'users'=>array('@'),
),
Now you need to create two views. First one for the actionFreeSearch() function which will be 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 Item Name Part Number or barcode<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>
Lastly you need to create a file _ajax_search.php for presenting the database results. This file is getting called every time we are typing each character.
foreach ($results as $row)
{
echo "<br>";
echo "Name: ".$row['name']." ";
echo "Part Number: ".$row['part_number']." ";
echo "Barcode ".$row['barcode']." ";
}
Why this need?
Yii has built-in extension for this http://www.yiiframework.com/doc/api/1.1/CAutoComplete
Not work on different fields of database tables with one keyword
CAutocomplete does not do a live search on database tables. What if you are not sure what field you want to search in. This will do a search on all fields
Thanks for your great extension.
Thanks for your amazing work....
Can this accomplish something like the search in this forum?(or like google site search?)
display all relative result???
That will be great...
It gives results as you type
Yaa it is something like what google does
So create a new model+view+controller?
So I need to create a new model+view+controller using your codes?
or I should revise some M/V/C file?
Add ur own MVC
Yes u need to add new MVC....also chande the table field name as required
help .. cant make it work ..
i cant make it work cananyone send me their example working code or a gist with steps .. i have spent a lot of time to make it work .. but cant figure it out .. please help .. thanks
not working properly for me....
i completed all the thing as above suggest.here,search field created and after that when i try to search anything from database using search field then it goes to the home page and not search anything...where i do mistake give me some suggestion which is appreciated me..
thanks.
Same here, it's not working for me
Hi,
Is there someone here who tried, aside from the developer, who was able to make this work for their project?
Same here, I even tried to tweak into the implementation. Maybe I'm just missing something or lack knowledge of how's the inner workings.
Regards,
a.c.
Help
where i can put the accessrule and how can i mange this code $model_name=Yii::app()->controller->id; my model name is Visitor
Use as rule similar to index
Rajib_NSU
Why you need to put the access rule. You can try first as under general user. where index file is there
Sudeep
Anyone else have problem
I am happy to resolve if any other person have any problem in this. Kindly put your error message as well
Sudeep
error
Hi sudeep
I am doing all the procedure.but the searching is not working. Could you help me?
Help me please
how to change this link $model_name=Yii::app()->controller->id; any one help me plz.
more details
Can you give me more details on tha, possibly your comeplete code
Detail code
I am create a controller name visitor and it's code is following:
public function actionFreeSearch()
{ $model=new Visitor('search'); $this->render('freeSearch',array('model'=>$model)); } public function actionSearchEngine($keyword) { // echo "THIS IS IAJAXX ".$keyword;
// $keyword='1814';
$model=new Visitor(); $model->unsetAttributes(); // clear any default values $results=$model->freeSearch($keyword); $this->renderPartial('_ajax_search',array( 'results'=>$results, )); }
And the model code is:
public function freeSearch($keyword)
{ /*Creating a new criteria for search*/ $criteria = new CDbCriteria; $criteria->compare('visitor_mobile_number', $keyword, true, 'OR');
// $criteria->compare('address', $keyword, true, 'OR');
// $criteria->compare('mobile', $keyword, true, 'OR');
/*result limit*/ $criteria->limit = 100; /*When we want to return model*/ return Visitor::model()->findAll($criteria); /*To return active dataprovider uncomment the following code*/
// return new CActiveDataProvider($this, array(
// 'criteria'=>$criteria,
// ));
//
then the two view page is:
freesearch.php code :
<?php
/To import the client script/
$baseUrl = Yii::app()->baseUrl;
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($baseUrl.'/js/jquery.js');
?>
$(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>2)
{
$.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;
});
});
<?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 Item Name Part Number or barcode<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>
_ajax_search.php code:
<?php
foreach ($results as $row)
{
echo "
";
echo "Name: ".$row['guest_name']." ";
echo "Address: ".$row['company_address']." ";
echo "Mobile ".$row['visitor_mobile_number']." ";
}
?>
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.