Php my helper函数引发消息:未定义的偏移量:0

Php my helper函数引发消息:未定义的偏移量:0,php,codeigniter,Php,Codeigniter,这里我有一个数组,在这个数组中studentAbsentId我还有一个数组,现在我想找到studentAbsentId 1表示他的名字,假设studentAbsentId 2表示他的名字,因此我编写了一个函数helper.php,但它抛出了错误,如何修复这个问题 我的控制器 打印(缺席) helper.php/位于helper文件夹下 从这里开始,如果我将打印studentAbsentId,则表示它正在工作,但我需要该id的名字,如何解决此问题?更改此项(在getStudentnameById函

这里我有一个数组,在这个数组中studentAbsentId我还有一个数组,现在我想找到studentAbsentId 1表示他的名字,假设studentAbsentId 2表示他的名字,因此我编写了一个函数helper.php,但它抛出了错误,如何修复这个问题

我的控制器

打印(缺席)

helper.php/位于helper文件夹下

从这里开始,如果我将打印studentAbsentId,则表示它正在工作,但我需要该id的名字,如何解决此问题?

更改此项(在
getStudentnameById
函数中)

如下

$first_name = isset($q[0]->firstName) ? $q[0]->firstName : '';
return $first_name;
由于数组中有3个元素,因此循环将继续3次,但在
new_student
中,每个
$id
的表数据可能不可用,因此它将出错。一旦您通过
isset()
进行检查,它将仅在找到数据时返回数据,否则将返回空字符串

但是,如果找不到类似的数据,您可以返回任何演示数据

$first_name = isset($q[0]->firstName) ? $q[0]->firstName : 'no name';
另一种方式

试试这个

if ( ! function_exists('getStudentnameById')){
   function getStudentnameById($id){
       $ci =& get_instance();
       $ci->load->database();

       $ci->db->select('firstName');
       $ci->db->where('student_id', $id);
       $q = $ci->db->get('new_student')->result();
       if(!empty($q)){
         $first_name = $q[0]->firstName;
       }else{
         $first_name = 'no data';
       }
       return $first_name;
   }
}

print\u r($q)
并发布数据print\u r($q),返回空数组,但我所写的内容仅是正确的
echo$id
并查看其上的数据是的,数据正在循环中,如1,2,3…在循环中??后示例
return $q[0]->firstName;
$first_name = isset($q[0]->firstName) ? $q[0]->firstName : '';
return $first_name;
$first_name = isset($q[0]->firstName) ? $q[0]->firstName : 'no name';
if ( ! function_exists('getStudentnameById')){
   function getStudentnameById($id){
       $ci =& get_instance();
       $ci->load->database();

       $ci->db->select('firstName');
       $ci->db->where('student_id', $id);
       $q = $ci->db->get('new_student')->result();
       if(!empty($q)){
         $first_name = $q[0]->firstName;
       }else{
         $first_name = 'no data';
       }
       return $first_name;
   }
}