How to view data before save

Hello Experts,

I am working in a project to develop a gambling application.

In this respect, I am trying to implement the random number from random function. There will be three numbers which will be generated from random function and one Next/refresh button to generate the next number.

I want to save these generated numbers in a db table.

When this page will be render for the first time it will start with displaying 3 numbers and the refresh button. Once the refresh button is pressed,the displayed numbers will be saved and a new set of number will be shown.

Can any one guide me how proceed in this situation.

I have googled a lot for the solution, but in every cases, I found data is already in the db and fetching the data from the db to view. But in my case, it is reverse actually. First view the data and then save the data.

Any idea.

Thanks in advance.

Regards,

Sid

should be pretty straight forward to achieve this there are many ways to do this lets start with the easiest one by using session, lets say you have an action like so


public function actionFoo()

{

    // check if session has your value $n

    // if so save it to the database


    // generate a new number

    $n = rand();

    // save your $n in a session

    // to later save it in the database


    $this->render('path/to/view', ['n' => $n]);

}

Hello Alrazi,

Thank for reply.

In the case shown by you,

I can echo out the value $n in the view page with the below code.




public function actionIndex(){

    $n = rand();

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

      }

                    



Now if I want to save this value to the db table, how to do that??

Thanks in advance.

Regards,

Sid

Consider the following steps to do what you want. This will be the fastest and the easiest way.

[list=1]

[*]Create a database table that can hold all the information that you want to save to db.

[*]Generate an Active Record model based on the table using Gii.

[*]Generate the CRUD for the generated model.

[*]Modify the "create" and/or "update" page to suit your needs.

[/list]

Examine carefully what Yii has generated for “create” and “update”, and you’ll know how to manipulate data in Yii.

BTW, I want to say it again. "Read the following sections of the guide."

Getting Started

  • Running Applications

  • Saying Hello

  • Working with Forms

  • Working with Databases

Especially the last 2 sections.

Hello Softark,

Thanks for your guidelines.

I have gone through the guides and also created the CRUD as guided by you.

But in all cases I found that user given the input through the form and submit the button to create the db record or fetch the data from the DB to update and view.

But for my case, the data is generated from the function automatically which will be display first then on submit, it will save the record to the data base. That is where I got stuck.

I did not find any documentation on this type of issue as of now.

If you can please help me to understand the logical structure to resolve this issue, it will help me a lot.

Thanks to your patience to guide me.

Regards,

Sid

I see. It’s fairly easy.

For example, this is a typical controller code for actionCreate generated by Gii:




    public function actionCreate()

    {

        $model = new YourModel();


        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id]);

        }


        return $this->render('create', [

            'model' => $model,

        ]);

    }



You can specify the value of the number before it is rendered in the form of the view.




    public function actionCreate()

    {

        $model = new YourModel();

        $model->number = rand(100, 199);  // just add this line


        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id]);

        }


        return $this->render('create', [

            'model' => $model,

        ]);

    }



And replace the text input for "number" field with a read only text in the view form that Gii has created for you.

That’s all. :)

Hello Softark,

Thanks for your help.

Its actually easy for you guys. But being a novice in yii framework, its little difficult for me. Learning day by day.

I got the point, where I got stuck.

One thing I just need to clarify in your example code. Can I refresh the page after saving the data (in example "create") keeping all the input parameters (in your example "number"), instead of redirect the page to another page (in your example "view"??

Regards,

Sid

I don’t think it’s a good UI to stay in the “create” page after the data has been saved. And it will be very tricky in terms of coding, too.

You’d better redirect to “view” or “update” page once the data has been saved.

Oh, I was wrong.

"read only text" (label) is not good. It must be a text field that is read only, or a hidden input.

Thanks for your guidance.

I already applied hidden input for this.

Regards,

Sid

Hello Softark,

Thanks for the input.

But if you look into my first post, you will find that this page refresh will be required I guess.

If refresh is required on that scenario, how shall I approach.

Can you give me the approach idea.

Regards,

Sid

I see. You want your user to create the second data after the first one has been saved, don’t you? Then redirecting to “create” (or refresh) might be OK.

But, on second thought, I would redirect to another page that tells the user about the successful creation of the data. Otherwise he/she might feel uneasy.

Another thing you may consider is the flash message.

https://stackoverflow.com/questions/32793569/how-to-set-a-flash-message-in-yii2

Hello Softark,

Thanks for your valuable inputs.

Correct. I want my user to create the second data after save the first one. Since it is the number gamble game (on refresh/submit button click, it will continue to generate the numbers, lets say for 5 min. Then it will redirect to another page with some message.

The example in the link, I understood the methodology.

Example code given as below







Below is the controller class for adding products


class ProductsController extends \yii\web\Controller

{

    public function actionCreate()

    {

        $ProductsModel = new Products();


        if ($ProductsModel->load(Yii::$app->request->post()) && $ProductsModel->save()) {

            Yii::$app->session->setFlash('success', "Product Added Successfully");

            return $this->redirect(['create']);

        }

        else{ 

            return $this->render('create', [

                'ProductsModel' => $ProductsModel

            ]);

        }

    }

}



.

Now instead of Yii::$app->session->setFlash(‘success’, “Product Added Successfully”); , can I put some code that will refresh the model/page??

Any idea??

Regards,

Sid

Sorry, but I don’t understand what you mean.

Hello softark,

The methodology, you have mentioned in earlier post for , I found that the 1st data is getting saved, but after that numbers are generating but saving the 0 value in the data base.

I am trying to find out the issue, I shall definitely update the issue details, what exactly happening with this structure.May be by next week.

For your convenience, I shall post you this thread link, when I shall be able to upload the issue with the code.

Thanks to your support till now.

Regards,

Sid