【问题标题】:How to display session variables in cakePHP如何在 cakePHP 中显示会话变量
【发布时间】:2011-03-09 12:37:14
【问题描述】:

我在下面有一个控制器类,它将一个学生添加到会话中。

类 StudentController 扩展 AppController { var $name="学生";

    function addstudent()
    {
        //$id=$_REQUEST['id'];
        //$this->Session->write('id', $id);
        static  $count=0;
        if (!empty($this->data)) {

        $students = $this->Session->read('Student');
        if (!$students) {
            $students = array();


        }
        $students[] = $this->data['Student'];/* data */
        $this->Session->write('Student', $students);
        $this->Session->write('student_count',$count);
            $this->redirect(array('controller'=>'students','action' => 'addstudent'));
        }       

    }
}

我的问题是如何在视图页面中显示所有添加的学生。请用语法解释我

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    将会话助手添加到您的视图中。访问 student_count 变量的代码是

    $session->read('student_count');
    

    一般语法是

    $session->read('var_name');
    

    【讨论】:

    • 另外,您应该仍然可以使用 $_SESSION 超全局变量访问变量。 CakePHP 仍然是 PHP!但是,是的,这个答案是在遵守 CakePHP 约定的同时做到这一点的正确方法。
    【解决方案2】:
    $student_list = $this->Session->write('Student', $students);
    $student_count = $this->Session->write('student_count',$count);
    
    $this->set('$student_list',student_list);
    $this->set('$student_count',student_count);
    

    在查看页面中使用 student_list 和 student_count。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 2015-05-27
      • 1970-01-01
      • 2012-05-03
      • 2012-04-10
      • 1970-01-01
      相关资源
      最近更新 更多