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

时间:2014-05-03 06:39:43

标签: php variables cakephp increment

基本上这只是一个猜谜游戏,这样我就可以了解蛋糕的来龙去脉。 我试图跟踪用户每次重新加载的猜测数量(使用变量名称$ 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');
        }
    }
  }

}

?>

1 个答案:

答案 0 :(得分:0)

试试这个:

<?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');
            }
        }
  }
}
?>