Rendering Problems

Hi All,

I have a controller that is supposed to display a survey. The first view, user, is supposed to collect user data and then go to the list page, which will populate a list of questions from a database. It collects the data fine, but when the ‘next’ button is clicked, it doesn’t work like I think it should. For some reason, the user function in the controller seems to render both views inside one page. I can’t figure it out. Please take a look and let me know if you have any idea of whats going on. Let me know if I should post anything else.

Thanks!

PS I know all the formatting in the list view isn’t right, but thats not really my concern right now, just displaying the right page would be a good start!

here is the Controller Code:




 public function actionUser()

        {

                $model= new UserForm;


                if(isset($_POST['UserForm']))

                {

                        $model->attributes=$_POST['UserForm'];


                        if($model->validate())

                        {

                                $teacher =new User;

                                $teacher->name = $model->name;

                                $teacher->department = $model->department;


                                $teacher->save();

                                

                                $criteria = new CDbCriteria;


                                $criteria->addSearchCondition('section',0, true, 'AND');


                                $questions=Questions::model()->findAll($criteria);


                                $name= $this->getName(0);

                                //I have changed this multiple times, preferably all I would pass is 'sec' and 'id', but then the 

                                //controller gives me an error that the other parts required for the list view are not supplied

                                $this->render('list',array('sec'=>0,'id'=>$teacher->id,'name'=>$name,'questions'=>$questions));

                        }


                }


                $this->render('user',array('model'=>$model));

        }


 public function actionList()

        {




                if(isset($_POST['Next']))

                {

                        $results = new Results();

                        $results->user_id = $_POST['id'];

                        $results->section_id = $_POST['sec'];


                        $skills = array(1=>'skill_response1',2=>'skill_response2',3=>'skill_response3',4=>'skill_response4',5=>'skill_response5',6=>'skill_response6',7=>'skill_response7',8=>'skill_response8',9=>'skill_response9');

                        $import = array(1=>'import_response1',2=>'import_response2',3=>'import_response3',4=>'import_response4',5=>'import_response5',6=>'import_response6',7=>'import_response7',8=>'import_response8',9=>'import_response9');


                        for($i = 1; $i < 10; $i++)

                        {

                                $results->question_id = $i;

                                if(isset($_POST[$skills[$i]]))

                                        $results->skill_rating = $_POST[$skills[$i]];

                                else

                                        $results->skill_rating = null;


                                if(isset($_POST[$import[$i]]))

                                        $results->importance_rating = $_POST[$import[$i]];

                                else

                                        $results->importance_rating = null;

                                $results->save();

                        }


                        $this->render('list', array('sec'=>($_POST['sec'] + 1),'id'=>($_POST['id'] + 1)));


                }


                else

                {

                        $section=Section::model()->findByPk($_POST['sec']);


                        $criteria = new CDbCriteria;


                        $criteria->addSearchCondition('section',$section->ID, true, 'AND');


                        $questions=Questions::model()->with('section')->findAll($criteria);


                        $name= $this->getName($_POST['sec']);


                        $this->render('list',array('sec'=>$_POST['sec'],'id'=>$_POST['id'],'questions'=>$questions));

                }


        }



here is the list view code:




<?php

        $this->pageTitle=Yii::app()->name.' - Assessment Questions';

?>


<div id="fullPage">

        <br />

        <?php echo '<h2>Section '.($sec + 1).' - '.$name.'</h2>'; ?>


        <br />


        <?php $form=$this->beginWidget('CActiveForm'); ?>


        <table class = "List">

                <tr>

                        <th>Question</th>

                        <th> <strong>My Skill Level Now</strong></th>

                        <th><strong>Importance for my Teaching Now or in the Future</strong></th>

                </tr>

                <tr>

                        <td></td>

                        <td>Needs Improvement &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp  Excellent</td>

                        <td>Not Important  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Very Important</td>

                </tr>

                <tr>

                        <td></td>

                        <td>1 2 3 4</td>

                        <td>1 2 3 4</td>

                </tr>


        <?php foreach ($questions as $n=>$question): ?>

                <tr>

                        <td class="question">

                                <?php echo $question->order.': '.$question->question; ?>

                        </td>

                        <td>

                        <?php for($i=1; $i<9; $i++) {


                                if($i <5)

                                        echo '<input type="radio" name="skill_response'.$i.$question->order.'>';

                                else

                                        echo '<input type="radio" name="import_response'.$i.$question->order.'>';

                        } ?>

                        </td>

                </tr>

        <?php endforeach; ?>

        <div class="action">

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

        </div>


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


</div>



I think the problem lies on here :


public function actionUser()


...

$this->render('user',array('model'=>$model));

}



This will cause the controller always render "user.php" no matter there are $_POST or not.

maybe you should change the code like this


public function actionUser(){

    $model = $this->loadModel();

    if(isset($_POST['UserForm']))

                {

                  //render list.php

    }else{

                  //render user

                  $this->render('user',array('model'=>$model));

    }

}

I haven’t test though…

after the line


$this->render('list',array('sec'=>0,'id'=>$teacher->id,'name'=>$name,'questions'=>$questions));

add


Yii::app()->end();

junxiong:

You were exactly right. I forgot an else statement, nothing like that to make you feel like a total idiot!

Thanks!

or return after your inner render. I don’t like ultimate “else” cases in my code.

Return early and often!

I have a new problem now. The user page goes to the list page just fine. Tried both the else and the return. Both worked great! My problem now is that the list page is render, thats great, but after the data is entered and the next button is clicked, the user page is then rendered instead of a new version of the list page. Any one have any ideas? I tried the Yii::app()->end(); but that didn’t seem to do the trick either. Thanks in advance!