为什么这段代码没问题
$this->sch_teacher_id = $this->ion_auth->user_by_username("vika")->row()->id;
但这不起作用?
$this->sch_teacher_id = $this->ion_auth->user_by_username($this->uri->segment(3))->row()->id;
我的网址是域名:8888 / admin_schedule / teacher / vika
route.php包含
$route['admin_schedule/teacher/(:any)'] = "admin_schedule/index/$1";
我在 function __construct()中使用代码行,并且它的结果在另一个控制器函数中使用,因为3个函数使用此结果。如果我在其中一个函数中移动此代码并使用 $ this-> uri-> segment(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函数中使用它作为参数,则不起作用!
页面生成的下一个方式: controller admin_schedule具有渲染视图index.php的函数索引。 在那个视图中我使用javascript,从admin_schedule中调用另一个函数controller = 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 :(得分:0)
我不记得原因 - 这只是你学会接受的CI怪癖 - 你不能直接使用$ this-&gt; uri-&gt; segment(3)等作为参数,你需要分配它然后传递给@almix建议进行他的理智测试。
至少我也一直很难直接使用它作为参数 - 所以我很乐意让任何人纠正我!
答案 1 :(得分: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'),而不是我控制器的其他功能。但我现在可以通过jQuery $ .ajax选项数据从视图中将此值(sch_teacher_id)传递给此控制器:
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',
}
],
然后(在山的另一边)抓住这个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);
...
}
这就是霹雳舞。