2011-09-25 131 views
0

我存储在MySQL表排序/按数字显示,然后按字母顺序

1 
2a 
2b 
2c 
3a 
3b 
4 
5a 
5b 
10 

一些有字母和一些没有这些类型的数字与字母的列表。

对于上面的样本数据,我希望它做的网页上显示这样

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 />"; 
} 

我大概可以做一些子和if语句,但我有一种感觉它能够在一个更好的方式来完成...或许正则表达式的

+0

如果有一个简单的方法,我将寻求帮助哈哈 – abney317

+1

哈哈看看natsort()函数http://ee.php.net/natsort哈哈.. – evilone

回答

0

一个人from here帮了我,给了我这个不错的代码块。这个诀窍恰到好处。

$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 />'; 
1

看看natsort()函数。

该功能实现了订单的字母数字字符串 在一个人会同时保持键/值 关联方式的排序算法。这被描述为“自然排序”。

+0

这绝对是有用的。谢谢。我仍然不确定我将如何获得格式化的值,虽然我想要的 – abney317