How To Both Variable Work Togather In Ajex Search Filter.....

hi friends…

i use the search feature using the ajex filter…

here i have 3 table

1-> tbl_product_index(product_id,master_Key,product_name)

2-> tbl_master_index(master_key,brand)

3-> tbl_pricing(product_id,price,mobile_type)

relation create by the gii…

here is my code…

in productindexcontroller:

actionindex is




     public function actionIndex()

	{

	$model = new ProductIndex();

    $master_key = (isset($_GET['ProductIndex']['master_key'])) ? $_GET['ProductIndex']['master_key'] : array();

    $product_id = (isset($_GET['ProductIndex']['product_id'])) ? $_GET['ProductIndex']['product_id'] : array();

	$result = array_merge((array)$master_key, (array)$product_id);

    CVarDumper::dump($master_key);

	CVarDumper::dump($product_id);

     $criteria = new CDbCriteria();

	 //take the other table column

	 $criteria->with = 'pricing'; 

	 $criteria->together = true; 

	if( count( $master_key ) > 0 )

	{

	$criteria->addInCondition( 'master_key', $master_key );  

	}

   if( count( $product_id ) > 0 )

	{

	 $criteria->addInCondition( 'pricing.mobile_type', $product_id );

	}

	$dataProvider = new CActiveDataProvider('ProductIndex',

	array('criteria'=>$criteria));

             $this->render('index',array(

            'dataProvider'=>$dataProvider,

			'model'=>$model,

            ));

			}

and in view is

index.php:




<h4>Brand:</h4>

<?php

Yii::app()->clientScript->registerScript('search',

    "var ajaxUpdateTimeout;

    var ajaxRequest;

	$('.masterindexFilter').change(function(){

	master_key = $('.masterindexFilter').serialize();

    $.fn.yiiListView.update(

        'ajaxListView',

                {

                 url: '" . CController::createUrl('productindex/index') . "',

                 data: master_key,

				}

    );

});

	$('.pricingFilter').change(function(){

    product_id = $('.pricingFilter').serialize();

    $.fn.yiiListView.update(

        'ajaxListView',

                {

                 url: '" . CController::createUrl('productindex/index') . "',

                 data: product_id,

				}

    );

});

$('input#productindex').keyup(function(){

        ajaxRequest = $(this).serialize();

        clearTimeout(ajaxUpdateTimeout);

        ajaxUpdateTimeout = setTimeout(function () {

            $.fn.yiiListView.update(

// this is the id of the CListView

                'ajaxListView',

                {data: ajaxRequest}

            )

        },

// this is the delay

        300);

    });"

);

?>

<?php

echo CHtml::activeCheckboxList(

 $model,  'master_key', 

  CHtml::listData(MasterIndex::model()->findAll(), 'master_key', 'brand'),

  array('template'=>'<li>{input} {label}</li>',  'class'=>'masterIndexFilter',)

);

?>

<h4>OS:</h4>

		<?php

echo CHtml::activeCheckBoxList($model,'product_id',

CHtml::listData(Pricing::model()->findAll(), 'mobile_type', 'mobile_type'),

		array( 'template'=>'<li>{input} {label}</li>',  'class'=>'pricingFilter',)); 

		?>

<?php

 $this->widget('zii.widgets.CListView', array(

'dataProvider'=>$dataProvider,

'itemView'=>'_new',

'id'=>'ajaxListView',

)); 

?> 

here i am able to search brand and mobile_type individually but not together because when i select brand checkbox then it select brand and store the value in $master_key.and when i select os(mobile_type) checkbox that time store the value in $product_id and $master_key is null.

i want to store the value in both the variable together one after other and search both together.

also i want to know that




$master_key = (isset($_GET['ProductIndex']['master_key'])) ? $_GET['ProductIndex']['master_key'] : array();

    $product_id = (isset($_GET['ProductIndex']['product_id'])) ? $_GET['ProductIndex']['product_id'] : array();

is any way to write this code in one variable in place of two variable?

any help pls.

thanks in advance.

please help me anyone?

Hi rajkumar000000,

I would create a form that includes both of the checkboxlists and … probably




