Hi, Jay,
I see perhaps I *was* thinking too complex. However this is *just* a bit
off:
Jay Blanchard wrote:
[snip]
Hi, can anyone even point me in a *direction*? I suppose this is the
most complex thing I've ever tried to do and I have been at it for tens
of hours and am still completely baffled.
Jack Jackson wrote:
Hi,
because that last topic on multipage forms was so exciting, I decided
to
database the questions as well. I wonder if anyone can help me with a
function to pull rows into dropdown boxes.
It's a 1:n relationship between questions and answers, and I have a
table of questions
q_id
q_name
q_text
q_style //dropdown, radio, checkboxes
q_cat //question category
and a table full of answers
a_id
q_id
a_answer
When I do
$fields = 'SELECT *';
$from = 'FROM questions,answers';
$sort = "ORDER BY questions.q_cat";
$where = "WHERE answers.q_id=questions.q_id";
// construct the sql query:
$sql = "$fields $from $where $sort";
// Run SQL query 1
if (!($result = mysql_query($sql))) {
echo "Could not connect to the database ($sql)" . mysql_error();
}
while ($row = mysql_fetch_assoc($result)) {
I need to loop through the results and make a dropdown list from them,
taking the question name as the select name, and then making each
answer
id and answer text the makings of an option ros.
Based on a problem a while ago which was similar but different,
someone
here actually made me a function *similar* to what I need to do now,
This one acts different and I just cannot adapt the old one, because I
still get confused at this level.
I think it wants to be something like (and note that part of the
return
is the code to react to error checking results):
$dropdown[] = '<option <?php if (' . $q_name . ' == "' . $a_id . '")
echo "selected "; ?> value=\'' . $a_id . '\'>' . $a_answer .
'</option>';
etc for all $a_answer(s)...
and then
return '<?php if (sizeof($message[\'' . $q_name . '\'])){ echo "<div
class='error'>"; } ?>
<div class=\'row\'>
<select class=\'answer\' name=\'' . $q_name . '\'>
<option value=\'\'>Select from this list</option>'
join('',$dropdown)
. '</select></div>'
. '<?php if (sizeof($message[\''. $q_name '\'])){ echo
"</div><!--/error-->"; } ?>';
Can anyone point me at the right direction?
Thanks!!
JJ
[/snip]
It seems that you have over-complicated the issue, let me boil it down a
little;
$sql = "select answer from table ";
if(!($result = mysql_query($sql, $connection))){
echo mysql_error() . "\n";
exit();
}
echo "<select name=\"answers\">\n;
while($row = mysql_fetch_array($result)){
echo "<option name=\"". $row['answer'] . "\">";
echo $row['answer'];
echo "</option>\n";
}
echo "</select>\n";
Your out put will look like this;
<select name="answers">
<option name="answer1">answer1</option>
<option name="answer2">answer2</option>
<option name="answer3">answer3</option>
</select>
Is this what you're after?
Very close to it, thank you! However this completes the gap: I am still
not sure if my syntax is right:
$sql = 'SELECT *
FROM questions,answers
WHERE answers.q_id=questions.q_id
ORDER BY questions.q_cat,questions.q_id,answers.q_id';
if(!($result = mysql_query($sql, $connection))){
echo mysql_error() . "\n";
exit();
}
/*each question has at least one answer, usually three to six
*I need to give the select the name of $q_name
*and dynamically loop through until all the question/answer
*sets have their own dropdown
*/
echo "<select name=\'" . $q_name . "\'>"\n;
while($row = mysql_fetch_array($result)){
echo "<option name=\"". $row['a_id'] . "\"";
echo "<?php if" . \$$q_name . " == " . $a_id . "echo \"selected \" ?>"\n;
echo $row['$a_answer'];
echo "</option>\n";
}
echo "</select>\n";
Then hopefully my output would look like
<select name="question_name">
<option <?php if $question_name == 1 echo "selected " ?>
value="1">Answer One</option>
<select name="question_name">
<option <?php if $question_name == 2 echo "selected " ?>
value="2">Answer Two</option>
<select name="question_name">
<option <?php if $question_name == 1 echo "selected " ?>
value="3">Answer Two</option>
</select>
TIA!!
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php