php mysql 将结果显示为下拉列表 [英] php mysql displaying results as drop down list

查看:31
本文介绍了php mysql 将结果显示为下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力尝试从数据库中获取结果以显示为下拉列表.

I am trying veru hard, to get results from a database to be displayed as a drop down list.

基本上我正在获取外键、它的唯一代码和它的名称/标题.我需要将其显示为下拉列表,以便正确输入到新表格中

Basically I am fetching the foreign key, its unique code and its name/title. I need to display it as a drop down list, to be entered correctly into a new table

        $sql = "SELECT quali_code, title FROM `qualifications`  ";
    $result = mysql_query($sql, $this->connection);
    $result_array = array();
    while($r=mysql_fetch_array($result))
    {
    $result_array[$r['quali_code']][] = $r;
    }
    return $result_array;

这样我就需要在 HTMl/PHP 中创建一个下拉列表来从中选择标题,但显然要存储值.<list name=array[title] value=array[code] >

With that I would need to create a drop down list in HTMl/PHP to select the title from, but obviously storing the value. sort of <list name=array[title] value=array[code] >

目前我已经被困在一个适合我的数组中返回我的 SQl 结果,并且不知道我将如何使用正确的数组结果填充下拉列表.

At the moment I am already stuck at returning my SQl result in an array that works for me, and have no clue how I will then populate a dropdown list with the correct array results.

上面关于我如何获取 sql 结果以及我的数组的示例代码:

Sample code above on how i fetch the sql results and here how my array looks like:

Array ( [AAT] => Array ( [0] => Array ( [0] => AAT [quali_code] => AAT [1] => AAT qualification [title] => AAT qualification ) ) [A_lev] => Array ( [0] => Array ( [0] => A_lev [quali_code] => A_lev [1] => A levels [title] => A levels ) ) [HNC] => Array ( [0] => Array ( [0] => HNC [quali_code] => HNC [1] => Fdsc/HNC [title] => Fdsc/HNC ) ) [Lan_ISLT] => Array ( [0] => Array ( [0] => Lan_ISLT [quali_code] => Lan_ISLT [1] => EISLT Qualification [title] => EISLT Qualification ) ) [Lan_qua] => Array ( [0] => Array ( [0] => Lan_qua [quali_code] => Lan_qua [1] => Language qualification [title] => Language qualification ) ) [Nat_Dip] => Array ( [0] => Array ( [0] => Nat_Dip [quali_code] => Nat_Dip [1] => National Diploma [title] => National Diploma ) ) ) 

请记住,我对 PHP 完全陌生.任何帮助,将不胜感激.但是这个数组对我来说看起来没用,我不确定如何填充列表.我获取结果/数组的文件不是我将数组填充到列表中的位置.

Bare in mind I am totally new to PHP. Any help would be appreciated. But this array looks useless to me, and am not sure how to populate a list. The file where I fetch the result / array, is not the location where I populate the array into a list.

非常感谢您对此事的任何提示

Many thanks for any hints on this matter

推荐答案

假设您要做的就是将这些行回显到下拉列表中:

Assuming all you're trying to do is echo these rows into a dropdown:

$sql = "SELECT quali_code, title FROM `qualifications` ORDER BY title ";
$result = mysql_query($sql, $this->connection);
while($r=mysql_fetch_array($result))
{
$quali_code = $r['quali_code'];
$result_array[$quali_code][] = $r['title'];
}


echo "<select name='x'>";
foreach($result_array as $q_code => $value)
{
    foreach($value as $title) {
    echo "<option value='" . $q_code . "'>" . $title . "</option>";
    }
}
echo "</select>";

这篇关于php mysql 将结果显示为下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