1

I am working on a QA app where i need to add multiple answers to a question as per requirement dynamically. for this i am sending an object which carry question and the answers like.

question={
    'question_text':'what is your name',
    'answers':[
            {'answer_text':'some answer','isCorrect':'1'},
            {'answer_text':'some answer','isCorrect':'1'},
            {'answer_text':'some answer'}    // answer may or may not have isCorrect key
        ]
    }

On server side I have two tables or migrations 1 for Question and 1 for answer. The answer table have three fields question_id, answer_text' andisCorrect'. question_id is the foreign key of question in answer table.

to store the objects what i am doing is

$question_text=Input::get('question_text');
$answers=Input::get('answers');

$question=new Question;
$question->question_text=$question_text;
$question->save();


foreach($answers as $ans){ 
   $answer=new Answer;
   $answer->question_id=$question->id;
   $answer->answer_text=$ans->answer_text;
   if($answer->isCorrect){
        $answer->is_correct=$ans->isCorrect;
   }
   else{
        $answer->is_correct=0;
   }
   $answer->save();
}

But while iterating there is an error

`production.ERROR: exception 'ErrorException' with message 'Trying to get property of non-object'`  

what wrong I am doing here. I am first time working on PHP. I am trying to iterate it like Javascript or Python way. Tell me how can i get the values of answers array object and store them.

Sajid Ahmad
  • 1,124
  • 4
  • 18
  • 40
  • What is the meaning of `$ans.answer_text;`? '.' is the text concatenation operator in PHP; this is not Java. You probably want `->`. – LSerni May 23 '14 at 11:29
  • 1
    Also, you shouldn't use `$answer->isCorrect` if you're not sure the property exists. Either use `isset()` to be sure, or just assign it based on `is_correct`: `$answer->isCorrect = isset($ans->is_correct)? (int)$ans->is_correct : 0;` – LSerni May 23 '14 at 11:32
  • that was -> notation of PHP. i have corrected that. Thanx Iserni – Sajid Ahmad May 23 '14 at 11:40
  • Are you sure Sajid? Check your database values. Input::get('answers') should be returning an array. So $ans is an array, not an object. – Laurence May 23 '14 at 11:46
  • Answer is an array not Object?? i am not getting you The Shift Exchange. – Sajid Ahmad May 23 '14 at 11:55
  • $answers (which you convert to $ans) is an array. Not an object. So doing $ans['answer_text'] is the correct way to access them. Unless there is more code which you have not posted, it is an array, not an object. – Laurence May 23 '14 at 12:00
  • It's my second day on PHP. so i am confused in PHP array and Objects also. I wrote the question in term or Python or JavaScript.which returns array / list of object when found multiple – Sajid Ahmad May 23 '14 at 12:01
  • `$answer->isCorrect = isset($ans->is_correct)? (int)$ans->is_correct : 0;` Using this statement it is always giving 0 as is_correct even if the key is exists in the object – Sajid Ahmad May 26 '14 at 07:05

1 Answers1

6

You dont seem to be referencing the array variables correctly. This should work:

foreach($answers as $ans){ 
   $answer=new Answer;
   $answer->question_id=$question->id;
   $answer->answer_text=$ans['answer_text'];
   $answer->is_correct = isset($ans['isCorrect']);
   $answer->save();
}

p.s. Im not sure about forEach - I'm suprised it works - but you should probably rename it to foreach to confirm to the normal standards

Laurence
  • 58,936
  • 21
  • 171
  • 212