SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying Query Results

    Hi all,

    I am working on developing a multiple choice quiz.

    Questions are stored in question table.
    Each answer is stored in answer table.
    Multiple answers are linked to each relevant question via question_ID

    I want to display each question with its relevant answer. At the moment my query displays the question six times.
    How do I fix the query?

    PHP Code:
     $query "SELECT question_text, answer_text ".
                       
    "FROM question c, answer a ".
                      
    "WHERE c.question_ID = a.question_ID ".
                      
    "ORDER BY c.question_ID";
     
    $result mssql_query($query) or die('Select Query Error');
     
    $num_results mssql_num_rows($result);
     echo 
    '<p>Results: '.$num_results.'</p>';
     
     
    // Display Question Results
     
    while($row mssql_fetch_array($result))
     {
                  echo 
    '<p>'.$row['question_text'].'</p>';
     }
     
     
    // Display Answer Results
     
    while($row mssql_fetch_array($result))
     {        
                  echo 
    '<label for="a1">'.$row['answer_text'].'</label>';
                 echo 
    '<input type="radio" name="a1" id="a1" value="" />';
     } 
    Thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    At the moment my query displays the question six times.
    I'm sure you probably realize this, but queries don't display anything; they retrieve information from a database.

    I want to display each question with its relevant answer.
    Your while loops are directing php to first display a list of all the questions:
    Code:
    // Display Question Results
    while($row = mssql_fetch_array($result))
    {
                 echo '<p>'.$row['question_text'].'</p>';
    }
    and then display a list of all the answers:
    Code:
    // Display Answer Results
    while($row = mssql_fetch_array($result))
    {        
                 echo '<label for="a1">'.$row['answer_text'].'</label>';
                echo '<input type="radio" name="a1" id="a1" value="" />';
    }
    Is that what you want? Or do you want to display a question followed by its answer(s), followed by the next question and its answer(s), etc? If its the latter, you can use DISTINCT to get only the unique questions:

    $query1 = 'SELECT DISTINCT question_ID, question_text FROM question ORDER BY question_ID';

    and use a while loop to step through each question. In the while loop, you would first display the question, and then you would set up an inner while loop to query the answer table for that question_ID and display the corresponding answers.

    Or, if you only want to make one query to the database, you can use php to sort the information returned by your present query, something like this:
    Code:
    while($row = mysql_fetch_array($sql_result) )
    {
        $questWithAnswsArray = [ $row['question_text'] ][] =  $row['answer_text'];
    }
    and then display the results like this:
    Code:
    foreach($questWithAnswsArray as $key=>$valueArray)
    {
        echo $key . "<br/>";
        
        foreach($valueArray as $value)
    
        {
            echo $value . "<br/>";
        }
        
        echo "<br/>";
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •