在cakephp中增加变量的问题(基础)


issue with incrementing a variable in cakephp (basics)

基本上这只是一个猜谜游戏,这样我就可以了解蛋糕的来龙去路了。我试图跟踪每次页面重新加载时用户猜测的次数(使用变量名$user_loop)。当我尝试增加它时,它保持静态或只增加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));
    }
}
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
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');
            }
        }
  }
}
?>