I have a list of these type of numbers with letters stored in a mySQL table
1 2a 2b 2c 3a 3b 4 5a 5b 10
Some have letters and some don't.
For the sample data above, I'd want it do display on the page like this
1
2a / 2b / 2c
3a / 3b
4
5a / 5b
10
and here is the basics of what I have so you can base your example off this which is just listing them on the page straight down
$sql = mysql_query("SELECT * FROM data") or die(mysql_error());
while($row = mysql_fetch_assoc($sql))
{
$dataID = $row['dataID'];
echo $dataID . "<br />";
}
I could probably do thi开发者_JAVA技巧s with some substrings and if statements, but I have a feeling it can be done in a much better way... probably with regular expressions
Look at the natsort() function.
This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a "natural ordering".
A guy from here helped me out and gave me this nice chunk of code. Did the trick just right.
$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 />';
精彩评论