0

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\"}]"
7
  • What data you are getting in '$_POST["json"]' ?? is it json encoded data ?? Commented Apr 5, 2013 at 5:24
  • @Mahendra : I have updated it.. see my edit Commented Apr 5, 2013 at 5:26
  • Hey, I had posted this on an answer but took it off because I don't actually see how it would help but it's worth a try -- try and change json_encode to have the second parameter as such: $data = json_decode($_POST['json'], true); This will force it to be an array instead of a php object Commented Apr 5, 2013 at 5:30
  • Check the double quotes that you are using. That could have cause this. try this. 0th row=> '[{"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"}]' Commented Apr 5, 2013 at 5:34
  • @francisco.preller : I tried json_decode($_POST['json'], true); but again its showing the same error Commented Apr 5, 2013 at 5:39

2 Answers 2

2

Your JSON string is invalid. It has \t and \n in it, which it shouldn't. You have to filter out those first:

$json = str_replace(array("\t","\n"), "", $_POST["json"]);
$data = json_decode($json); 

var_dump($data);

foreach($data as $item)
{
  $this->codegen_model->setCurrentAttendance($item);
}

See: this works.

Sign up to request clarification or add additional context in comments.

5 Comments

What is the difference? Did you try what I suggest? If you filter out the \n's and the \t's it works fine for me.
@NealCaffrey - Please update your question with what you tried exactly. Also: the json decoded data at the end of your question is missing ]" at the end. Is that a typo? Or is it really missing?
missing ]" was a typo and I have explained a more about my problem.. please let me know if there is still any ambiguity.. Thanks
@NealCaffrey - Your question still doesn't show that you tried the above and what error you get after trying it. When I try it, it works.
0

Please use this code. It might work for you.

EDIT

Code updates to print each student full name as an example.

    <?php
$data = '{"students" : [{"roll_no":"1101","full_name":"John Smith","dayspresent":"1","totalclasses":"2","percent_att":"50","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":"100","hasAttended":"A","att_date":"Fri Apr 05 2013 ","st_class":"1","st_section":"A"}]}';

$data = json_decode($data); 

        //var_dump($data);

        foreach($data->students as $item){
           print $item->full_name;
        }
?>

2 Comments

Seems he cannot alter the string he is receiving, so this is not possible.
@Ummar : Have a look at my note.. I am passing $item as an array to my model where I am inserting it into my db

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.