Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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.

share|improve this question
    
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. – The Shift Exchange 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

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer
    
i have made some changes in question. please see again and consider if condition in your answer. Thank you – Sajid Ahmad May 23 '14 at 11:28
    
I've updated my answer. Also - if this doesnt work - the error should tell you what "line" the error occurs on - just tell us the exact code on that line. – The Shift Exchange May 23 '14 at 11:31
    
Just fixed another issue I found. Use the latest code. – The Shift Exchange May 23 '14 at 11:32
    
you are right The Shift Exchange. It is foreach not forEach. forEach is in javascript – Sajid Ahmad May 23 '14 at 12:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.