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

时间:2016-05-04 21:29:37

标签: php

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

中如下所示
example.com/sfm?dir=uploads/sfm/root

在rootdir中有一个名为folder1的文件夹 点击folder1时,网址为:

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

在folder1中有一个名为folder2的文件夹 点击该文件夹时,网址将变为:

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

依旧...... 如何根据网址制作一些带有锚点到文件夹的面包屑?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您要做的就是接受$_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 /
}