[eluser]skooter[/eluser]
I am indeed using Codeigniter
With the code above I was just trying to prove my point. Anyway, I made this which seems to make my very simple question look complicated in Codeigniter. Also there's a problem:
Model:
Code:
$options_questions = array('SurveyID' => $input_id);
$query_questions = $this->db->getwhere('Questions', $options_questions);
if ($query_questions->num_rows() > 0)
{
foreach ($query_questions->result() as $row_question)
{
$survey['question'][$row_question->ID] = $row_question->Question;
$options_answers = array('QuestionID' => $row_question->ID);
$query_answers = $this->db->getwhere('Answers', $options_answers);
if ($query_answers->num_rows() > 0)
{
foreach ($query_answers->result() as $row_answer)
{
$survey['answer'][$row_question->ID][] = $row_answer->Answer;
}
}
}
}
View:
Code:
<ul>
<?php foreach($survey['question'] as $question_id => $question): ?>
<li><?=$question?>
<ul>
<?php foreach($survey['answer'][$question_id] as $answer): ?>
<li><?=$answer?></li>
<?php endforeach;?>
</ul>
</li>
<?php endforeach;?>
</ul>
... When there's no answers to errors is thrown - both pointing at the second foreach in the view:
1) Message: Undefined offset: 2
2) Message: Invalid argument supplied for foreach()
I understand why the show up but not how to fix it - of course putting an "if" inside the view is no-go.
Also is this really the way to do it or are there an easier option?