n92
n92

Reputation: 7592

How to display session variables in cakePHP

I have a controller class below which adds a student in to session.

class StudentsController extends AppController { var $name="Student";

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

    }
}

my question is how to display all the added students in the view page.please explain me with syntax

Upvotes: 0

Views: 9798

Answers (2)

srinivas
srinivas

Reputation: 472

$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);

use the student_list and student_count in view page .

Upvotes: 0

Belinda
Belinda

Reputation: 1231

Add the Session helper to your view. The code to access the student_count variable would be

$session->read('student_count');

The general syntax is

$session->read('var_name');

Upvotes: 3

Related Questions