Reputation: 79
I am building a page navigation based on php files in a directory.
At the moment I have 5 files in my directory: index.php, 2.php, 3.php, 4.php, 5.php
With my limited PHP skills I have managed to work the code to display the navigation as: [2] [3] [4] [5] [index]
However I have hit a road block that is a little beyond me at this time.
I would like to have [index] displayed as [1], and I would like [1] to be displayed first.
I would like to have it look like this: [1] [2] [3] [4] [5]
<?php
$pathfiles = "../directory/";
$files = glob("../directory/*.php");
foreach( $files as $file ) {
echo '[<a href="'.($pathfiles).''
.basename($file).'">'
.basename($file, ".php").'</a>] ';
}
?>
Any help or leads would be appreciated. Thank you in advance for your time.
Upvotes: 1
Views: 287
Reputation: 3806
You'll need to push the index.php value to the top before iterating;
$key = array_search('index.php', $files);
unset($files[$key]);
natsort($files);
array_unshift($files, 'index.php');
Then, in your iteration, just look for the value index.php and change it to [1]
Upvotes: 1
Reputation: 39
<?php
$pathfiles = "../directory/";
$files = glob("../directory/*.php");
$list = array();
foreach($files as $file)
{
$nb = basename($file) == "index" ? "1" : basename($file);
$list[$nb] = '[<a href="'.($pathfiles).''.basename($file).'">'.$nb.'</a>] ';
}
ksort($list);
foreach ($list as $l)
{
echo $l;
}
?>
Upvotes: 1