Re: [PHP] Dropdown Building Function
Jack Jackson [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Aaron, Thanks for showing me this it is very cool indeed. However maybe I am being dumb and obdurate but what I am really trying to do is build some of these things myself (with, thankfully, help) Perhaps this can help. I'm sorry the doc is in Spanish, but the code is not so large. There are two funtions, one to load the data for a dropdown list (CargaCombo) and the other to show it. The reason for this is that a single combo might be used in two or more places within a web page so this allow for reusability, plus it allows for the display of combos not generated from the database but from a plain handcoded array, such as a list of month names. CargaCombo expects an SQL statement. It will assume the first field is the key of the dropdown box. It will then concatenate every field from the second to whichever to make the description. MuestraCombo will display the dropdown from an array. It will assume the key of the array to be the value attribute of the option tag and the value of the array to be the description. The first parameter is the name of the select tag, the third the key value of the item to be pre-selected and the fourth an array of events for the object. This last one has to be in the form of eventName = action. For example: MuestraCombo('SomeName', array(0='No',1='Yes'),1,array('onClick' = 'doSomething();')); Satyam /** * Esta función devuelve un array que se puede usar en [EMAIL PROTECTED] MuestraCombo MuestraCombo} para mostrar un combobox. * * La función toma una instrucción de SQL y con ella arma un array donde el primer campo que encuentra lo usa como * clave dentro del array y los campos subsiguientes como valor. Si hubiera más de dos campos, concatenará los valores * de los campos desde el segundo en adelante separándolos con barra vertical para asignar como valor a la clave dada * por el primer campo. Usualmente se usa con una tabla del tipo Codigo == Descripción * * @param string $sql Instrucción de selección de SQL, debe proveer al menos dos campos. * @return array Array con una entrada por registro obtenido, usando el campo 0 como clave y los subsiguientes como valor */ function CargaCombo($sql){ $result = mysql_query($sql) or die ('Error en la consulta: ' . mysql_error() . \r\nbr$sql); while ($line = mysql_fetch_row($result)){ $s = ''; for ($i = 1;$i mysql_num_fields($result);$i++) $s .= ' | ' . $line[$i]; $combo[$line[0]] = substr($s, 3); } return $combo; mysql_free_result($result); } /** * Emite el código HTML correspondiente a un ComboBox usando el array que se indica. * * El array puede haber sido obtenido mediante * [EMAIL PROTECTED] CargaCombo CargaCombo}, o generado por otro medio. * Por cada item del array emitirá un samplt;optiongt;/samp con sampvalue/samp igual a la clave y el texto * a mostrar igual al valor correspondiente a esa clave. * * @param string $Nombre Nombre que se le dará al control * @param array $Combo Array donde aparecen los valores a mostrar * @param mixed $Default Clave del item que deberá aparecer seleccionado inicialmente * @param array $Eventos Array de acciones por eventos a ser asignadas a este control. * El array debe estar estructurado con el nombre del evento como clave y la acción asociada como dato. */ function MuestraCombo($Nombre, $Combo, $Default = null, $Eventos = null){ echo 'Select size=1 name=' , $Nombre ,''; if ($Eventos){ foreach($Eventos as $OnEvent = $Funcion){ echo ' ', $OnEvent, '=', $Funcion , ''; } } echo '', CRLF; foreach ($Combo as $key = $value){ echo 'Option value=' , htmlentities($key) , ''; If ($key == $Default Or (Is_Null($key) And Is_Null($Default))) echo ' selected '; echo '', $value , '/option', CRLF; } echo '/Select',CRLF; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dropdown Building Function
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Dropdown Building Function
[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=answer1answer1/option option name=answer2answer2/option option name=answer3answer3/option /select Is this what you're after? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dropdown Building Function
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=1answer 1/option option value=2answer 2/option option value=3answer 3/option select name=question2 option value=1answer 1/option option value=2answer 2/option option value=3answer 3/option option value=4answer 4/option option value=5answer 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 ;) kgt Jack Jackson wrote: 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dropdown Building Function
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=answer1answer1/option option name=answer2answer2/option option name=answer3answer3/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=1Answer One/option select name=question_name option ?php if $question_name == 2 echo selected ? value=2Answer Two/option select name=question_name option ?php if $question_name == 1 echo selected ? value=3Answer Two/option /select TIA!! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dropdown Building Function
Kristen, Thank you so much this is precisely what I was trying to accomplish. However I was also trying to learn how to do it with those arrays you mentioned which I figure will help me emerge from total newbieness. To date, I hope the list has seen I've progressed from absolutely *no* programming experience whatever to the point where I feel comfortable saying I am a newbie programmer (as opposed to a clueless one). But Multidimensional arrays still baffle me and I believe that certain aspects of them work by magic! I hope that people here will help me figure out both approaches. And thank you so much for the code here and the fix, which even I can follow! JJ 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=1answer 1/option option value=2answer 2/option option value=3answer 3/option select name=question2 option value=1answer 1/option option value=2answer 2/option option value=3answer 3/option option value=4answer 4/option option value=5answer 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 ;) kgt Jack Jackson wrote: 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dropdown Building Function
And of course, I am finding that this way of yours is the easiest!! Thanks, again, Kristen JJ 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=1answer 1/option option value=2answer 2/option option value=3answer 3/option select name=question2 option value=1answer 1/option option value=2answer 2/option option value=3answer 3/option option value=4answer 4/option option value=5answer 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 ;) kgt Jack Jackson wrote: 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dropdown Building Function
Jay, This is why I think frameworks are helpful Here's how I'd do it with Lampshade in one line of code with my own hypothetical field names: dropdownBox('answerid', SELECT answerid, CONCAT(questions.description,' - ',answers.description) AS display FROM questions LEFT JOIN answers ON questions.questionid=answers.questionid ORDER BY questions.description, answers.description, $myrow['answerid'], 'Question and Answer', 'Choose an Answer'); For more information on this function: http://www.thinkcomputer.com/software/lampshade/documentation.html?function=dropdownBox I'd be happy to help you get it set up if you have any questions. Aaron Aaron Greenspan President CEO Think Computer Corporation http://www.thinkcomputer.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dropdown Building Function
Aaron, Thanks for showing me this it is very cool indeed. However maybe I am being dumb and obdurate but what I am really trying to do is build some of these things myself (with, thankfully, help) so that I understand enough that when I see a great tool like yours I'll actually appreciate what it's doing for me. I can see that your tool is doing everything in one line, and I think its really great. I just am such a beginner that I think tools like that would encourage me to *never* get above this level I'm at now. I got the questions and answers thing working, starting with Kristen's two-query code and moving from there. I would like to come back to see how it can be done with one query and one function, but at this point my eyes are so bleary I'm grateful for the fact that I can now focus on getting the form to process! Thanks again JJ Aaron Greenspan wrote: Jay, This is why I think frameworks are helpful Here's how I'd do it with Lampshade in one line of code with my own hypothetical field names: dropdownBox('answerid', SELECT answerid, CONCAT(questions.description,' - ',answers.description) AS display FROM questions LEFT JOIN answers ON questions.questionid=answers.questionid ORDER BY questions.description, answers.description, $myrow['answerid'], 'Question and Answer', 'Choose an Answer'); For more information on this function: http://www.thinkcomputer.com/software/lampshade/documentation.html?function=dropdownBox I'd be happy to help you get it set up if you have any questions. Aaron Aaron Greenspan President CEO Think Computer Corporation http://www.thinkcomputer.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dropdown Building Function
On Fri, 2005-07-29 at 15:58 -0400, Jack Jackson wrote: 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 JJ, I like to TBS (TinyButStrong) for all of my PHP-HTML needs. There are build in functions to handle output from database to a list like this as well as wide depth of other great features for auto-generating specific HTML. http://www.tinybutstrong.com Dave -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dropdown Building Function
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=1answer 1/option option value=2answer 2/option option value=3answer 3/option select name=question2 option value=1answer 1/option option value=2answer 2/option option value=3answer 3/option option value=4answer 4/option option value=5answer 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