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.
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]);
}
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.
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"??
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.
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??
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.