0

Why this code is ok

$this->sch_teacher_id = $this->ion_auth->user_by_username("vika")->row()->id;

But this doesn't work?

$this->sch_teacher_id = $this->ion_auth->user_by_username($this->uri->segment(3))->row()->id;

My url is domain:8888/admin_schedule/teacher/vika

route.php contains

$route['admin_schedule/teacher/(:any)'] = "admin_schedule/index/$1";

I use code lines in function __construct(), and the result of it use in another controller function, because 3 functions use this result. If I move this code in one of this functions and use $this->uri->segment(3) then I get not 'vika's lessons, but my own lessons, so

public function user_by_username($username = FALSE)
    {
        $this->trigger_events('user');

        //if no id was passed use the current users id
        $username || $username = $this->session->userdata('username');

        $this->limit(1);
        $this->where($this->tables['users'].'.username', $username);

        $this->users();

        return $this;
    } 

works good. But $this->uri->segment(3) if use it as parameter in user_by_username function, doesn't work!

page generated next way: controller admin_schedule have function index which render view index.php. And in that view I use javascript, that call another function from admin_schedule controller = get_schedule_db_recurring_events_on_daysweek such way:

...
{
                url: '/admin_schedule/get_schedule_db_recurring_events_on_daysweek/',//"<?echo $data_path?>",
                backgroundColor: 'red',
            }

and in controller

function get_schedule_db_recurring_events_on_daysweek()
    {   
        $start = date('Y-m-d H:i', $this->input->get('start')); 
        $end = date('Y-m-d H:i', $this->input->get('end')); 
        $sch_teacher_id = $this->uri->segment(3); // <--- this doesn't work, but $sch_teacher_id = 111 works perfectly
        $result=$this->Schedule_model->get_schedule_recurring_events_on_daysweek($start, $end, $sch_teacher_id);
        $count=count($result);      
        if($count>0){
                echo json_encode($result);
        }
    }

Please, help understand this problem.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
almix
  • 289
  • 1
  • 9
  • 23
  • Have you tried to assign this to a variable and echo it to see what is going on? – Ricardo Souza Nov 28 '12 at 14:18
  • yes, of course: $username1 = "'".$this->uri->segment(3)."'"; print_r($username1); This print 'vika' as it should. – almix Nov 28 '12 at 14:27
  • If so, your last question is already answered. This left us with the main problem. Have you tried to pass this value with a variable instead of directly to the method (seems weird but it causes some trouble with references in some cases). Just a try. – Ricardo Souza Nov 28 '12 at 14:35
  • Passing value 'vika' in variable $username1 = "vika"; gives good result, yes. Problem happened if I use $this->uri->segment(3). I've tried rsegment instead segment and it doesn't help. – almix Nov 28 '12 at 14:49
  • If you solved your problem, post the solution as an answer, so it can help others. – Ricardo Souza Nov 28 '12 at 15:03

2 Answers2

0

I can't remember the reason - it's just CI quirk you have learn to accept - you can't use $this->uri->segment(3) etc as an argument directly, you will need to assign it and then pass that as @almix suggested for his sanity test.

At-least I have also always had trouble using it directly as an argument - tho I will be happy for anyone to correct me!

Brian
  • 8,418
  • 2
  • 25
  • 32
  • No, Brian, assigning it to argument doesn't work too. $sch_teacher_id = 111;//$this->uri->segment(3); $result=$this->Schedule_model->get_schedule_recurring_events_on_daysweek($start, $end, $sch_teacher_id); works, but with $sch_teacher_id = $this->uri->segment(3); doesn't! – almix Nov 28 '12 at 15:47
  • you mean: $username1 = $this->uri->segment(3); then using $username1 does'nt work? – Brian Nov 28 '12 at 15:48
  • Hmmm ok http://ellislab.com/codeigniter/user-guide/libraries/uri.html states that it will return 'false' if the item is not found. – Brian Nov 28 '12 at 15:51
  • I add explaination about route in question. – almix Nov 28 '12 at 16:17
  • may be somehow use this advise http://stackoverflow.com/questions/2062086/how-to-get-controller-action-url-informations-with-codeigniter#2071176 – almix Nov 28 '12 at 16:27
0

I solved my problem!

Think that something goes wrong because of ajax calls. So when I code in controller

function __construct()
 {
        parent::__construct();
        ...

        $this->sch_teacher_row = $this->ion_auth->user_by_username($this->uri->segment(3))->row();
        $this->data['sch_teacher_id'] = $this->sch_teacher_row->id;
        $this->data['subtitle'] = $this->sch_teacher_row->username;          
 } 

Next I have the right value ('vika'), or to be more precise vika's id ('911') in view file but not in other function of my controller. But I can pass this value now (sch_teacher_id) from the view to this controller with jQuery $.ajax option data:

eventSources: [
...         
{
                url: '/admin_schedule/get_schedule_db_recurring_events_on_daysweek/',//"<?echo $data_path?>",
                type: 'GET',
                data: {sch_teacher_id: sch_teacher_id},
                backgroundColor: 'red',
            }  
        ],

And next (on the other side of the mountain) catch this GET parameter and kick it to the model function:

function get_schedule_db_recurring_events_on_daysweek()
    {   
        ...
        $sch_teacher_id_from_view = $this->input->get('sch_teacher_id');
        $result=$this->Schedule_model->get_schedule_recurring_events_on_daysweek($start, $end, $sch_teacher_id_from_view);
        ...
    }

And it is all the breakdance.

almix
  • 289
  • 1
  • 9
  • 23