Hi!
I’ve hit a wall here.
I’m using a form to search the db.
After submiting the form I use CGridView to show the data. The data shows fine, but if I try to sort, change page, CGridView breaks, shows me a part of the menu instead of a table with data.
My search controller is this:
class SearchController extends Controller
{
public $searchType;
public $sortType;
public $channel;
public $startDate;
public $endDate;
public $defaultAction = 'SearchStat';
public function actionSearchStat()
{
$model=new SearchStatForm;
if(isset($_POST['SearchStatForm']))
{
$model->attributes=$_POST['SearchStatForm'];
if($model->validate($model))
{
$criteria = new CDbCriteria();
$criteria->compare('date', '>'.$model->startDate);
$criteria->compare('date', '<'. $model->endDate);
$criteria->compare('content_id', $model->channel,true);
if($model->searchType == 'daily'){
$dataProvider=new CActiveDataProvider(
'StatDaily',
array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>86,
),
)
);
}elseif($model->searchType == 'monthly'){
//code will go here
}
$this->render('searchStatResults',array('dataProvider'=>$dataProvider));
}
}else{
$this->render('searchStat',array('model'=>$model));
}
}
}
My searchForm view:
<?php
/* @var $this SiteController */
/* @var $model ContactForm */
/* @var $form CActiveForm */
$this->pageTitle=Yii::app()->name . ' - Search Stats';
$this->breadcrumbs=array(
'Stats',
);
?>
<h1>Channel statistics</h1>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'searchStat-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'searchType'); ?>
<?php
echo $form->dropDownList(
$model,
'searchType',
$model->getTypeOptions()
);?>
<?php echo $form->error($model,'searchType'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'sortType'); ?>
<?php echo $form->radioButtonList($model,'sortType' ,array('viewers'=>'Viewers', 'time'=>'Time'),
''); ?>
<?php echo $form->error($model,'sortType'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'channel'); ?>
<?php
echo $form->dropDownList(
$model,
'channel',CHtml::listData(Channel::model()->findAll(),'id_channel','channel_tag'),
array('empty'=>array(0=>'All channels'))
);?>
<?php echo $form->error($model,'channel'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'startDate'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name'=>'SearchStatForm[startDate]',
'value'=>$model->startDate,
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
));
?>
<?php echo $form->error($model,'startDate'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'endDate'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name'=>'SearchStatForm[endDate]',
'value'=>$model->endDate,
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
));
?>
<?php echo $form->error($model,'endDate'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
My searchResults view:
<h1>Search results</h1>
<?php if (!empty($dataProvider)): ?>
<?php var_dump($dataProvider);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
));?>
<?php else: ?>
<p class="error">No results matched your search terms.</p>
<?php endif; ?>
What am I doing wrong? I’ve been re-wrting this for a day and still can’t see what I’m doing wrong.