Jump to content

Query output displays too many times.


Go to solution Solved by bytesize,

Recommended Posts

<?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?
 

Link to comment
https://forums.phpfreaks.com/topic/276534-query-output-displays-too-many-times/
Share on other sites

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?

  • Solution

$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?

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.