How To Return The Fetched Data Back To My View?

How to return the fetched data (in this case: $arr) back to my view

(which i can manipulate like displaying in a table)

if i have this in my controller action processed via ajax:




public function actionFetchregularload(){

		$criteria = new CDbCriteria;

		$criteria->limit = 15;

		$criteria->condition='semester = 2';

		$criteria->select = array('subjCode','subjDesc','lec','lab','units');

		$criteria->addSearchCondition('yrLvl', $_GET['yrlevel']);

		$data = CompeProspectusA::model()->findAll($criteria);

		$arr = array();

		foreach ($data as $item){

			$arr[] = array(

				'subjcode' => $item->subjCode,

				'subjdesc' => $item->subjDesc,

				'lab' => $item->lab,

				'lec' => $item->lec,

				'units' => $item->units,

			);

		}

	}



add:

echo CJSON::encode( $arr );

at the end.

simply render ur view like this

renderPartial


$this->renderPartial('create',array(

			'arr'=>$arr,

		));

create- view file name

then you will get $arr as in your view

it is a basic thing.

or if only wants the data simply echo that at the end


echo CJSON::encode( $arr );

u will get that in the ajax success .


public function actionFetchregularload(){

		$criteria = new CDbCriteria;

		$criteria->limit = 15;

		$criteria->condition='semester = 2';

		$criteria->select = array('subjCode','subjDesc','lec','lab','units');

		$x = new CompeProspectusA;

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

			$x->attributes=$_POST['CompeProspectusA'];

		}

		$criteria->addSearchCondition('yrLvl',$x->yrLvl);

		$data = CompeProspectusA::model()->findAll($criteria);

		$arr = array();

		foreach ($data as $item){

			$arr[] = array(

				'subjcode' => $item->subjCode,

				'subjdesc' => $item->subjDesc,

				'lab' => $item->lab,

				'lec' => $item->lec,

				'units' => $item->units,

			);

		}

	    echo CJSON::encode( $arr );

		/*

		$this->renderPartial(

			'admin', array

			('arr'=>$arr), 

		);

		*/

				

	}

Not sure if im doing ryt with $x and $x->yrLvl ??

In my ajaxsubmitbutton:




<?php echo CHtml::ajaxSubmitButton("Generate", array('success' => 'js:function(data) {

		 jQuery("#leftcontent").html(data);

		}'));?>



post ur ajax call code

what do you mean ajax call code?




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

					'id'=>'student-info',

					'action'=>Yii::app()->createUrl('engineering/fetchregularload'),

					'enableAjaxValidation'=>false,

		)); ?>






<?php echo CHtml::ajaxSubmitButton("Generate", array('success' => 'js:function(data) {

                 jQuery("#leftcontent").html(data);

                }'));?>



u will get this echo CJSON::encode( $arr ); here as (data)

try




public function actionFetchregularload(){

                $criteria = new CDbCriteria;

                $criteria->limit = 15;

                $criteria->condition='semester = 2';

                $criteria->select = array('subjCode','subjDesc','lec','lab','units');

                $x = new CompeProspectusA;

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

                        $x->attributes=$_POST['CompeProspectusA'];

                }

                $criteria->addSearchCondition('yrLvl',$x->yrLvl);

                $data = CompeProspectusA::model()->findAll($criteria);

                $arr = array();

                foreach ($data as $item){

                        $arr[] = array(

                                'subjcode' => $item->subjCode,

                                'subjdesc' => $item->subjDesc,

                                'lab' => $item->lab,

                                'lec' => $item->lec,

                                'units' => $item->units,

                        );

                }

            //echo CJSON::encode( $arr );


             print_r($arr);

                /*

                $this->renderPartial(

                        'admin', array

                        ('arr'=>$arr), 

                );

                */

                                

        }



i mean this


<?php echo CHtml::ajaxSubmitButton("Generate", array('success' => 'js:function(data) {

                 jQuery("#leftcontent").html(data);

                }'));?>

got it

it will automatically call the action in my form right?

i didnt get u. any way u may simply echo something in public function actionFetchregularload , is leftcontent refreshing with that? check that

It doesnt refresh…its content doesnt change.

try like this…this is working for me




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

						'id'=>'search-form',

						'action'=>'/site/manage',

                        'method'=>'GET',


								

							)); ?>





<?php  echo CHtml::ajaxButton("Apply",CController::createUrl('/site/manage'), array(

		'data' => 'js:$("#search-form").serialize()',

		'update' => '#student_panel_handler',

		'type'=>'GET',

		),array());?>



i think u just need a ajaxlink

is it posting anything to that action?

It’s now working but CJSON will return a “messy” data.

How will I make it into a tabular form?

How will I prevent returning unnecessary data if I submitted it with empty fields?

addSearchCondition should be in if section:


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

    $x->attributes=$_POST['CompeProspectusA'];

    $criteria->addSearchCondition('yrLvl',$x->yrLvl);

}

Method should be "post"


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

    'id'=>'student-info',

    'method' => 'post',

    'action'=>Yii::app()->createUrl('engineering/fetchregularload'),

    'enableAjaxValidation'=>false,

)); ?>

check data returned by action:


<?php echo CHtml::ajaxSubmitButton("Generate", array('success' => 'js:function(data) {

    alert(data);

    jQuery("#leftcontent").html(data);

}'));?>

Finally you need to renderPartial view (for example "_table") which contains markup for your table (or something else you want to see with your $arr).

Also do not need to create two different AR models. Just use this code:


$criteria = new CDbCriteria;

    $criteria->limit = 15;

    $criteria->condition='semester = 2';

    $criteria->select = array('subjCode','subjDesc','lec','lab','units');    

    if(isset($_POST['CompeProspectusA']['yrLvl'])){

        $criteria->addSearchCondition('yrLvl',$_POST['CompeProspectusA']['yrLvl']);

    }

    $data = CompeProspectusA::model()->findAll($criteria);



u may render a view to display that data $arr. that is the best option. what is the functionality u want to achieve?