I have Trouble in PHP and codeIgniter in web application. Error this application on Invalid argument supplied for foreach(), error_handler and require_once A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: controllers/Student1.php
Line Number: 117
Backtrace:
File: C:\wamp\www\sms\application\controllers\Student1.php
Line: 117
Function: _error_handler
File: C:\wamp\www\sms\index.php
Line: 292
Function: require_once
My code is below:
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);
}
chorn
August 7, 2018, 1:54pm
2
yasheshinkcadre:
My code is below:
And which is the line the error occures? And what does the variable you give to foreach
contains?
Error occurs in foreach and line number 117.
foreach ($studentData as $key => $value)
Also post my js file function:
$.ajax
url: 'student1/fetchStudentData/'+ studentId,
type: 'get',
dataType: 'json',
success:function(response){
$("#editName").val(response.name);
$("#editFname").val(response.fname);
$("#editSname").val(response.sname);
$("#editMname").val(response.mname);
$("#editDob").val(response.date_of_birth);
$("#editAge").val(response.age);
$("#editGender").val(response.gender);
$("#editCategory").val(response.category);
$("#editSubcategory").val(response.subcategory);
$("#editContact").val(response.contact);
$("#editContact1").val(response.contact1);
$("#editEmail").val(response.email);
$("#editAddress").val(response.address);
$("#editCity").val(response.city);
$("#editCountry").val(response.country);
$("#editRegisterDate").val(response.register_date);
$("#editIncome").val(response.income);
$("#editAadhar").val(response.aadhar);
$("#editPan").val(response.pan);
$("#student_photo").attr('src', '../' + response.image);
JSON.parse(response);
// submit the student information form
$("#updateStudentInfoForm").unbind('submit').bind('submit', function() {
var form = $(this);
var url = form.attr('action');
var type = form.attr('method');
$.ajax({
url: url + '/' + studentId,
type: type,
data: form.serialize(),
dataType: 'json',
success:function(response) {
if(response.success == true) {
$("#edit-personal-student-message").html('<div class="alert alert-success alert-dismissible" role="alert">'+
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+
response.messages +
'</div>');
manageStudentTable.ajax.reload(null, false);
$('.form-group').removeClass('has-error').removeClass('has-success');
$('.text-danger').remove();
}
else {
if(response.messages instanceof Object) {
$.each(response.messages, function(index, value) {
var key = $("#" + index);
key.closest('.form-group')
.removeClass('has-error')
.removeClass('has-success')
.addClass(value.length > 0 ? 'has-error' : 'has-success')
.find('.text-danger').remove();
key.after(value);
});
}
else {
$("#edit-personal-student-message").html('<div class="alert alert-warning alert-dismissible" role="alert">'+
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+
response.messages +
'</div>');
}
} // /else
} // /success
}); // /ajax
return false;
});
Model_student1:
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();
}
}
chorn
August 8, 2018, 6:40am
4
You didn’t answer the question:
check with var_dump()
.
Hello,
Thanks for reply. check with var_dump() but value is null showing. I am new in PHP and codeIgniter that why I am not proper understand this warning. And thanks for replying.
chorn
August 8, 2018, 9:36am
6
Now you have to spcify what the intended behavior is. Can this variable be null, by logic? If yes, check the variables type before running foreach. If no, throw an exception. Then your problem lies somewhere else, you have to look up all parts of your code where the function is used and get to know why it is null when it should not.
1 Like
zilfos
August 8, 2018, 3:44pm
7
foreach returns that error when the data passes is not an array or object
Make sure this line $studentData = $this->model_student1->fetchStudentData();
returns an array or an object. Probably the problem is in your model
1 Like
I add wrong value in model part that’s why foreach problem…
Thanks for you help.
system
Closed
November 22, 2018, 6:56pm
9
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.