0

I have an error when I am going to insert to database.

I have this array:

When I print_r($students) its structure is this:

Array ( [0] => stdClass Object ( [lastname] => en [firstname] => estudianten [code] => U0009876 [id_estud] => 5 ) [1] => stdClass Object ( [lastname] => Euno [firstname] => estudiante| [code] => U00020814 [id_estud] => 6 ) )

In my model I have this code:

function insert_register_students($students) {
        foreach ($students as $student) {
            foreach ($student['dates'] as $key => $value) {
                $data = array(
                    'field1' =>$student['id'],
                    'field2' => $key,
                    'field3' => '',
                );
                $this->db->insert('mytable', $data);
            }
        }
    }

In the model how can I do reference that $students is a stdClass Object? The last code in the model works well for me if $students is an array but now has stdClass Object.

What is my error?

Thanks for your help.

4
  • which error you see? You do foreach ($student['dates'] as $key => $value) but in the array there are not 'dates'... Commented Nov 28, 2011 at 12:19
  • Thanks for your answer. There is other array named dates_data, where for each students are registered many values for each date. Commented Nov 28, 2011 at 12:41
  • Have you tried the answers below? Commented Nov 28, 2011 at 12:49
  • Yes, I am trying. Thanks very much. Commented Nov 28, 2011 at 12:52

2 Answers 2

1

Actually you have an array of stdObject. On each iteration in the foreach you handle an object. So if you want to access its properties you have to do

'field1' => $student->property,

instead of

'field1' => $student['property']
Sign up to request clarification or add additional context in comments.

Comments

1

You could do:

foreach ($students as $student) {
  echo $student->lastname; //and so on
}

Hope it helps

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.