I am trying to read the data from the excel file. Every topic has just one question so i have done it easily by reading rows.
I'm readying the topic's questions easily row by row & inserting it into db perfectly. Here is my code where I'm inserting the data.
foreach ($rowData as $rowIndex => $row) {
$topicQuestion = trim($row[2]);
$topicOptions = trim($row[3]);
$topicAnswer = trim($row[4]);
// Then im saving the questions, options, answers, (works Perfectly)
$question = new Question();
$question->topic_id = trim($row[0]);
$question->description = $topicQuestion;
$question->save();
if ($question->id) {
$options = explode("\n", $topicOptions);
foreach ($options as $option) {
$optionNumber = trim(substr($option, 0, 2));
$optionNumber = preg_replace('/\D/', '', $optionNumber);
$answer = new Answer();
$answer->description = $optionDescription;
$answer->question_id = $question->id;
$answer->answer_number = $optionNumber;
$answer->is_correct = ($answer->answer_number == $topicAnswer) ? 1 : 0;
$answer->save();
}
}
}
But I have the data in a different way, Every topic has 1 question before, But it has multiple questions? I'm a little bit confused now How can use the get the questions, options, answers in an array so i can use the foreach loop
before $question = new Question();
, Any suggestions?
Here is sample of data.. Every topic can have multiple Questions
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2sTeszX
via IFTTT
Aucun commentaire:
Enregistrer un commentaire