bytesize Posted April 4, 2013 Share Posted April 4, 2013 <?php $user = current_user('code'); $query = sprintf("SELECT * FROM responds JOIN questions ON questions.q_id=responds.question_id JOIN users ON questions.q_questionby=users.code WHERE responds.re_respondby='%s' AND questions.q_questionby=users.code ORDER BY responds.re_id", mysql_real_escape_string($user)); $result = mysql_query($query); $exists = mysql_num_rows($result);?>If I have more than one answer per question, the same question will display multiple times depending on how many answers there are for that question.Example question: How can I do this if I'm doing that?Let's say there are 10 answers to that question.3 of those answers are mine.That question will display 3 times instead of once.Can this be done in the query? Quote Link to comment https://forums.phpfreaks.com/topic/276534-query-output-displays-too-many-times/ Share on other sites More sharing options...
requinix Posted April 4, 2013 Share Posted April 4, 2013 Not really. Do it in code instead: 1. Make your query sort by the question so that way all the answers for the same question will come together 2. As you loop through the answers, keep track of the current and previous question 3. When the question changes (or it's the first answer) then you display it anew, otherwise it's the same so don't do anything 4. Then display the answer What kind of output are you aiming for? Quote Link to comment https://forums.phpfreaks.com/topic/276534-query-output-displays-too-many-times/#findComment-1422928 Share on other sites More sharing options...
Solution bytesize Posted April 4, 2013 Author Solution Share Posted April 4, 2013 $user = current_user('code'); $query = sprintf("SELECT * FROM responds JOIN questions ON questions.q_id=responds.question_id JOIN users ON questions.q_questionby=users.code WHERE responds.re_respondby='%s' AND questions.q_questionby=users.code ORDER BY questions.question", mysql_real_escape_string($user)); $result = mysql_query($query); $exists = mysql_num_rows($result); while($row = mysql_fetch_array($result)) { if($user!=$row['code']) { echo $row["question"]; } } Thank you for responding to my question. This is the display output. The numbers represent the question id. 6 Proin id lorem nunc, eget scelerisque lectus. Nulla quis nisi accumsan leo rhoncus elementum.… 6 Proin id lorem nunc, eget scelerisque lectus. Nulla quis nisi accumsan leo rhoncus elementum.… 16 Eget diam mi. Nam sed lectus vel metus elementum varius. Curabitur tincidunt faucibus neque… 3 Diam libero iaculis a gravida ac elementum ac dolor. In hac habitasse platea dictumst. Nulla tem… 3 Diam libero iaculis a gravida ac elementum ac dolor. In hac habitasse platea dictumst. Nulla tem… 20 Cras aliquam odio a metus varius eget dictum ipsum accumsan. Maecenas justo dui, aliquam… 20 Cras aliquam odio a metus varius eget dictum ipsum accumsan. Maecenas justo dui, aliquam… As you can see, the question is being display more than once for questions with more than one answer. The $user variable is showing all my answers in responds.re_respondby='%s' How can I show only one question? Quote Link to comment https://forums.phpfreaks.com/topic/276534-query-output-displays-too-many-times/#findComment-1422982 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.