I’m working on a CMS, and the navigation is generated dynamically depending on what pages are in the "pages" directory. I then use scandir() to build the array of pages and a loop to build the navigation.
The problem is, I’d like to be able to change the order of the pages in the navigation by a user-defined #.
I.e., "Home – Contact us – About us" could be changed to "Home – About us – Contact us" depending on if the user set the "weight" of the "About us" page to a lower value.
<?php
$views_dir = $_SERVER['DOCUMENT_ROOT'] . "kloudcms/" . VIEWS_LOCATION;
$views = scandir($views_dir, 0);
unset($views[0], $views[1]);
echo "<ul>";
foreach ($views as $view) {
$page_name = substr($view, 0, count($view) - 5);
echo "<li>" . ucwords($page_name) . "</li>";
}
echo "</ul>";
?>
If you are using a CMS, then I think it’s better to have a section for generating Static Pages with option to add weight for each page, save it in database, and query the pages sorting it according to weights assigned to it.
As per your current situation, since you are using scandir, you can only sorted alphabetically:
Reference: Sorting Scandir listing