while loop problems:(

Greetings, thanks in advance to you experts I’m trying to group some records in my database for a quiz,
I’m can’t sort out how in the world to add a closing after the last record in the group, it seems no matter how i try I get an

  • after each individual record. Can someone please help me?

    thanks again
    here’s my code:

    $theQuestions = mysql_query("
    SELECT
    q.id
    ,q.question
    ,a.id
    ,a.answer
    ,a.questionId
    FROM
    questions AS q
    INNER JOIN
    answers AS a
    ON
    q.id = a.questionId

    ");

    while($row = mysql_fetch_array($theQuestions)){

    		if ($cur_Qid != $row['questionId']){ //first record in this category
    			echo  '<li>'  . "\n"; //  li
    			echo  $row['question']. '<br />' . "\n" . '<br />' . "\n"; //print the category header
    		}
    
    
    		$cur_Qid = $row['questionId'];
    		echo '<label><input type="radio" name="question-' . $row['questionId'] . '" value="radio"  />';
    		echo  $row['answer'] . "\n";
    		echo '</label>' . '<br />'. "\n";
    
    
    }
    

    here’s the results in html

  • How would you rate the overall quality of your visit?

    Poor

    Below average


    Average


    Above average


    Excellent

  • Just echo right after the while loop.

    thanks for your time, unfortunately that just ads 1 after the entire loop, I need an after each group:(

    it now looks like this…

  • question?

    0-5
    6-10
    11-20
    21-30
    More than 30
  • Question 2?

    0-29
    30-59
    60-89
    90-99
    More than 100
  • Question 3?

    Poor

    Below average


    Average


    Above average


    Excellent

  • Thanks again for lending your expertise!

    Hi there,

    The simplest way may be to do:
    [php]
    $i = 1;
    $rows = mysql_num_rows($theQuestions);
    while($row = mysql_fetch_array($theQuestions)){
    if ($i == 1){ //first record in this category
    echo “

  • \n”;
    echo $row[‘question’]."
    \n
    \n";
    }
    $cur_Qid = $row[‘questionId’];
    echo ‘’;
    echo $row[‘answer’] . “\n”;
    echo ‘’ . ‘
    ’. “\n”;
    if($i == $rows-1)
    {
    echo “
  • ”;
    }
    $i++;
    }
    [/php]

    I haven’t tested this, but give it a shot and let me know how it goes.

    Thanks Smokey, You Rock!

    No problem, glad to be of assistance!

    Sponsor our Newsletter | Privacy Policy | Terms of Service