我有以下foreach循环,它给了我无效的参数错误:
$all_students = $this -> model_accounts -> get_students_on_account($account['account_id']);
foreach ($all_students as $student)
{
...
}
我尝试使用 print_r 进行调试以检查 $all_students 数组的外观,结果得到了这个:
Array
(
[0] => Array
(
[id] => 00062
[student_name] => Reckless
[student_surname] => Harmse
[acc_id] => 198
[dob] => 07-07-1993
[gender] => Male
[active] => 1
[has_pic] => 1
[avatar] => 106.jpg
[pic_ver] => 1
[student_identity_num] => 9307075052084
)
)
所以数组就在那里,如print_r细节所示,它被视为一个数组。
我看过其他类似的问题,但没有一个能帮助。
提前感谢
添加:用于获取学生的模型函数
function get_students_on_account($account_id)
{
$data = '';
$this->db->where('acc_id', $account_id);
$q = $this->db->get('students');
if ($q->num_rows() > 0)
{
foreach ($q->result_array() as $row)
{
$row['dob'] = $this -> change_date_format($row['dob']);
$data[] = $row;
}
}
return $data;
}
在函数get_students_on_account中,替换以下内容:
$data = '';
通过这个:
$data = array();
这是一致返回数组所必需的。您不想返回字符串。当它发生时,将出现您描述的问题。
试试这个
$res = $q->result_array();
foreach ($res as &$row)
{
$row['dob'] = $this -> change_date_format($row['dob']);
}
return $res;
试试这个
在控制器中
$all_students = $this->model_accounts->get_students_on_account($account['account_id']);
if($all_students == false){
echo "Empty result";
}
else{
foreach ($all_students[0] as $student)
{
...
}
}
在模型中
function get_students_on_account($account_id)
{
$query = $this->db->query("SELECT * FROM students WHERE acc_id = $account_id");
$result = $query->result_array();
$count = count($result);
if (empty($count)) {
return false;
}
else{
return $result;
}
}