0
votes

I am trying to get the CodeIgniter calender library to work, and for that I need to use $this->uri->segment(), according to the documentation at least. The url for the calender looks like this:

http://www.example.com/index.php/tasks/calendar/2015/03

Which means, as far as I can understand, that $this->uri->segment(3) would be the year, and $this->uri->segment(4) the month. However, both of these returns nothing at all, here is the code for my calender page:

echo "Selected year: ".$this->uri->segment(3)." and month: ".$this->uri->segment(4);
echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4));

The first line is merely used for debugging purposes, and confirms that none of the commands returns any output. The calender library is loaded in the controller, with the following code:

public function calendar() {
    // Calender configuration (must be done prior to loading the library
    $conf = array(
            'start_day' => 'monday',
            'show_next_prev' => true,
            'next_prev_url' => base_url() . 'index.php/tasks/calendar'
    );

    // Load libraries and helpers
    $this->load->library('calendar',$conf);

    // Show page, including header and footer
    $this->load->view('templates/header', $data);
    $this->load->view('tasks/calendar', $data);
    $this->load->view('templates/footer');
}

Can anyone tell me where I am failing? I read the documentation over and over again, and checked my code, but I can't see what I do different than their example?

UPDATE

I think I have ruled out that the problem is related to the $this->uri->section() function. Is likely to do with the controller loading an incorrect page, when additional sections is added to the URL after .../tasks/calendar/(...). I still haven't found a solution, but I wanted to let you know, in case others who read this had the same issue.

UPDATE 2

I think it might be a routing issue, the routes.php looks like this, for anything related to tasks:

$route['tasks/create']                  = 'tasks/create';
$route['tasks/calendar/(:num)/(:num)']  = 'tasks/calendar/$1/$2';
$route['tasks/calendar']                = 'tasks/calendar';
$route['tasks']                         = 'tasks';
$route['tasks/(:any)']                  = 'tasks/view/$1';
3
I decided to make a new issue, seeing as it isn't exactly the $this->uri->segment() which is the issue... If anyone is interested, it can be found here - Jakob Busk Sørensen

3 Answers

3
votes

for this to work you have to load the Codeigniter url helper since youre using base_url(). You can do this by running this $this->load->helper('url'); or by going to your autoload.php and placing it there in helper

1
votes

Ok the problem here is not your URI class. The problem is that youre trying to use $this->uri->segment() inside your view. This is not how you should do this. You should only use that inside your controller. To fix this, you have two options, you can either pass your year and month as parameters to your controller method or you can use the uri->segment inside your controller.

  • Example 1: Passing year and month as parameters

    URL: index.php/tasks/calendar/2015/03

    Controller:

    class Tasks extends MY_Controller {
        public function calendar($year, $month){
            $conf = array(
                'start_day' => 'monday',
                'show_next_prev' => true,
                'next_prev_url' => base_url() . 'index.php/tasks/calendar'
            );
            $this->load->library('calendar',$conf);
            $data = array(
                "year" => $year,
                "month" => $month
            );
            $this->load->view('tasks/calendar', $data);
        }
    }
    

    View:

    echo "Selected year: $year and month: $month";
    echo $this->calendar->generate($year, $month);
    
  • Example 2: getting the variable using $this->uri

    URL: index.php/tasks/calendar/2015/03

    Controller:

    class Tasks extends MY_Controller {
        public function calendar(){
            $conf = array(
                'start_day' => 'monday',
                'show_next_prev' => true,
                'next_prev_url' => base_url() . 'index.php/tasks/calendar'
            );
            $this->load->library('calendar',$conf);
            $data = array(
                "year" => $this->uri->segment(3),
                "month" => $this->uri->segment(4)
            );
            $this->load->view('tasks/calendar', $data);
        }
    }
    

    View:

    echo "Selected year: $year and month: $month";
    echo $this->calendar->generate($year, $month);
    
0
votes

What do you get if you try echo

"Selected year: ".$this->uri->segment(1)." and month: ".$this->uri->segment(2);

I'm pretty sure the index is relative based on the default_controller path setting in the routes.php config.