selecting data from two tables |
[eluser]Unknown[/eluser]
Hi I'm kinda new to codeigniter and I have a problem ![]() SQL sample structure: http://pastebin.com/jFag1QdU I need to generatr form that will look something like that: 1. Question 1 a) answer A b) answer B c) answer C 2. Question 2 a) answer A b) answer B c) answer C I made it this way: Model: Code: <?php Code: <?php View: Code: <!doctype html> And my problem is that I get output that looks like that: Quote:Test Started Is there any way to get rid of questions appearing for each answer and make it look like I showed you at the beginning ? Thx in advance ![]()
[eluser]mr lister[/eluser]
A nested loop is the way I would achieve what you are trying to achieve. Using your 'id' from your 'questions' table as the primary key you need another, (nested), loop to obtain the answers from your 'answers' table - as your 'questionid' is a 'foreign key' - you select the rows from the 'answers' table where 'questionid' = 'id'. Probably not the most efficient way, but, this is how I would do it: * In your controller, query the 'questions' table to obtain returned array, which will include the 'id' * In your view page, the outer loop contains the questions, with a loop nested in that loop which will obtain the answers from your 'answers' table with another query where 'questionid' = 'id'. Hope that makes sense to you and helps you.
[eluser]jmadsen[/eluser]
that would be horribly inefficient. run the single query, loop through the results, but only echo the question when the id has changed.
[eluser]mr lister[/eluser]
Ah yes, I see how that works. I am still getting over the Dreamweaver / Developer Toolbox way of doing things - using their nested loops. The M-V-C concept is far different to what I was used too, but I think is it a far better way of doing things. [Edit] Actually, the more I think about it, the more confused I am about it... in the foreach loop you would have to have some conditional statement that checks the 'id' value. Something along the lines of?: Code: <?php That should work? |
Welcome Guest, Not a member yet? Register Sign In |