TOA Posted February 1, 2011 Share Posted February 1, 2011 Hey guys, hoping you can help... I'm trying to pull questions and answers for a quiz. I have it working in multiple selects, but am trying to figure out a way to do it in one. I've tried Unions, Joins of all flavors and can't seem to get what I need. What's happening is that each question is being displayed for each answer, instead of all answers for each question. For example What's happening: Question 1 Answer 1 for question 1 Question 1 Answer 2 for question 1 etc... What I need is: Question 1 Answer 1 for question1 Answer 2 for question 1 etc... This is the sql that gets me the above: SELECT Ques_Text, Ans_Text FROM Questions NATURAL JOIN Answers Any help would be greatly appreciated, Thanks P.s if you need more info, just let me know, glad to get whatever is needed Quote Link to comment https://forums.phpfreaks.com/topic/226363-problem-with-multiple-result-sets/ Share on other sites More sharing options...
mikosiko Posted February 2, 2011 Share Posted February 2, 2011 the select that you are showing is the right one... important thing is how you DISPLAY the information... display is just a matter of simple logic.... I've seen/give several examples answering the same question here before.... just search... Quote Link to comment https://forums.phpfreaks.com/topic/226363-problem-with-multiple-result-sets/#findComment-1168673 Share on other sites More sharing options...
TOA Posted February 2, 2011 Author Share Posted February 2, 2011 the select that you are showing is the right one... important thing is how you DISPLAY the information... display is just a matter of simple logic.... Yes, I knew that. I just phrased it wrong, I guess, forgive me. I've seen/give several examples answering the same question here before.... just search... I've tried that, and google, which is why I asked here . Maybe you can recommend a term to search for since the one's I'm trying aren't bringing any results? Quote Link to comment https://forums.phpfreaks.com/topic/226363-problem-with-multiple-result-sets/#findComment-1168693 Share on other sites More sharing options...
mikosiko Posted February 2, 2011 Share Posted February 2, 2011 .... here is an small and very simple to caught fish... $sql = "SELECT Ques_Text, Ans_Text FROM Questions NATURAL JOIN Answers"; $result = mysql_query($sql) or die("Query Error : " . mysql_error()); $old_Ques = ''; // Variable to Hold the LAST DISPLAYED Quest Value while ($row = mysql_fetch_array($result)) { if ($old_Ques != $row['Ques_Text']) { // Condition to DISPLAY or not the QUEST echo "<br /> Question : " . $row['Ques_Text']; $old_Ques = $row['Ques_Text']; } echo "<br />Ans : " . $row['Ans_Text']; // DISPLAY the remaining field(s) } Quote Link to comment https://forums.phpfreaks.com/topic/226363-problem-with-multiple-result-sets/#findComment-1168879 Share on other sites More sharing options...
TOA Posted February 2, 2011 Author Share Posted February 2, 2011 .... here is an small and very simple to caught fish... $sql = "SELECT Ques_Text, Ans_Text FROM Questions NATURAL JOIN Answers"; $result = mysql_query($sql) or die("Query Error : " . mysql_error()); $old_Ques = ''; // Variable to Hold the LAST DISPLAYED Quest Value while ($row = mysql_fetch_array($result)) { if ($old_Ques != $row['Ques_Text']) { // Condition to DISPLAY or not the QUEST echo "<br /> Question : " . $row['Ques_Text']; $old_Ques = $row['Ques_Text']; } echo "<br />Ans : " . $row['Ans_Text']; // DISPLAY the remaining field(s) } Over and above my good man, all I needed was a term. Thank you for the head start. I was in the right area, just didn't have my if condition right I think. I'll try this in just a few and report back. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/226363-problem-with-multiple-result-sets/#findComment-1168888 Share on other sites More sharing options...
TOA Posted February 2, 2011 Author Share Posted February 2, 2011 Ok, implemented the same logic into my script, it worked. I just wasn't getting the comparison right. Thanks very much. Quote Link to comment https://forums.phpfreaks.com/topic/226363-problem-with-multiple-result-sets/#findComment-1168894 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.