在PHP中按字母顺序排序HTML列表
I have a website that contains a manual with many office precedents. When a user uses the HTML upload script it uploads the precedent to a folder on the server. I use the following script to list out all the files in that specific folder.
<?php //define the path as relative
$dir = "./OFFICE PRECEDENTS/Business & Corporate Law";
$webpath =""; //using the opendir function
echo "<ol>";
// Open a known directory, and proceed to read its contents
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (is_dir($dir.'/'.$file)){}
else if ($file == '.'){}
else if ($file == '..'){}
else if ($file == 'index.php'){}
else {
echo "<li><a href='https://manual.malicki-law.ca/$dir/$file'>$file</a></li>
";
}
}
closedir($dh);
}
echo "</ol>";
?>
How can I implement a system to alphabetically sort the list?
我的网站包含许多办公室先例的手册。 当用户使用HTML上载脚本时,它会将先例上载到服务器上的文件夹。 我使用以下脚本列出该特定文件夹中的所有文件。 p>
&lt;?php //将路径定义为relative
$ dir =“./OFFICE PRECEDENTS / Business&amp; Corporate Law“;
$ webpath =“”; //使用opendir函数
echo“&lt; ol&gt;”;
//打开一个已知目录,然后继续阅读其内容
if($ dh = opendir($ dir)){
while(($ file = readdir($ dh))!== false){\ n if(is_dir($ dir。'/'。$ file)){}
else if($ file =='。'){}
else if($ file =='..'){} \ 否则if($ file =='index.php'){}
else {
echo“&lt; li&gt;&lt; a href ='https://manual.malicki-law.ca/$dir/$ 文件'&GT; $文件&LT; / A&GT;&LT; /立GT;
“个;
}
}
closedir($ dh);
}
echo“&lt; / ol&gt;”;
?&gt;
code> pre>
如何实现系统按字母顺序对列表进行排序? p>
div>
For your specific case: If you use scandir()
instead of readdir()
, the default order is already in alphabetical order.
http://php.net/manual/en/function.scandir.php
The second parameter is:
sorting_order
By default, the sorted order is alphabetical in ascending order. If the optional sorting_order is set to SCANDIR_SORT_DESCENDING, then the sort order is alphabetical in descending order. If it is set to SCANDIR_SORT_NONE then the result is unsorted.
So instead of:
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
// your code
}
}
closedir($dh);
Just use something like:
if ($files = scandir($dir))
{
foreach ($files as $file) {
// your code
}
}
Save it into an array, but then instead of a numerical key, use an alpha-numeric one. Then you should be able to use a sort function on the keys and your values change to the correct order. Then just iterate over the array and echo them back out again.
For more info on sorting arrays: http://php.net/manual/en/array.sorting.php
I believe this should help you:
natcasesort: http://www.php.net/manual/en/function.natcasesort.php
usort: http://www.php.net/manual/en/function.usort.php with a comparator function that compares strtolower(a) and strtolower(b)
You need to create an array first.
Hope this helps.
<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
sort($my_array);
print_r($my_array);
?>
or You can use: asort, rsort, arsort
Many ways todo this, glob() is also good, then you dont have to worry about directory's.
<?php
$dir = "./OFFICE PRECEDENTS/Business & Corporate Law/";
$files = glob($dir."*.*");
sort($files);//Sort the array
echo "<ol>";
foreach($files as $file){
if($file == $dir.'index.php'){continue;}
$file = basename($file);
echo "\t<li><a href='https://manual.malicki-law.ca/$dir/$file'>$file</a></li>
";
}
echo "</ol>";
?>