eggshot
(Arsenical A)
#1
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,
);
}
}
redguy
(Maciej Lizewski)
#2
add:
echo CJSON::encode( $arr );
at the end.
traprajith
(Traprajith)
#3
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.
traprajith
(Traprajith)
#4
or if only wants the data simply echo that at the end
echo CJSON::encode( $arr );
u will get that in the ajax success .
eggshot
(Arsenical A)
#5
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);
}'));?>
eggshot
(Arsenical A)
#7
what do you mean ajax call code?
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'student-info',
'action'=>Yii::app()->createUrl('engineering/fetchregularload'),
'enableAjaxValidation'=>false,
)); ?>
traprajith
(Traprajith)
#8
<?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),
);
*/
}
traprajith
(Traprajith)
#9
i mean this
<?php echo CHtml::ajaxSubmitButton("Generate", array('success' => 'js:function(data) {
jQuery("#leftcontent").html(data);
}'));?>
got it
eggshot
(Arsenical A)
#10
it will automatically call the action in my form right?
traprajith
(Traprajith)
#11
i didnt get u. any way u may simply echo something in public function actionFetchregularload , is leftcontent refreshing with that? check that
eggshot
(Arsenical A)
#12
It doesnt refresh…its content doesnt change.
traprajith
(Traprajith)
#13
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());?>
traprajith
(Traprajith)
#14
i think u just need a ajaxlink
is it posting anything to that action?
eggshot
(Arsenical A)
#15
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?
driver_by
(Driver By)
#16
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).
driver_by
(Driver By)
#17
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);
traprajith
(Traprajith)
#18
u may render a view to display that data $arr. that is the best option. what is the functionality u want to achieve?