John Davidson

php - Mysql query to select quiz options as A B C D with using extra foreach loop

0 comments
Message:


I am working on quiz concept so first all 20 question will be fetched, then each question will be loop and with every loop I am passing question id and getting options of those related question, but my requirement is to get option like A B C D infront of all options without using extra loop


Table structure for options:


id    option_value       question_id

1 some value 1
2 some value 1
3 some value 1
4 some value 1
5 some value 2
6 some value 2
7 some value 2
8 some value 2

Expected output:


option_id   option_name option_value
1 A some value
2 B some value
3 C some value
4 D some value
5 A some value
6 B some value
7 C some value
8 D some value



$question_list // all 20 question using select *



foreach($question_list as $list){
$this->db->select('id,option_vaue')->from('option')->where('question_id',$list->id)->order_by('id','asc')->get()->result();
}


My Answer:
db->query("SELECT * FROM options WHERE question_id = ".$list['id']);
$options = $this->db->result();
$option_names = ['A', 'B', 'C', 'D'];
foreach($options as $key => $option){
echo "";
echo "".$option['id']."";
echo "".$option_names[$key]."";
echo "".$option['option_value']."";
echo "";
}
}

Rate this post

5 of 5 based on 2991 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us