$('#form').on('change', 'input:checkbox', function(event){

    $.fn.yiiListView.update(

        'ajaxListView',

        {

           url: '" . CController::createUrl('productindex/index') . "',

           data: $('#form').seriarize()

        }

    );

});



thanks a lot softark…

i have done this but it is not working.i select checkbox after that it select but not progress,nothing is happening.

before work all thing except not search together.

how to search together?

or

how to both variable store in a single variable?

please give me other suggestion…which is appreciated to me…

Did you wrap all the checkboxes in a form?

You are now submitting checkbox values of master_key and those of product_id individually. Wrap those checkboxes all in a single form and submit them together.

yes,i wrap all the checkboxes in a form.

here is my code

index.php




 <?php


$this->breadcrumbs=array(

	'Product Indexes',

);


?>


<h2 class="product-index-heading">Product Indexes</h2>


<!-- add a search box: -->

<h4>Brand:</h4>

<?php

Yii::app()->clientScript->registerScript('search',

    "var ajaxUpdateTimeout;

    var ajaxRequest;

$('#form').on('change', 'input:checkbox', function(event){

    $.fn.yiiListView.update(

        'ajaxListView',

        {

           url: '" . CController::createUrl('productindex/index') . "',

           data: $('#form').seriarize()

        }

    );

});

$('input#productindex').keyup(function(){

        ajaxRequest = $(this).serialize();

        clearTimeout(ajaxUpdateTimeout);

        ajaxUpdateTimeout = setTimeout(function () {

            $.fn.yiiListView.update(

// this is the id of the CListView

                'ajaxListView',

                {data: ajaxRequest}

            )

        },

// this is the delay

        300);

    });"

);

?>

<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'product-index-form',

	'enableAjaxValidation'=>false,

	'htmlOptions' => array('enctype' => 'multipart/form-data'), // ADD THIS

)); ?>

  <?php echo $form->errorSummary($model); ?>

<?php

echo CHtml::activeCheckboxList(

 $model,  'master_key', 

  CHtml::listData(MasterIndex::model()->findAll(), 'master_key', 'brand'),

  array('template'=>'<li>{input} {label}</li>',  'class'=>'masterIndexFilter',)

);

echo CHtml::activeCheckBoxList($model,'product_id',

CHtml::listData(Pricing::model()->findAll(), 'mobile_type', 'mobile_type'),

		array( 'template'=>'<li>{input} {label}</li>',  'class'=>'pricingFilter',)); 

		echo CHtml::submitButton('Search'); ?>

		<?php $this->endWidget(); ?>

 

</div><!-- form -->


<?php

 $this->widget('zii.widgets.CListView', array(

'dataProvider'=>$dataProvider,

'itemView'=>'_new',

'id'=>'ajaxListView',

)); 

?> 



when i select together and submit the form then not the pass the value, that’s cause it is not work for search and stay at the same position.

$masterkey AND $product_id is null.

thanks softark…

i did it with the help of yours.

thanks a lot…




<?php

Yii::app()->clientScript->registerScript('search', "

$('#product-index-form').on('change', 'input:checkbox', function(event){

    $.fn.yiiListView.update(

        'ajaxListView',

        {

           url: '" . CController::createUrl('productindex/index') . "',

           data: $('product-index-form').seriarize()

        }

    );

});"

);

?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'product-index-form',

	'enableAjaxValidation'=>false,

)); ?>

...

<?php echo CHtml::submitButton('Search'); ?>

<?php $this->endWidget(); ?>

</div><!-- form -->



