Php 按数字和字母顺序排序/显示
我有一个存储在mySQL表中的带有字母的数字列表 有些有字母,有些没有 对于上面的示例数据,我希望它能像这样显示在页面上Php 按数字和字母顺序排序/显示,php,Php,我有一个存储在mySQL表中的带有字母的数字列表 有些有字母,有些没有 对于上面的示例数据,我希望它能像这样显示在页面上 1 2a / 2b / 2c 3a / 3b 4 5a / 5b 10 这是我的基本知识,所以你可以根据这个例子,直接在页面上列出它们 $sql = mysql_query("SELECT * FROM data") or die(mysql_error()); while($row = mysql_fetch_assoc($sql)) { $dataID = $
1
2a / 2b / 2c
3a / 3b
4
5a / 5b
10
这是我的基本知识,所以你可以根据这个例子,直接在页面上列出它们
$sql = mysql_query("SELECT * FROM data") or die(mysql_error());
while($row = mysql_fetch_assoc($sql))
{
$dataID = $row['dataID'];
echo $dataID . "<br />";
}
$sql=mysql\u查询(“选择*来自数据”)或死(mysql\u error());
while($row=mysql\u fetch\u assoc($sql))
{
$dataID=$row['dataID'];
echo$dataID。“
”;
}
我可能可以用一些子字符串和if语句来实现这一点,但我感觉可以用更好的方式实现。。。可能使用正则表达式查看函数
此函数实现对字母数字进行排序的排序算法
字符串,就像人类在维护键/值时使用的字符串一样
协会。这被称为“自然有序”
一个家伙帮了我,给了我这段代码。把戏做得恰到好处
$query = "select dataID, dataID * 1 as ord FROM data order by ord, dataID";
$result = mysql_query($query);
$heading = null; // remember last heading, initialize to a value that will never exist as data
while(list($dataID,$ord) = mysql_fetch_row($result)){
if($heading != $ord){
// a new (or first) heading found
if($heading != null){
// not the first heading, close out the previous section
echo implode (' / ',$data) . '<br />';
}
$heading = $ord; // remember new heading
// start a new section
$data = array();
}
// handle each piece of data
$data[] = $dataID;
}
// close out the last section
echo implode (' / ',$data) . '<br />';
$query=“按ord,dataID从数据顺序中选择dataID,dataID*1作为ord”;
$result=mysql\u query($query);
$heading=null;//记住最后一个标题,初始化为一个永远不会作为数据存在的值
while(list($dataID,$ord)=mysql\u fetch\u row($result)){
如果($heading!=$ord){
//找到新的(或第一个)标题
如果($heading!=null){
//不是第一个标题,结束上一节
回波内爆(“/”,$data)。“
”;
}
$heading=$ord;//记住新的标题
//开始一个新的部分
$data=array();
}
//处理每一条数据
$data[]=$dataID;
}
//结束最后一部分
回波内爆('/',$data)。'
;
如果有一个简单的方法,我会寻求帮助哈哈哈…看看natsort()函数哈哈哈…那肯定很有用。谢谢我仍然不确定如何将值格式化为我想要的格式
$query = "select dataID, dataID * 1 as ord FROM data order by ord, dataID";
$result = mysql_query($query);
$heading = null; // remember last heading, initialize to a value that will never exist as data
while(list($dataID,$ord) = mysql_fetch_row($result)){
if($heading != $ord){
// a new (or first) heading found
if($heading != null){
// not the first heading, close out the previous section
echo implode (' / ',$data) . '<br />';
}
$heading = $ord; // remember new heading
// start a new section
$data = array();
}
// handle each piece of data
$data[] = $dataID;
}
// close out the last section
echo implode (' / ',$data) . '<br />';