On Button click incement

Hello Experts,

Can you guide me how to put conditional increment in my controller??

Controller code:




public function actionIncre()

	{

        $q=1;

        //I want to put conditional increment, Like below condition:

        //for ($i=0; $i <=10 ; $i++) { 

    	//echo $q

        //}

   	return $this->render('incre', ['Number' => $q]);    

    }



View Code:




<div class="row">

                <?php $form = ActiveForm::begin([

                'id' => 'form-Increment',

                'options' => ['class' => 'form-horizontal'],

                ])?>

                <div class="container-fluid" align="center">

               <div class="col-xs-2">

               <?=$Number ?>

               </div>

               </div>

   </div>

    <div class="row">

                <?= Html::submitButton('Test Increment', ['class' => 'btn btn-success']) ?>

                </div>

                <div class="form-group"> 

                    <?php ActiveForm::end(); ?>

                </div>

    </div>

Increment will be continued on each button click untill the condition is true.

Can you guide me on this?

Regards,

Sid

Use javascript for this logic and afterwards send result to controller::action() once.

Hello Umneeq,

Thanks for your reply.

Being a novice, its little difficult for me. If at all possible can you give me some example code snippet because i want to store those incremented value into the database.

Really appreciated your help.

Regards,

Sid

Hi Sid,

  1. Your view code has a closing ‘</div>’ without the corresponding opening ‘<div>’.

  2. You should put ActiveForm::begin() and ActiveForm::end() as the direct children of the same <div> element. They belong to different divs in your view code.

  3. I don’t understand what you mean by “conditional increment”. Do you want to set the maximum limit?

Hello softark,

Thanks for your inputs.

  1. There were some other <div>s which I did not copied. That’s my bad. I shall double check on this part as guided by you.

  2. Ok. I shall put ActiveForm::begin() and ActiveForm::end() with in same <div> element.

  3. Yes, I want to set maximum limit of the no of times of the increment. This increment "maximum limit" will depend on the my input number, e.g. some time I can give 10 as maximum limit or some time i can put 20 as maximum limit etc. Maximum limit would be depended on my input. But in all cases, increment will be happen on button click. Once the maximum limit reaches, it will be redirected to another view file.

I hope, I am able to explain you, what I want to achieve.

Thanks and Regards,

Sid

Hi Sid,

  1. What does "maximum limit" depend on? Another number, or the input number itself? The latter is impossible, because it starts from 1 and will be going up one by one in all cases.

  2. What are those items? What do they belong to? You need some database table(s) to hold those items, don’t you?

Hello Softark,

Sorry for the delayed reply.

Thanks for your reply.

  1. I have a registration form. Database consist of 3 fields namely "id", "name" and "maxlim". On Registration this "maxlim" no is getting forwarded to the redirected page. In the redirected page counting starts from 1 and on button click increment by 1 and goes upto the "maxlim" times. On reaching the "maxlim" the page will redirected to another page.

  2. Yes. I need a database table to hold each incremented values.

I have attached process flow as attachment.

Can you guide me how to do it?

Regards,

Sid

Hi Sid, welcome back!

I’ve seen your process flow and I think I understand what it means, but I still don’t understand what you really want to do. Frankly saying, I would not want to be a user of this process. Why should I continue to click the submit button until the number goes up to the limit before being redirected to another page? I would rather input the number directly.

You might want to say that this process flow is just for testing purpose and is different from the real world application that is in your mind.

Would you please explain the design of your app as a whole? No one would be able to guide you to the goal without a map.

Hello Softark,

Thanks for your reply.

I am developing a site for gambling where user will be given option for 10/20 no of plays in one registration. Also I want to capture the value with the random no the user is getting along with the this incremental no (for which we are discussing now) for my own purpose.

Can I do that?

Its not testing purpose. Its very much real world application I am trying to develop.

I hope I am able to explain you my purpose.

Regards

Sid.

I still don’t get you. :(

Anyway, I think you should consider developing an ActiveRecord model for the game. Probably "Game" model based on "game" table.




table "game"


id ... PK

id_user ... FK to the user table

max_trial_count ... integer

trial_count ... integer

question ... string

answer ... string

right_answer ... string



A user is supposed to buy a new record of "game" by paying some money. In a record of the "game" table, "id_user" will hold the id of the user and "max_trial_count" will hold a random number that the user has got. Initially "trial_count" is set to 0. The user is supposed to answer some question within a limited count of trial.

This would be a skeleton code for the "game" action:




public function actionGame($id)

{

    $model =  Game::findOne($id);


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

        $model->trial_count++;

        $model->save();

        if ($model->answer == $model->right_answer) {

            // the user has won. Congrats!

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

        } 

        if ($model->trial_count > $model->max_trial_count) {

            // the user has lost

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

        }

        // retry

        return $this->refresh();

    }


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

        'model' => $model,

    ]);

}



And a skeleton of the view would be:




...

    <p>Your trial : <?= $model->trial_count ?> / <?= $model->max_trial_count ?></p>

    <p>Question : <?= $model->question; ?></p>

    <?php $form = ActiveForm::begin([...]); ?>

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

        <?= Html::submitButton('Submit', ...)?>

    <?php ActiveForm::end(); ?>

...



Hello Softark,

Thanks for your valuable input. I got the idea. But need some clarification on


$model->trial_count++;



.

In the scenario you suggested, every time we click the Submit button, the form will be refreshed.

Will it be able to hold the ‘trial_count’ value?


 $model->trial_count++;



is not working in my end. Am I missing something??

Thanks in advance.

Regards,

Sid.

Hi Sid,

The value of trial_count is saved right after the increment. The incremented value should be rendered in the next turn of the game if you are loading the same instance.

Would you please show your controller code?

Hello Softark,

Thanks for your valued input.

Please find the below action code:




public function actionGame($id,$max_trial_count){      //id and max_trial_count is coming from the registration form.

  

 $model = new GameForm();

    

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

              if ($increment = $model->incretest())  {

            

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

          'Trialcount'=>$trial_count,

          'Maxtrialno' => $max_trial_count, 

          'Question'=>$question, 

          'Right'=>$right_answer, 

          'Answer' => $answer,

          'model'=>$model,

           ]);

        }

  }


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

          'Trialcount'=>$trial_count,

          'Maxtrialno' => $max_trial_no, 

          'Question'=>$question, 

          'Right'=>$right_answer, 

          'Answer' => $answer,

          'model'=>$model,

        ]);

    }







Model: Gameform.


incretest()


......

 public function incretest()

    {

        if ($this->validate()) 

        {

        $increment = new Inc(); //Inc model is in common\models\Inc 

        $increment->trial_count=$this->trial_count;

        $increment->max_trial_count=$this->max_trial_count;

        $increment->question=$this->question;

        $increment->right_answer = $this->right_answer;

        $increment->answer = $this->answer;

             if($increment->save()){

           return null;

            }

        }

      return null;

        

    }

}     

In this, where should I place "$model->trial_count++;"??

Regards

Sid

Hi Sid,

First of all, you have to learn the very basics of programming before you try to develop a web application. The code above will never work, because you are referencing undefined variables like “$trial_count” and “$max_trial_count”. It’s a kind of mistake that no one would make except for sheer beginners in programming.

Please consider finding someone who will teach you the basics of programming over your shoulders. Attending some programming class would be a good idea.