Search in tabular data

Hello,

I have an action like this:




public function actionViewPresents($id){


$presents = Present::model()->findAll(array(

         'with'=>'student',

         'order'=>'student.lastname, student.firstname, t.studentid, presentdate',

         'condition'=>'schoolclassid=:schoolclassid',

         'params'=>array(':schoolclassid'=>$id),

      ));


$presentdates = Present::model()->findall(array(

        'condition'=>'schoolclassid=:schoolclassid',

        'params'=>array(':schoolclassid'=>$id),

        'select'=>'presentdate, schoolclassid', 

        'distinct'=>true, 


));


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

                'presentdates'=>$presentdates,

                'presents'=>$presents));





}



and my view is like this:




     <div class="panel panel-primary">

           <div class="panel-heading">

                <?php

    foreach  ($presents as $t){


        echo $t->schoolclass->description;

        break;

    }

?>




              </div>

         </div>

       <?php

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

    $('.search-button').click(function(){

    $('.search-form').toggle();

    return false;

});

$('.search-form form').submit(function(){

    $.fn.yiiGridView.update('payment-grid', {

        data: $(this).serialize()

    });

    return false;

});

");

   ?>







<div class="panel panel-default">

            <div class="panel-body">

<?php   

echo '<table id="presents">';

echo '<thead>';

echo '<tr>';

echo '<th>'.Yii::t('default', 'Full Name').'</th>';


foreach ($presentdates as $p) {

    $pdat = DateTime::createFromFormat('d/m/Y', $p->presentdate)->format('Y-m-d');    

    echo '<th>'.date("D",strtotime($pdat)).' '.date("d",strtotime($pdat)).'</th>';


}


echo '</tr>';

echo '</thead>';

echo '<tbody>';


$prvid=0;


foreach ($presents as $row) {




    if($prvid!=$row->studentid) {

       echo '<tr>';

       echo '<td>'.$row->student->lastname. ' '. $row->student->firstname.'</td>';

       $prvid=$row->studentid;

    }


    if ($row['ishere']==1) {

       echo '<td><div class="icon-large icon-check"></div></td>';

    }  else {

       echo '</td>';

    }





}


echo '</tbody>';

echo '</table>';  


?>

                </div>

     </div>

<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(

    'action'=>Yii::app()->createUrl($this->route),

    'method'=>'get',

)); ?>


<div class="panel panel-default">

            <div class="panel-body">

<?php 


/*

                'day1s',

        'day1e',

        'day2s',

        'day2e',

        'day3s',

        'day3e',

        'day4s',

        'day4e',

        'day5s',

        'day5e',

        'day6s',

        'day6e',

        'day7s',

        'day7e', 

 */

     foreach ($presentdates as $k) {

        $k->presentdate=Date('m');

    }


     //var_dump($k);exit;

    echo $form->dropDownList($k, 'presentdate', array( 

              '01'=>Yii::t('default', 'January'). ' ' .date("Y"),

              '02' => Yii::t('default', 'February'). ' ' .date("Y"), 

              '03'=>Yii::t('default', 'March'). ' ' .date("Y"), 

              '04'=>Yii::t('default', 'April'). ' ' .date("Y"), 

              '05'=>Yii::t('default', 'May'). ' ' .date("Y"), 

              '06'=>Yii::t('default', 'June'). ' ' .date("Y"),

              '07'=>Yii::t('default', 'July'). ' ' .date("Y"), 

              '08'=>Yii::t('default', 'August'). ' ' .date("Y"), 

              '09'=>Yii::t('default', 'September'). ' ' .date("Y"), 

              '10'=>Yii::t('default', 'Octomber'). ' ' .date("Y"), 

              '11'=>Yii::t('default', 'November'). ' ' .date("Y"),  

              '12'=>Yii::t('default', 'December'). ' '.date("Y"),),

      array(

              'prompt'=>Yii::t('default', 'Select a month'),


            )); 




    ?>


    <div class="form-actions">

        <?php $this->widget('bootstrap.widgets.TbButton', array(

            'buttonType' => 'submit',

            'type'=>'primary',

            'label'=>Yii::t('default', 'Search'),

                        'htmlOptions'   => array('name'=> 'search'),

        )); ?>

    </div>


    </div>

        </div>       




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




as you can see i have a dropdownlist in which i pass manually some values(months). The thing I want to do is perform a search based on the selection of a month. E.g. when I select January and hit search I want to retrieve the records of January and display it in my view. I’m new to Yii and I’m having trouble wrapping my head around it. If anyone could help I would much appreciate it! So far when I hit search I get an Error 400. I probably should perform $_GET in my action but I’m not sure how to do it. Thanx in advance

you dont need a form inside your view. to be able to search, see functionAdmin in controller. this is only an example, you should adjust to your code.

modify your Present model. make a search base on parameter you need





	public function searchById($id) {




                    $criteria = new CDbCriteria(array(

                        'condition' => 'present_id=' . $id,

                        'order' => 'present_id ASC',

                    ));  

                

		$criteria->compare('Present', $this->present_id);

		

                //other compare


		return new CActiveDataProvider($this, array(

			'criteria' => $criteria,

		));

	}        



controller[size="2"] [/size]




	public function actionIndex($id)

	{


		$model=newPresent('searchById($id)');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Present']))[/size]

			$model->attributes=$_GET['Present'];[/size]


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

			'model'=>$model,

			'id'=>$id // dont forget to pass id value.

		));                

	}

index




        <?php 

            $this->widget('bootstrap.widgets.BsGridView',array(

                'id'=>'installation-grid',

                'dataProvider'=>$model->searchById($id),

                'filter'=>$model,

                'columns'=>array(

			'present_id',

                    array(

                        'name'=>'customer.customer_name',

                        'value'=>'$data->customer->customer_name',

                        'filter'=>BsHtml::activeTextField($model,'customer_name'), 

                    ),   

                    //other field,

                ),

            )); 

        ?>

ok, thats all. hopefuly will help you. :)

cheers.