动态显示名字和姓氏作为选择选项

时间:2018-06-18 06:03:21

标签: php sql select dynamic option

我正在尝试动态显示数据库中人员的名字和姓氏。我已成功显示名字,但我不知道如何使用姓氏。任何帮助将不胜感激:)

PHP:

$swimmername_query = "SELECT * From Swimmers ORDER BY FirstName ASC";
$swimmername_results = mysqli_query($dbcon, $swimmername_query);
echo "<option value=>";
echo "Select Customer";
echo "</option>";

while ($displayname = mysqli_fetch_assoc($swimmername_results)){
echo "<option value = '".$displayname['FirstName']."'>".$displayname['FirstName']."</option>";
            }

2 个答案:

答案 0 :(得分:2)

您可以直接在查询中连接它,如下所示:

$swimmername_query = "SELECT *, CONCAT(FirstName, '-', LastName) AS FullName From Swimmers ORDER BY FirstName ASC";
$swimmername_results = mysqli_query($dbcon, $swimmername_query);
echo "<option value=>";
echo "Select Customer";
echo "</option>";

while ($displayname = mysqli_fetch_assoc($swimmername_results)){
     echo "<option value = '".$displayname['FullName']."'>".$displayname['FullName']."</option>";
}

或者在while循环中连接字符串:

$swimmername_query = "SELECT * From Swimmers ORDER BY FirstName ASC";
$swimmername_results = mysqli_query($dbcon, $swimmername_query);
echo "<option value=>";
echo "Select Customer";
echo "</option>";

while ($displayname = mysqli_fetch_assoc($swimmername_results)){
    $fullname = $displayname['FirstName']."-".$displayname['LastName'];
    echo "<option value = '".$fullname."'>".$fullname."</option>";
}

答案 1 :(得分:0)

最好将ID列作为值,并在查询中将firstname与lastname合并在一起。

$swimmername_query = "SELECT SwimmersID, Firstname || ' ' || Lastname SwimmerName From Swimmers ORDER BY FirstName ASC";
$swimmername_results = mysqli_query($dbcon, $swimmername_query);
echo "<option value=>";
echo "Select Customer";
echo "</option>";

while ($displayname = mysqli_fetch_assoc($swimmername_results)){
echo "<option value = '".$displayname['SwimmersID']."'>".$displayname['SwimmerName']."</option>";