English 中文(简体)
从MySQL数据库中利用PHP制作多个Combo盒
原标题:Populating Multiple Combo Boxes with PHP from MySQL database

我最多选择100个 com子(现为79个),其中79个。 每一个问题都存在,并记录在MySQL的“问题”表格中。 这些备选办法记录在一份称为“问题-编码”的表格中,并填写与一些问题有关的外地名称。 我在相互之间尝试了两处住宿,但我认为,由于79*79座电梯,服务器超负荷。 我也有一个案文领域,取决于与阿贾克斯的甄选工作,工作得当。 表中“问题-编码”的0个领域是id,我没有使用。 这里是我的法典。

$code_query="SELECT * FROM questions WHERE id= 1 ";
$question_query="SELECT * FROM questions WHERE id= 2 ";

$result_question_query=mysql_query($question_query,$conn);
$result_code_query=mysql_query($code_query,$conn);

$selected_query=mysql_query("SELECT * FROM question_codes");

while($row_selected=mysql_fetch_row($selected_query))
{
    $column_count=count($row_selected);
}

while($row=mysql_fetch_array($result_question_query) && $row2=mysql_fetch_array($result_code_query))

for($i=1;$i<$column_count;$i++)
    {
        $db_q="q$i";
        $fill=$row[$db_q];
        $fill_code=$row2[$db_q];
    print( <tr style="background:navy;color:white"><td width="60px">Question  .$i. </td>
    <td>
        <select name=" .$db_q. _code" id=" .$db_q. _code" onchange="showQuestion(this)">

        <option value="">Select a question</option>

for ($j=1;$j<$column_count;$j++)
{
                $name=mysql_field_name($selected_query,$j);

if ($fill_code==$name)
        {
            $selected="selected";
        }
        else
        {
            $selected="";
        }

        print( <option value=" .$name. "  .$selected. > .$name. </option> );
}

 print( </select>

       </td>

    <td><input type="text" value=" .$fill. " name=" .$db_q. " id=" .$db_q. "></td></tr> );
最佳回答

建立数据库和PHP的方法可能比较合适。

也许像这件事一样:

CREATE TABLE `questions` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `question` varchar(255) NOT NULL,
  `answer` int(11) unsigned NOT NULL DEFAULT  0 
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


CREATE TABLE `question_options` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `question_id` int(11) unsigned NOT NULL DEFAULT  0 ,
  `option` varchar(64) NOT NULL
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

......以及PHP......

$q_sql = mysql_query("SELECT * FROM `questions`");
while ($q = mysql_fetch_row($q_sql)) {
    echo  
        <tr><td>Question  . $q[ id ] . </td>
            <td><select name="q . $q[ id ] . "> ;
    $qo_sql = mysql_query("SELECT * FROM `question_options` WHERE `question_id` =  ". $q[ id ] ." ");
    while ($qo = mysql_fetch_row($qo_sql)) {
        $selected = ($q[ answer ] == $qo[ id ]) ?   selected  :   ;
        echo  
                    <option value=" . $qo[ id ] . " . $selected . > . $qo[ option ] . </option> ;
    }
    echo  
                </select>
            </td></tr> ;
}

为了进一步强化这一方法(这种方法可以产生太多的问询),......提出单一疑问,例如:

SELECT q.`question`, qo.*
FROM `question_options` as `qo`
LEFT JOIN `questions` as q
    ON (q.`id` = qo.`question_id`)
ORDER BY qo.`question_id`

之后加上一个<代码>,同时,对问题的改动进行核对。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签