Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeigniter - Parse each row of json data
    primarykey
    data
    text
    <p>Hi I am using foreach loop to parse each row of json data..</p> <p>What <strong>exactly</strong> 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..</p> <p>The php code is :</p> <pre><code>$data = json_decode($_POST["json"]); var_dump($data); foreach($data as $item){ $this-&gt;codegen_model-&gt;setCurrentAttendance($item); } </code></pre> <p><strong>NOTE</strong></p> <pre><code>$this-&gt;codegen_model-&gt;setCurrentAttendance($item); </code></pre> <p>redirects to my model where I am trying to pass the $item as an array and inserting it into the database..</p> <pre><code>function setCurrentAttendance($data){ $this-&gt;db-&gt;insert('table_name', $data); if ($this-&gt;db-&gt;affected_rows() &gt;= '1') { return TRUE; } return FALSE; } </code></pre> <p>The json data variable $data is :</p> <pre><code> "[{"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"}]" </code></pre> <p>But I am getting the following error and not able to guess WHY ??</p> <pre><code>&lt;div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;"&gt; &lt;h4&gt;A PHP Error was encountered&lt;/h4&gt; &lt;p&gt;Severity: Warning&lt;/p&gt; &lt;p&gt;Message: Invalid argument supplied for foreach()&lt;/p&gt; &lt;p&gt;Filename: controllers/controller_name.php&lt;/p&gt; &lt;p&gt;Line Number: 245&lt;/p&gt; &lt;/div&gt; </code></pre> <p>Please let me know where I am wrong.</p> <p>Thanks in advance.</p> <p><strong>UPDATE</strong></p> <p>json encoded data:</p> <pre><code>"[{\"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\"}]" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload