I have the following code in a controller:
<?php
class Student extends CI_Controller
{
function index()
{
$data = $this->init->set();
$this->parser->parse('include/header', $data);
$this->parser->parse('student/student_index', $data);
$this->parser->parse('include/footer', $data);
}
function planner()
{
$data = $this->init->set();
$this->parser->parse('include/header', $data);
$this->parser->parse('student/student_cal', $data);
$this->parser->parse('include/footer', $data);
}
}
?>
As you can see, there's a lot of repetition here. Basically all of it. I already put my variables in a model so I only have to call the model function each time instead of putting the whole $data array at the start of each function. Anyway, I tried to reduce the repetition here by doing the following:
<?php
class Student extends CI_Controller
{
function index()
{
$data = $this->init->set();
$this->parser->parse('include/header', $data);
switch($this->uri->segment(2))
{
case '': $this->home($data); break;
case 'planner': $this->planner($data); break;
}
$this->parser->parse('include/footer', $data);
}
function home($data)
{
$this->parser->parse('student/student_index', $data);
}
function planner($data)
{
$this->parser->parse('student/student_cal', $data);
}
}
?>
This, somehow, works fine for my homepage. It parses the variables and there's no problem whatsoever. However, on the 'planner' page, I get errors:
Message: Missing argument 1 for Student::planner()
Message: Undefined variable: data
Message: Invalid argument supplied for foreach()
I'm quite sure I get these errors because the function somehow doesn't receive the $data array. I also read in the CI docs that the third segment in a URL gets passed as argument, and in this case the third segment is non-existent, thus nothing gets passed. However, the CI docs didn't tell me how I could pass my $data array from the index() function to my planner() function. I also wonder why the home function works fine, without errors.