You have to change $(’#form’) to $(’#product-index-form’), because you have defined the id of the form as “product-index-form”.

thanks…

now i have another problem.it works proper for the together but give the error when select only one either of them.

the error is

reset() expects parameter 1 to be array, string given .

Well, it should be the problem in the controller.

Where do you use reset() ?

i am not use reset in my controller…IT is predefine in yii framework.

error is:




 reset() expects parameter 1 to be array, string given


C:\xampp\htdocs\yii\framework\db\schema\CDbCriteria.php(274)


262      * @param string $column the column name (or a valid SQL expression)

263      * @param array $values list of values that the column value should be in

264      * @param string $operator the operator used to concatenate the new condition with the existing one.

265      * Defaults to 'AND'.

266      * @return CDbCriteria the criteria object itself

267      */

268     public function addInCondition($column,$values,$operator='AND')

269     {

270         if(($n=count($values))<1)

271             $condition='0=1'; // 0=1 is used because in MSSQL value alone can't be used in WHERE

272         elseif($n===1)

273         {

274             $value=reset($values);

275             if($value===null)

276                 $condition=$column.' IS NULL';

277             else

278             {

279                 $condition=$column.'='.self::PARAM_PREFIX.self::$paramCount;

280                 $this->params[self::PARAM_PREFIX.self::$paramCount++]=$value;

281             }

282         }

283         else

284         {

285             $params=array();

286             foreach($values as $value)




here is my code for the index:

controller:




 public function actionIndex()

	{

	$model = new ProductIndex();

	$master_key = (isset($_POST['ProductIndex']['master_key'])) ? $_POST['ProductIndex']['master_key'] : array();

    $product_id = (isset($_POST['ProductIndex']['product_id'])) ? $_POST['ProductIndex']['product_id'] : array();

	$result = array_merge((array)$master_key, (array)$product_id);

    CVarDumper::dump($master_key);

	CVarDumper::dump($product_id);

     $criteria = new CDbCriteria();

	 //take the other table column

	 $criteria->with = 'pricing'; 

	 $criteria->together = true; 

	 //$productindex = ProductIndex::model()->with('pricing')->findAll($criteria);

	if( count( $master_key ) > 0 )

	{

	$criteria->addInCondition( 'master_key', $master_key );  

	}

	

   if( count( $product_id ) > 0 )

	{

	 $criteria->addInCondition( 'pricing.mobile_type', $product_id );

	}

	$dataProvider = new CActiveDataProvider('ProductIndex',

	array('criteria'=>$criteria));

             $this->render('index',array(

            'dataProvider'=>$dataProvider,

			'model'=>$model,

            ));

			}



how to put the condition to work for 1st variable, 2nd variable, and together?

pls suggest me.

Dear Friend

I just simulated your scenario in my localhost.

I have 3 models.

1.People (id,name,r_id,s_id).

2.Status(id,name),

3.Region (id,name).

The following are relations declared in People model.




public function relations()

	{

		return array(

		'status'=>array(self::BELONGS_TO,'Status','s_id'),

		'region'=>array(self::BELONGS_TO,'Region','r_id'),

		);

	}



The following is the controller logic for listing the people.




public function actionIndex($rid=null,$sid=null)

	{   

            $criteria=new CDbCriteria;


	    if($rid!==null && $rid!=="")

		    $criteria->addCondition("r_id=$rid");


	    if($sid!==null && $sid!=="")

                    $criteria->addCondition("s_id=$sid");


	    $dataProvider=new CActiveDataProvider('People',array(

			'criteria'=>$criteria,

		));


		$this->render('index',array(

			'dataProvider'=>$dataProvider,

			'model'=>new People,

		));

	}



Following is code for the index.php




<div class="row">

<!--assigned id region for this dropdown-->		

<?php echo CHtml::activeDropDownList($model,'r_id',

	CHtml::listData(Region::model()->findAll(),'id','name'),array('id'=>'region','prompt'=>'select region')); ?>

		

</div>


<div class="row">

<!--assigned id status for this dropdown-->		

<?php echo CHtml::activeDropDownList($model,'s_id',

	CHtml::listData(Status::model()->findAll(),'id','name'),array('id'=>'status','prompt'=>'select status')); ?>

		

	</div>


<h1>Peoples</h1>


<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

	'id'=>'people' //assigned id people.

)); ?>


<?php

Yii::app()->clientScript->registerScript('people','


$("body").on("change","#region, #status",function(){

	$.fn.yiiListView.update("people",{data:{rid:$("#region").val(),sid:$("#status").val()}});

	});




');




If you are going to select multiple values from dropDown, then we can make the query in the following way.

Befor that ensure that $rid AND $sid exists in array format.




public function actionIndex($rid=null,$sid=null)

        {   

            $criteria=new CDbCriteria;


            if($rid!==null && $rid!==array())

                    $criteria->addInCondition("r_id",$rid);


            if($sid!==null && $sid!==array())

                    $criteria->addInCondition("s_id",$sid);

.....................................................................

.....................................................................



I hope I helped a bit…

Regards.