How to show results of model on another view

I’m new to Yii framework and am not advanced in PHP but I know what I’m doing as a beginner. I want to renderPartial view on another view and also have the results show after submit. I have two views namely: staff-index for staff (with model: staffinfo) and index for student (with model: studentlaptop). Currerntly, I did renderPartial of the index on top of staff-index and the form on index view appears but when I enter information on the form and submit, I don’t see the results. The page just refreshes. I want a user to fill out the form and show the result on the same view but for now I can’t do that.

Can you help me fix this issue, please?

Here’s my Controller




/* -- Student Laptop DB -- */

    public function actionStudentlaptop()

    {

        $firstnames = false;

        $model = new studentlaptop();


        if (!empty($_POST)) {

            $model->load($_POST);

            if ($model->validate()) {

                $firstnames = $model->findFirstnames();

            }

        }

        return $this->render('index', ['model' => $model, 'firstnames' => $firstnames]);

    }


/* -- Staff Laptop DB -- */

    public function actionStaffinfo()

    {

        $firstname = false;

        $model = new staffinfo();


        if (!empty($_POST)) {

            $model->load($_POST);

            if ($model->validate()) {

                $firstname = $model->findFirstname();

            }

        }

        return $this->render('staff-index', ['model' => $model, 'firstname' => $firstname]);

    }



My views

Staff-index


  

<div id="studentDetails" class="span12">

    

    <?= Yii::$app->controller->renderPartial('index', ['model'=>$model]) ?>


    </div>



Index view




        <div class="msoe_form">

            <?php $form = ActiveForm::begin(['options' => ['class' => 'form-vertical'], 'id' => 'student_form']) ?>

              <label>Staff Look Up:</label>

                <p style=""><?= Html::a("Employees", ['site/staffinfo'], ['class' => 'msoe-submit-button']) ?></p>

              <hr><br>

            <p><strong>Enter the following to check status:</strong></p>

            <?= $form->field($model, 'firstName') ?>

            <?= $form->field($model, 'lastName') ?>

            <?= $form->field($model, 'jenzId') ?>

            <?= Html::submitButton('Look Up', ['class' => 'msoe-submit-button']) ?>

            <?php $form->end() ?>

        </div>

 


<div class="row">

<?php

if (!empty($firstnames)) {

    ?>

    <div class="span12">

        <h3>Student Standings</h3>

         <table width="100%">

              <tr style="background:#ebebeb; border: 1px solid #ccc;">

                <th>ID Number</th>

                <th>Full Names</th>

                <th>Last Name</th>

                <th>Current Degree</th>                

                <th>Student Standing</th>

                <th>Major</th>

                <th>Recent Year Enrolled</th>

              </tr>

               <tr style="border:1px solid #ccc; margin-bottom:20px;">

                <td><?= $firstnames['JenzabarID'] ?> </td>

                <td style="border-left:1px solid #ccc;"><?= $firstnames['First Name'] ?> </td>

                <td style="border-left:1px solid #ccc;"><?= $firstnames['Last Name'] ?> </td>

                <td style="border-left:1px solid #ccc;"><?= $firstnames['Current Degree'] ?> </td>

                <td style="border-left:1px solid #ccc;"><?= $firstnames['Student Standing'] ?></td>

                <td style="border-left:1px solid #ccc;"><?= $firstnames['Major'] ?> </td>

                <td style="border-left:1px solid #ccc;"><?= $firstnames['Recent Year Enrolled'] ?> </td>

              </tr>

              </table>



My models are: studentlaptop for index and staffinfo for staff-index. I want to render index on staff-index view.

So you just want to lookup the student info in actionStaffinfo.

Then instantiate the model and call your method findFirstnames().

(As an alternative you may define a calculated field in the model and just return a model instance)

In actionStaffinfo:


return $this->render('staff-index', ['model' => $model, 'firstname' => $firstname, 'firstnames' => $firstnames]);



(Assuming you need "firstname" somewhere in view staff-index)

In view staff-index


Yii::$app->controller->renderPartial('index', ['model'=>$model], 'firstnames' => $firstnames)



There are more to this e.g the way you instantiate the models, the query object you defined in your previous Yii 1.1 thread (seemingly without performing the query).

I also recommend that you thoroughly read the guide.

Thank you!

However, the edits gives an error of Undefined variable: firstnames.

On View


<?= Yii::$app->controller->renderPartial('index', ['model'=> $model], 'firstnames' => $firstnames) ?>

I do refer to the docs but I get confused with what goes where with the examples given. Meaning, does the example code goes to controller, model or view. I’m still learning PHP and am new to MVC framework too! I just started on Yii last month, so there’s a lot I need to learn here.




$model = new studentlaptop();

$firstnames = $model->findFirstnames();



BTW I was wrong about the instantiation syntax (haven’t used PHP/Yii for 4 years).

Thank you!

Why the form on the partial render (index) inherits the form fields of the staff-index? I think that maybe the problem? It seems to me that the partial render does not call its model with the set rules, attributes and the findFirstnames function.

If mean you want to use information from the staff lookup for selecting student info, that’s a different topic. In this thread I believe you asked how to show info from the student model together with staff info (in the view you presented).

Yes, correct that I want to show data from student with staff info not the firs first sentence. But the partial rendered form did inherit the staff form fields. That’s why I was asking why I can’t see the results of the student model.

Do you mena you want to use the student model in the "staff form fields" in the index view? As a search form for student?

You can pass more than one model as params to the render call(s). Or a model instance as well as the array you return from the findFirstNames() call. Just give those items different names. If you need use two different models (excluded form in staff-index view?) you can handle both in the same controller action.

BTW The names (firstname/firstnames) are a bit misleading.

(I think you should consider adding a computed field to the model instead of building an array.)

I want to use two different models (excluded forms) on staff-index view. I don’t know how to handle those actions in controller. What is computed field and how can I add that?

I think code examples would help so much! I’m also reading the Yii2 By Example by Fabrizio and still trying to figure out what I’m doing.

Thanks!


http://www.yiiframework.com/doc-2.0/guide-input-multiple-models.html


http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#selecting-extra-fields

tri,

From the first you provided the $user also refers to table name, right? That being the case, my sql query is too complicated with multiple CASES and using table views and that makes it hard for me to wrap my head around the example. Like this:




->from('name_master nm')

->leftJoin('student_master sm', 'nm.id_num=sm.id_num')

->leftJoin('degree_history dh', 'nm.id_num=dh.id_num')

->leftJoin('CANDIDACY c', 'nm.id_num=c.id_num')

->leftjoin('STUDENT_DIV_MAST sdm', 'nm.id_num=sdm.id_num');



I’m not trying to solve your problem (which you btw didn’t explain in full).

I posted a couple of links to the guide, not a solution. You have to work out something by yourself or perhaps other forum users will make suggestions.

(I’m currently learning what differs in Yii2 and since I’m not programming/testing a project it will take some time. I might answer questions if I think I can add anything useful.)

Point taken my brother and I really appreciate your input and suggestions! Thank you!