按特定顺序显示数据库中的数据:PHP 或 MySQL


display data from database in a specific order: php or mysql?

我有一个电话号码表:

phone:
phoneID (PK)
peopleID (fK)
countrycode
areacode
phonenumber
extension
phonetype //EDIT!!! (sorry forgot this in first post)

每个人最多可以有4个电话号码:公司,传真,家庭,手机。

我有一个编辑表单。编辑表单从数据库中提取数据并填充表单。此代码拉取数据:

$stmt = $conn->prepare("SELECT * FROM phone WHERE peopleID=?");
if ( !$stmt ) {
    die(printf("Error: %s.'n", mysqli_stmt_error($stmt) ) );
}
else if ( !$stmt->bind_param('i', $peopleID) ) {
    die(printf("Error: %s.'n", mysqli_stmt_error($stmt) ) );
}
else if ( !$stmt->execute() ) { 
    die(printf("Error: %s.'n", mysqli_stmt_error($stmt) ) ); 
} else {
    $resultaddress = $stmt->get_result();
    while($row = $resultaddress->fetch_assoc()) {
        $phoneID_array[] = $row['phoneID'];  
        $phonetype_array[] = (isset ($row['phonetype']) ? $row['phonetype'] : "");
        $countrycode_array[] = (isset ($row['countrycode']) ? $row['countrycode'] : "");
        $areacode_array[] = (isset ($row['areacode']) ? $row['areacode'] : "");
        $phonenumber_array[] = (isset ($row['phonenumber']) ? $row['phonenumber'] : "");
        $extension_array[] = (isset ($row['extension']) ? $row['extension'] : "");
    }   
}

这来填充表单:

for ($i = 0; $i < 4; $i++) {
    echo 'Phone<input type="text" name="type[]" id="" value="' . (isset ($phonetype_array[$i]) ? $phonetype_array[$i] : "company") . '"/>';
    echo '<input type="text" name="countrycode[]" id="" size="3" maxlength="3" value="' . (isset ($countrycode_array[$i]) ? $countrycode_array[$i] : "") . '"/>';
    echo '<input type="text" name="countrycode[]" id="" value="' . (isset ($areacode_array[$i]) ? $areacode_array[$i] : "") . '"/>';
    echo '<input type="text" name="number[]" id="" value="' . (isset ($phonenumber_array[$i]) ? $phonenumber_array[$i] : "") . '"/>';
    echo '<input type="text" name="extension[]" id="" value="' . (isset ($extension_array[$i]) ? $extension_array[$i] : "") . '"/><br>';                
}

窗体现在显示:

公司 000 000 1234567

首页 000

000 1234569

(没有空字段最终输入新数字)

问题:

我想始终以相同的顺序显示电话号码:假设我在数据库中只有公司和家庭号码,编辑表单应如下所示

公司 : 000

000 1234567

传真:要填写的空白字段

首页 : 000

000 1234569

移动:要填写的空白字段

它可能可以用关联数组来完成,但我不知道如何:(

感谢您的帮助!

编辑:在阅读了这里的所有答案和评论后,我明白有一个专用的表电话类型,并在电话表中使用外键是一个更好的设计。认为这是一个完全不同的场景,我问了另一个问题 mysql 左连接不返回所有左表行再次感谢所有花时间帮助我的人。

您需要

向电话表添加另一个字段:

'phonetype' ENUM('company','fax','home','mobile')

和查询:

SELECT phone.* FROM people 
    LEFT JOIN phone ON phone.peopleID=people.peopleID 
WHERE people.peopleID=? AND phone.phonetype='company'
UNION 
SELECT phone.* FROM people 
    LEFT JOIN phone ON phone.peopleID=people.peopleID 
WHERE people.peopleID=? AND phone.phonetype='fax'
UNION 
SELECT phone.* FROM people 
    LEFT JOIN phone ON phone.peopleID=people.peopleID 
WHERE people.peopleID=? AND phone.phonetype='home'
UNION 
SELECT phone.* FROM people 
    LEFT JOIN phone ON phone.peopleID=people.peopleID 
WHERE people.peopleID=? AND phone.phonetype='mobile'

现在你会得到 4 行,其中一些可能是空的

我可以想到十几种方法来改善这一点;最明显的是数据库中的单独电话号码表。但是,为了最简单的插入式代码替换,您可以将其设置为按类型键控的单个数组:

/* Make an empty array with empty values for each phone type */
$emptyphone = array(
    "countrycode"=>"",
    "areacode"=>"",
    "phonenumber"=>"",
    "extension"=>"",
);
$phones = array(
    "company"=>$emptyphone + array("phonetype"=>"company"),
    "fax"=>$emptyphone + array("phonetype"=>"fax"),
    "home"=>$emptyphone + array("phonetype"=>"home"),
    "mobile"=>$emptyphone + array("phonetype"=>"mobile"),
);
/* Now fill in the values you do have */
while($row = $resultaddress->fetch_assoc()) {
    $phones[$row["phonetype"]] = $row;
}
/* Now you can loop through them with a simple foreach */
foreach($phones as $phone) {
    echo 'Phone<input type="text" name="type[]" id="" value="' . $phone["phonetype"] . '"/>';
    echo '<input type="text" name="countrycode[]" id="" size="3" maxlength="3" value="' . $phone["countrycode"] . '"/>';
    echo '<input type="text" name="countrycode[]" id="" value="' . $phone["areacode"] . '"/>';
    echo '<input type="text" name="number[]" id="" value="' . $phone["phonenumber"] . '"/>';
    echo '<input type="text" name="extension[]" id="" value="' . $phone["extension"] . '"/><br>';                
}