Codeigniter有未定义的索引

时间:2016-05-23 08:05:57

标签: php mysql codeigniter

我需要通过引用另一个表ID来访问表,并且通过这样做,我首先创建了一个foreach循环来退出第一个表数据,然后将其ID引用提供给第二个表。我已经在我的其他函数中完成了它但我不知道在这个函数中提示我它是未定义的索引。这是我的模特:

function retrieve_Sched($id)
{
    $this->db->select('*');
    $this->db->from('schedule');
    $this->db->where('EmpID',$id);
    $query = $this->db->get();

    if($query->num_rows() == 0)
    {
        return false;
    }
    else
    {
        return $query->result_array();
    }
}

这是控制器功能:

foreach($data['result'] as $val)
        {
            $data['schedule'] = $this->DBmodel->retrieve_Sched($val['EmpID']);

            if($data['schedule'] == false)
            {
                $consTimeIn = '05:30';
                $consLunchOut = '10:50';
                $consLunchIn = '13:00';
                $consTimeOut = '17:00';
            }
            else
            {
                $consTimeIn = $data['schedule']['TimeFrom'];
                $consLunchOut = $data['schedule']['LunchOut'];
                $consLunchIn = $data['schedule']['LunchIn'];
                $consTimeOut = $data['schedule']['TimeTo']; 
            }

这是我得到的错误:

ERROR - 2016-05-23 09:57:02 --> Severity: Notice --> Undefined index: TimeFrom C:\xampp\htdocs\TMS\application\controllers\tms.php 482
ERROR - 2016-05-23 09:57:02 --> Severity: Notice --> Undefined index: LunchOut C:\xampp\htdocs\TMS\application\controllers\tms.php 483
ERROR - 2016-05-23 09:57:02 --> Severity: Notice --> Undefined index: LunchIn C:\xampp\htdocs\TMS\application\controllers\tms.php 484
ERROR - 2016-05-23 09:57:02 --> Severity: Notice --> Undefined index: TimeTo C:\xampp\htdocs\TMS\application\controllers\tms.php 485
ERROR - 2016-05-23 09:58:41 --> Severity: Notice --> Undefined index: TimeFrom C:\xampp\htdocs\TMS\application\controllers\tms.php 482
ERROR - 2016-05-23 09:58:41 --> Severity: Notice --> Undefined index: LunchOut C:\xampp\htdocs\TMS\application\controllers\tms.php 483
ERROR - 2016-05-23 09:58:41 --> Severity: Notice --> Undefined index: LunchIn C:\xampp\htdocs\TMS\application\controllers\tms.php 484
ERROR - 2016-05-23 09:58:41 --> Severity: Notice --> Undefined index: TimeTo C:\xampp\htdocs\TMS\application\controllers\tms.php 485

4 个答案:

答案 0 :(得分:1)

从您的控制器

,您使用的函数是返回数组。 [HttpGet] public userDTO User(int id) { return dal.GetUser(id); } public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"); config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType); } }

尝试一下:

$data['schedule'] = $this->DBmodel->retrieve_Sched($val['EmpID']);

答案 1 :(得分:0)

result_array()总是返回数组,所以你必须使用row()或者只使用$ data [' schedule']作为数组

 new LoadProfileImage().execute("http://looksok.files.wordpress.com/2011/12/me.jpg");

答案 2 :(得分:0)

如果您想要单行数据,请使用$ row_array()。

function retrieve_Sched($id)
{
    $this->db->select('*');
    $this->db->from('schedule');
    $this->db->where('EmpID',$id);
    $query = $this->db->get();

    if($query->num_rows() == 0)
    {
        return false;
    }
    else
    {
        return $query->row_array();
    }
}

答案 3 :(得分:0)

这解决了我的问题:

if($data['result'] == false)
    {
        echo 'Sorry files have already been processed';
    }
    else
    {
        foreach($data['result'] as $val)
        {
            $data['check'] = $this->DBmodel->check_Sked($val['EmpID']);

            if($data['check'] == false)
            {
                $consTimeIn = "05:30";
                $consLunchOut = "10:50";
                $consLunchIn = "13:00";
                $consTimeOut = "17:00";
            }
            else
            {
                $data['schedule'] = $this->DBmodel->retrieve_sched($val['EmpID'],$val['ValidDate']);

                if($data['schedule'] == false)
                {
                    $consTimeIn = "05:30";
                    $consLunchOut = "10:50";
                    $consLunchIn = "13:00";
                    $consTimeOut = "17:00";
                }
                else
                {
                    foreach($data['schedule'] as $value)
                    {
                        $conTimeIn = $value['TimeFrom'];
                        $consTimeOut = $value['TimeTo'];

                        if($value['LunchOut'] == NULL || $value['LunchOut'] == "")
                        {
                            $consLunchOut = @date('H:i',@strtotime($consTimeIn,'+4 hour'));
                        }
                        else
                        {
                            $consLunchOut = $value['LunchOut'];
                        }

                        if($value['LunchIn'] == NULL || $value['LunchIn'] == "")
                        {
                            $consLunchIn = @date('H:i',@strtotime($consTimeIn,'+6 hour'));
                        }
                        else
                        {
                            $consLunchIn = $value['LunchIn'];
                        }
                    }
                }
            }