Yii Framework Forum: How Can I Write Search Condition Using Criteria - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How Can I Write Search Condition Using Criteria Rate Topic: -----

#1 User is offline   rajkumar000000 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 88
  • Joined: 18-September 12

Posted 05 December 2012 - 02:20 AM

i have select one column from the table.
in my index page i have add two search field such as
Attached File  product.PNG (32.36K)
Number of downloads: 21
i want to both the search field active at a time and search the column elements.
so, what i condition put in cdbcriteria in model or controller ?
please suggest me..
thanks

in controller :
public function actionIndex()
	{
	$criteria=new CDbCriteria;
	

	$dataProvider=new CActiveDataProvider('ProductIndex',array('criteria'=>array('select'=>'product_name',
					'condition'=>array('where'=>array('and','product_name=micromax100','product_name=lalala')))
					));
	$model = new ProductIndex('search');
    $model->unsetAttributes();
	if(isset($_GET['q']))
		$model->product_name = $_GET['q'];	
			 $model->save();
		$this->render('index',array(
			'dataProvider'=>$model->search()
			));
	}


in index.php
//this adds a javascript event listener to the search box that will query the server with each keystroke. yw0 is the id of the clistview. q is the id of the text input box:
Yii::app()->clientScript->registerScript('search', "
  $('input#q').keyup(function(){
  $.fn.yiiListView.update('yw0', {
  data: $(this).serialize()
  });
  return false;
});
");

?>

<h1>Product Indexes</h1>

<!-- add a search box: -->
<input type="textfield" id="q" name="q" value="mxn100" />
<input type="text" id="q" name="q" value="lalala" />
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>


in model:
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('index',$this->index);
		$criteria->compare('product_name',$this->product_name,true);
	        $criteria->compare('product_id',$this->product_id,true);
		$criteria->compare('image_index',$this->image_index,true);
		$criteria->compare('master_key',$this->master_key);
		$ProductIndex=ProductIndex::model()->findAll($criteria);
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			
					));
	
	}


please suggest me what condition put in this code for the working of the both search field together?
0

#2 User is offline   rajkumar000000 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 88
  • Joined: 18-September 12

Posted 06 December 2012 - 01:19 AM

please anyone give me some suggestion.......
thanks
0

#3 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,032
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 06 December 2012 - 01:51 AM

I am not sure if this what you looking for

public function actionIndex()
        {
        $criteria=new CDbCriteria;
        

        $dataProvider=new CActiveDataProvider('ProductIndex',array('criteria'=>array('select'=>'product_name',
                                        'condition'=>array('where'=>array('and','product_name=micromax100','product_name=lalala')))
                                        ));
        $model = new ProductIndex('search');
    $model->unsetAttributes();
        if(isset($_GET['q']))
                $model->product_name = $_GET['q'];      
                         $model->save();
                $this->render('index',array(
                        'dataProvider'=>$model->search()
                        ));
        }




change it as following

public function actionIndex()
        {
       $criteria=new CDbCriteria;
 $criteria->select="product_name";
 $criteria->addInCondition('product_name',array('lalala', 'micromax100')); 

 $dataProvider=new CActiveDataProvider('ProductIndex',array(
 	'criteria'=>$criteria
 ));
        $model = new ProductIndex('search');
    $model->unsetAttributes();
        if(isset($_GET['q']))
                $model->product_name = $_GET['q'];      
                         $model->save();
                $this->render('index',array(
                        'dataProvider'=>$model->search()
                        ));
        }

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users