Hi I am using foreach loop to parse each row of json data..
What exactly I am trying is to get json data from the view and then sending it to the controller where I am parsing each row of json to insert it into my database.. I am trying to insert the data by putting it in a loop and calling the setCurrentAttendance($item) which is present in my model. If my approach is wrong please let me know the correct one..
The php code is :
$data = json_decode($_POST["json"]);
var_dump($data);
foreach($data as $item){
$this->codegen_model->setCurrentAttendance($item);
}
NOTE
$this->codegen_model->setCurrentAttendance($item);
redirects to my model where I am trying to pass the $item as an array and inserting it into the database..
function setCurrentAttendance($data){
$this->db->insert('table_name', $data);
if ($this->db->affected_rows() >= '1')
{
return TRUE;
}
return FALSE;
}
The json data variable $data is :
"[{"roll_no":"1101","full_name":"John Smith","dayspresent":"1","totalclasses":"2","percent_att":"50","hasAttended":"P","att_date":"Thu Apr 04 2013","st_class":"1","st_section":"A"},
{"roll_no":"1102","full_name":"Ram Puri","dayspresent":"4","totalclasses":"4","percent_att":"100","hasAttended":"P","att_date":"Thu Apr 04 2013 ","st_class":"1","st_section":"A"}]"
But I am getting the following error and not able to guess WHY ??
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: Invalid argument supplied for foreach()</p>
<p>Filename: controllers/controller_name.php</p>
<p>Line Number: 245</p>
</div>
Please let me know where I am wrong.
Thanks in advance.
UPDATE
json encoded data:
"[{\"roll_no\":\"1101\",\"full_name\":\"John Smith\",\"dayspresent\":\"1\",\"totalclasses\":\"2\",\"percent_att\":\"\n\t\t\t50\t\t\t\",\"hasAttended\":\"P\",\"att_date\":\"Fri Apr 05 2013 \",\"st_class\":\"1\",\"st_section\":\"A\"},{\"roll_no\":\"1102\",\"full_name\":\"Ram Puri\",\"dayspresent\":\"4\",\"totalclasses\":\"4\",\"percent_att\":\"\n\t\t\t100\t\t\t\",\"hasAttended\":\"A\",\"att_date\":\"Fri Apr 05 2013 \",\"st_class\":\"1\",\"st_section\":\"A\"}]"