【问题标题】:issue with incrementing a variable in cakephp (basics)在 cakephp 中增加变量的问题(基础)
【发布时间】:2014-05-03 06:39:42
【问题描述】:

基本上这只是一个猜谜游戏,这样我就可以了解蛋糕的来龙去脉。 我正在尝试跟踪用户在每次重新加载页面时的猜测次数(使用变量名 $user_loop)。当我尝试增加它时,它保持静态或仅增加一并停止。我已将增量 sn-p 放在我的视图中,也只能收到相同的结果。任何帮助都会很棒。

<?php

class GuessController extends AppController {

public $uses = array(); 

function beforeFilter() {

    if (!$this->Session->check('Random.Num')) {
        $this->Session->write('Random.Num', rand(1, 9));
    }
}

function create() {

    if ($this->request->is('post', 'put')) {

        $rn = $this->Session->read('Random.Num');
        $user_guess = $this->request->data['Guess']['guess'];
        $this->set('user_guess', $user_guess);

        $user_loop++      // <- Also getting undefined variable here 
        echo $user_loop; //for testing

        if ($rn == $user_guess) {   
            echo 'Cashe Cleared';
            $this->Session->delete('Random');
        }
    }
  }

}

?>

【问题讨论】:

    标签: php variables cakephp increment


    【解决方案1】:

    试试这个:

    <?php
    class GuessController extends AppController {
        public $uses = array(); 
    
        function beforeFilter() {
            if (!$this->Session->check('Random.Num')) {
                $this->Session->write('Random.Num', rand(1, 9));
            }
    
            if(!$this->Session->check('user_loop')) {
                $this->Session->write('user_loop',0);
            }
        }
    
        function create() {
            if ($this->request->is('post', 'put')) {
    
                $rn = $this->Session->read('Random.Num');
                $user_guess = $this->request->data['Guess']['guess'];
                $this->set('user_guess', $user_guess);
    
                $user_loop = $this->Session->read('user_loop')+1;
                echo $user_loop; //for testing
                $this->Session->write('user_loop',$user_loop);
    
                if ($rn == $user_guess) {   
                    echo 'Cash Cleared';
                    $this->Session->delete('Random');
                }
            }
      }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2013-08-06
      • 2015-04-07
      • 2016-01-12
      • 1970-01-01
      相关资源
      最近更新 更多