Re: Dropdown Building Function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





Kristen G. Thorson wrote:
I'm not 100% sure where you're saying you're stuck, but I think you're trying to do with one query what you will probably find is best to do with several. Hopefully the following will help some:


//first get all the questions
$sql = "select * from questions";
$result = mysql_query( $sql );

//now one-by-one go through the questions
while( $row = mysql_fetch_row( $result ) {

   //create the drop down box for THIS question
   echo '<select name="'.$row['question_text'].'">';

   //get all of the answers for THIS question
$ans_sql = "select * from answers where question_id=".$row['question_id'];
   $ans_result = mysql_query( $ans_sql );
   while( $ans_row = mysql_fetch_row( $ans_result ) ) {

      //list the answers for THIS question
echo '<option value="'.$ans_row['answer_id'].'">'.$ans_row['answer_text'].'</option>';
   }
   echo '</select>';
}

outputs:
<select name="question1">
<option value="1">answer 1</option>
<option value="2">answer 2</option>
<option value="3">answer 3</option>

<select name="question2">
<option value="1">answer 1</option>
<option value="2">answer 2</option>
<option value="3">answer 3</option>
<option value="4">answer 4</option>
<option value="5">answer 5</option>



It requires numerous DB calls, but I believe in most cases, it's still better than what you'd have to do otherwise (create some arrays and do some sorting, etc.). That is, if I've understood what you need ;)


 And it did!!
//first get all the questions
$sql = "select * from questions ORDER BY q_cat";
$result = mysql_query($sql);

	//now one-by-one go through the questions
	while($row = mysql_fetch_assoc($result)) {

		//if the form has been submitted, and the question unanswered
		//highlight this whole question and answer block in red.
		if (sizeof($message[$row['q_name']])){
			echo "<div class='error'>";
			}
			
		//State the question
		echo "<div class='question'>";
		echo $row['q_text'] . "</div>";
		echo "\n\n";
		echo " <div class='answer'>";
		echo "\n";
		
		//Create the dropdown for the answers
		echo '  <select name="' . $row['q_name'] . '">';
		echo "\n";
		
		//get all of the answers for THIS question
	$ans_sql = "select * from answers where answers.q_id=" . $row['q_id'];
	$ans_result = mysql_query($ans_sql);

		echo "    <option value=\"\">Select from this list</option>\n";
	while($ans_row = mysql_fetch_assoc($ans_result)) {
		
		//list the answers for THIS question
		echo "    <option ";
		echo "value=\"" . $ans_row['a_id'] . "\"";

		if  ($row['q_name'] == $ans_row['a_id']) {
			echo " selected";
			}

		echo ">" . $ans_row['a_answer'] . "</option>";
		echo "\n";
		}
		echo '  </select>' . "\n" . ' </div>' . "\n\n";
		//If there *was* an error div, close it
		if (sizeof($message[$row['q_name']])){
			echo "</div><!--/error-->";
			}
	}



Thanks so much!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux