Forms And Cgridview?

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.

Hi,

Seems that you are using $_POST for form… it should be $_GET

Secondly, Store the $_POST data in a variable and then update it in case change as




$post=$_POST['SearchForm'];

$_SESSION['searchpost']=$post;


if(isset($_POST['SearchForm'])){

$post=$_POST['SearchForm'];

$_SESSION['searchpost']=$post;

}else{

$post=$_SESSION['searchpost'];

}

$model->attributes=$post;



thanks

I don’t see how saving $_POST in $_SESSION changes anything, but I’ve gone and did it, no change regarding CGridView.

After entering the form I get CGridView populated with data, but can’t navigate it.

First viewing:


<div id="content">

<h1>Search results</h1>

<div id="yw0" class="grid-view">

<div class="summary">Displaying 1-86 of 811 results.</div>

<table class="items">

<div class="pager">

<div class="keys" title="index.php?r=search/searchStat" style="display:none">

</div>

</div>

Second:


<div id="content">

<h1>Search results</h1>

<ul id="yw0">

I have CGridView working fine in another part of the app:


<div id="content">

<h1>find/index</h1>

<h1>Search results</h1>

<div id="yw0" class="grid-view">

<div class="summary">Displaying 11-20 of 3550 results.</div>

<table class="items">

<div class="pager">

<div class="keys" title="index.php?r=find%2Findex&ajax=yw0&StatDaily_page=2&StatDaily_sort=watched_time" style="display:none">

</div>

The difference is that the working CGridView has &ajax=divID in the links.

I would say that you have an element ID conflict. Last time I looked for example, the standard Yii CGridview search form duplicates IDs.

Check carefull for these (view page source).

Yeah, indeed there was one, but I added a custom id to the CGridview, still not wokring. :(

I think you should do as




 public function actionSearchStat()

        {               

                $model=new SearchStatForm;

                 if(!isset($_SESSION['postdata'])){

                    $_SESSION['postdata']=array();

                   }

                if(isset($_POST['SearchStatForm']))

                {

                $_SESSION['postdata']=$_POST['SearchStatForum'];        

                }


                 if(!empty($_SESSION['postdata']))){ 




                         $model->attributes=$_SESSION['postdata'];

                        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));

                }

        

        }




Hope this will help