如何在php中使用锚形式url创建面包屑


how to create breadcrumbs with anchor form url in php

对于文件管理系统,我想从url在php中创建一些面包屑。每个用户的根目录在url中如下所示:

example.com/sfm?dir=uploads/sfm/root

在rootdir中有一个名为folder1的文件夹单击文件夹1时,url为:

example.com/sfm?dir=uploads/sfm/root/folder1

文件夹1中有一个名为folder2的文件夹当点击该文件夹时,url变为:

example.com/sfm?dir=uploads/sfm/root/folder1/folder2

等等。。。我如何制作一些面包屑,并根据url锚定到文件夹?

如果我理解正确,您想做的是接受$_GET['dir']的争用,并将其按/进行拆分,然后提供指向每个争用的链接。

以下是我的做法:

$crumbs=explode('/',$_GET['dir']); // this splits the sections of $_GET['dir'] separated by / into an array of values
$url_pre=''; // we'll use this to keep track of the crumbs we've sifted through already
// foreach cycles through each element in an array
// $crumbs is the array, and $crumb is the current listing in the array we're looking at
foreach($crumbs as $crumb){
    $url_pre .= $crumb;
    echo '<a href="?dir='.$url_pre.'">'.$crumb.'</a>';
    $url_pre .= '/'; // add this after you echo the link, so that dir doesn't start with a /
}