我有一张这类数字的列表,其中的字母存储在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语句来做这件事,但是我有一种感觉,它可以用一种更好的方法.可能有正则表达式
发布于 2011-09-27 02:59:48
一个从这里开始帮了我的忙,给了我这段不错的代码。做得很好。
$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 />';发布于 2011-09-25 19:27:46
看看纳提德()函数。
该函数实现了一种排序算法,该算法在维护键/值关联的同时,以人的方式对字母数字字符串进行排序。这被描述为一种“自然秩序”。
https://stackoverflow.com/questions/7547960
复制相似问题