0

为什么这段代码没问题

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

我的网址是 domain:8888/admin_schedule/teacher/vika

route.php 包含

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

我在函数 __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 函数中的参数,则不起作用!

页面生成的下一个方式:控制器 admin_schedule 具有渲染视图 index.php 的函数 index。在那个视图中,我使用 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);
        }
    }

请帮助理解这个问题。

4

2 回答 2

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!

于 2012-11-28T15:41:06.400 回答
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);
        ...
    }

这就是霹雳舞。

于 2012-11-28T18:42:49.273 回答