1

ディレクトリ内のphpファイルに基づいてページナビゲーションを構築しています。

現在、私のディレクトリには 5 つのファイルがあります: index.php、2.php、3.php、4.php、5.php

私の限られた PHP スキルで、コードを操作してナビゲーションを次のように表示することができました: [2] [3] [4] [5] [index]

しかし、私は現時点で少し先の障害にぶつかりました。

[index]を[1]と表示させたいのですが、[1]が先に表示されるようにしたいです。

次のようにしたいと思います: [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>] ';
    }
?>

ヘルプやリードをいただければ幸いです。よろしくお願いいたします。

4

2 に答える 2

1
<?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;
    }
?>
于 2012-12-20T09:27:49.623 に答える
1

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]

于 2012-12-20T09:28:26.863 に答える