Hi everybody,
I want to have every place on my website a ‘search box’ in the top page.
The problem: it not possible uses $model data because only is possible to use this from the ‘view’. So, it is necessary some extension allow a call method from the controller to execute a sql sentence and return back an “echo array()” to displayed.
Any idea how to implement this?
Thanks for advance,
Totto.
¿Something idea or advice?
Hi,
At YiiFramework I found a clue after reading Front-end developer’s guide.
Here said: "Layout is a special view that is used to decorate views. It usually contains parts of a user interface that are common among several views"
Afterward to find how, I was ready for find with what, and find myself with Lucene y Sphinx, a search index tool for better MySql query’s.
I search in YiiFramework and found it how configure Zend Lucene. And it’s making a CPorlet widget and a controller, this last to creating a text index file to get search results.
When it is implemented I will publish a result.
I hope that will help someone.
Finally I achieved that worked!
Following the previous orders that you can see at this resulting skeleton and I hope that will help someone.
YourModel, create New Static Function: "namesAutoComplete"
public static function namesAutoComplete($term='')
{
$query = 'SELECT
...
FROM tbl_name
WHERE name LIKE :term
;
$connection = Yii::app()->db;
$command =$connection->createCommand($query);
$command->bindValue(':term', '%'.$term.'%', PDO::PARAM_STR);
return $command->queryAll();
}
NewController: "SearchController"
-
Task - action mainSearch: rendering selected results.
-
Task - action SuggestTask: Ajax action (call new method -namesAutoComplete-).
<?php
class SearchController extends Controller
{
public function actionMainSearch()
{
$this->layout='column1';
//Recovering search "term" and rendering from result (table).
if (($term = Yii::app()->getRequest()->getParam('tag_autocomplete', null)) !== null) {
$dataProvider = YourModel::taggingRoutes($term);
//Parseing if it's necesary!
//Rendering
$model = new YourModel;
$this->render('search', array(
'model' => $model,
'dataProvider' => $dataProvider,
'term' => $term,
));
}
}
public function actionSuggestName($term) {
if($term !='')
{
$tags = YourModel::namesAutoComplete($term);
$ret = array();
foreach($list as $object) {
$ret[] = $object['name'];
}
echo CJSON::encode($ret);
Yii::app()->end();
}else{
echo CJSON::encode(array('name'=>'Proof other combination...'));
Yii::app()->end();
}
}
}
Modify Column1.php:
-
Task: organizing html blocks (header, body -content- and footer), when header contained Search action
<?php /* @var $this Controller */ ?>
<?
$this->beginContent('//layouts/main');
?>
<!--div id="header"-->
<header>
<div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?>
<div id="search-form">
<?php
echo CHtml::beginForm(array('search/mainSearch'), 'get', array('style'=> 'inline'));
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'your_autocomplete',
'sourceUrl'=>$this->createUrl('search/suggestName'),
'options'=>array(
'minLength'=>'0',
),
'htmlOptions'=>array(
'size'=>45,
'maxlength'=>45,
'placeholder' => "Search...",
),
));
echo CHtml::submitButton('Search!',array('style'=>'width:70px;')) .
CHtml::endForm('');
?>
</div><!-- search-form -->
</div>
<div id="mainmenu">
...
</div><!-- mainmenu -->
<div id="breadcrumbs">
...
</div><!-- breadcrumbs -->
</header>
<!--div class="container" id="page"-->
<div id="content">
<?php echo $content; ?>
</div><!-- content -->
<!--/div--><!-- page -->
<div class="clear"></div>
<div id="footer">
...
</div><!-- footer -->
<?php $this->endContent(); ?>