为foreach()提供了无效的参数,严重性:警告PHP和无效的json

时间:2018-08-08 09:00:36

标签: php

我花了将近8个小时来尝试解决此警告。我尝试用CodeIgniter和PHP编写一个站点,但是PHP出现了问题。我敢肯定,它是如此简单,而且不会错。但是我不知道来自Bug的漏洞,只是CodeIgniter和PHP的新手。

控制器

public function fetchStudentData($studentId = null) {
    if($studentId) {
        $result = $this->model_student1->fetchStudentData($studentId);
    }
else {
        $studentData = $this->model_student1->fetchStudentData();
        $result = array('data' => array());

        foreach ($studentData as $key => $value) {

            $button = '<!-- Single button -->
                <div class="btn-group">
                  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Action <span class="caret"></span>
                  </button>
                  <ul class="dropdown-menu">
                    <li><a type="button" data-toggle="modal" data-target="#updateStudentModal" onclick="editStudent('.$value['student_id'].')"> <i class="glyphicon glyphicon-edit"></i> Edit</a></li>
                    <li><a type="button" data-toggle="modal" data-target="#removeStudentModal" onclick="removeStudent('.$value['student_id'].')"> <i class="glyphicon glyphicon-trash"></i> Remove</a></li>
                  </ul>
                </div>';
$photo = '  <img src="../'.$value['image'].'" alt="Photo" class="img-circle candidate-photo"/>';

            $result['data'][$key] = array(
                $photo,
                $value['name'] . ' ' . $value['lname'],
                $value['age'],
                $value['contact'],
                $value['email'],
                $button
            );
        } // /foreach       }
    }echo json_encode($result);
}

模型

public function fetchStudentData($studentId = null)
{
    if($studentId) {
        $sql = "SELECT * FROM student WHERE student_id = ?";
        $query = $this->db->query($sql, array($studentId));
        return $query->row_array();
    }
}

1 个答案:

答案 0 :(得分:1)

尝试

if (is_array($studentData) || is_object($studentData))
{
    foreach ($studentData as $value)
    {
        ...
    }
}