You are here: Re: [PHP] Dropdown Building Function « PHP « IT news, forums, messages
Re: [PHP] Dropdown Building Function

Posted by Jack Jackson on 07/29/05 23:46

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="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 ;)
>
>
>
>
> 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
>>>
>>
>
>
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация