gpt4 book ai didi

php - 对结果进行排序 mysql/php

转载 作者:行者123 更新时间:2023-11-30 01:28:48 24 4
gpt4 key购买 nike

假设我有一个装满手机的列表。很容易以无序列表形式打印所有电话,例如:

$get_phones = $mysqli->query("
SELECT
a.id,
b.phone_id,
a.phonename AS pname,
b.modelname AS mname,
FROM phone_brands
a LEFT OUTER JOIN phone_models b
ON a.id = b.phone_id");

while($phones = $get_phones->fetch_assoc()){
echo $phones['pname'] . $phones['mname'];}

但是我如何通过对所有型号和手机进行排序来使列表更具可读性,例如:

iPhone
3G
3GS
4G

诺基亚
Lumia 1020
Lumia 925
Lumia 520

我的猜测是我应该这样做:

if($phones['pname'] == $phones['pname']{}

但我不知道我离这里是否遥远?任何帮助将不胜感激:)

最佳答案

当然,您可以按 mname 对查询进行排序:

$get_phones = $mysqli->query("
SELECT
a.id,
b.phone_id,
a.phonename AS pname,
b.modelname AS mname,
FROM phone_brands
a LEFT OUTER JOIN phone_models b
ON a.id = b.phone_id
ORDER BY mname");

然后在 php 中执行如下操作:

$currentModel = "";

while($phones = $get_phones->fetch_assoc()){
if($currentModel != $phones['mname']){
echo $phones['mname'].'<br/>';
$currentModel = $phones['mname'];
}
echo $phones['pname'];
}

关于php - 对结果进行排序 mysql/php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17698904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com