0

使用这个 - > URI->段(3)价值?笨不能在功能

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

我的网址域名:8888/admin_schedule /教师/ VIKA

route.php包含

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

我使用的代码行功能__construct(),和结果它在另一个控制器功能中使用,因为3个功能使用此结果。如果我在这个功能中的一个移动这个代码,并使用$这个 - > URI->段(3)然后我得到不是“VIKA的教训,但我自己的经验教训,所以

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

做工不错。但是$ this-> uri-> segment(3)如果在user_by_username函数中使用它作为参数,则不起作用!

下一页生成的页面: 控制器admin_schedule具有渲染视图index.php的函数索引。 并在该视图中使用JavaScript,调用另一个函数从admin_schedule控制器= get_schedule_db_recurring_events_on_daysweek这样的方式:

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

和控制器

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

请,有助于理解这个问题。

+0

您是否尝试将其分配给一个变量并回显以查看发生了什么? – rcdmk

+0

是的,当然是:$ username1 =“'”。$ this-> uri-> segment(3)。“'”; \t \t print_r($ username1);这张照片应该是'vika'。 – almix

+0

如果是这样,您的最后一个问题已经得到解答。这给我们留下了主要问题。你有没有试图将这个值与一个变量而不是直接传递给该方法(看起来很奇怪,但在某些情况下会引起一些引用问题)。试试吧。 – rcdmk

回答

0

我不记得原因 - 它只是你学会接受的CI怪癖 - 你不能直接使用$ this-> uri-> segment(3)等作为参数,你需要指定它然后通过@almix为他的完整性测试进行传递。

至少我也一直有麻烦直接用它作为参数 - 我会很高兴任何人纠正我!

+0

不,布赖恩,将它分配给参数不起作用。 $ sch_teacher_id = 111; // $ this-> uri-> segment(3); \t \t $ result = $ this-> Schedule_model-> get_schedule_recurring_events_on_daysweek($ start,$ end,$ sch_teacher_id);作品,但$ sch_teacher_id = $ this-> uri-> segment(3);不! – almix

+0

你的意思是:$ username1 = $ this-> uri-> segment(3);那么使用$ username1 does not工作? – Brian

+0

嗯,确定http://ellislab.com/codeigniter/user-guide/libraries/uri.html指出,如果找不到该项目,它将返回'false'。 – Brian

0

我解决了我的问题!

认为由于ajax调用而出现问题。所以,当我在控制器代码

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

接下来我有正确的价值(“VIKA”),或者更精确地说VIKA的ID(“911”)在视图文件但不是在我的控制器的其他功能。但我可以从现在视图(sch_teacher_id)将该值传递给该控制器使用jQuery $就选择数据

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', 
      } 
     ], 

和next(在山的另一边)抓住这个GET参数,而且踢它的模型功能:

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

而这一切都是霹雳舞。

+0

这与我们的建议相同,然后分配它交上来。 – Brian