Home > Backend Development > PHP Problem > How to implement breadcrumb navigation in php

How to implement breadcrumb navigation in php

王林
Release: 2023-03-12 21:52:02
Original
2204 people have browsed it

php method to implement breadcrumb navigation: [public function mbx($cat_id){$goods_info = D('goods')->find( I ('get.goods_id') );$row = D('cat')->f...].

How to implement breadcrumb navigation in php

The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.

The role of breadcrumb navigation is to tell visitors where they are on the website and how to return. Many friends may not know much about breadcrumb navigation. In fact, it comes from a fairy tale "Hansel and Gretel". When Hansel and Gretel were walking through the forest, they accidentally got lost, but they found that they were walking along the way. Breadcrumbs were scattered wherever they went to help them find their way home.

There are three types of breadcrumb navigation, namely:

1. Location-based breadcrumb navigation

This type is the most common. This type of breadcrumb navigation is a great way to point out the hierarchy of the current page and the entire site. This breadcrumb navigation can display links to the previous page or directory of the current page. This allows visitors to understand their location and find the page they want to reach more quickly. It can greatly improve the user-friendly experience.

2. Attribute-based breadcrumb navigation

This kind of breadcrumb navigation most often appears on e-commerce sites. This kind of breadcrumb navigation can be a good way to point out other attributes or categories of products on the current page. A product often has more than one attribute, and this breadcrumb navigation can give consumers a more intuitive understanding.

3. Path-based breadcrumb navigation

This kind of breadcrumb navigation is the least common. This type of breadcrumb navigation is very similar to the fairy tale type mentioned above. They can display links to web pages that visitors visited before arriving at the page. Breadcrumbs are not very popular because they basically function the same as forward and back buttons.

So how do we implement breadcrumb navigation? Let’s take a look at the specific implementation code!

The specific implementation code is as follows:

<?php 
/**
 * C层
 * GoodsController.class.php     面包屑导航
 * 获取上一层,上上层的名称
 */
public function mbx($cat_id){
    //获取当前cat_id的该条信息
    $row = D(&#39;cat&#39;)->find($cat_id);
    $tree[] = $row;//将该条数据放入数组中
    while($row[&#39;parent_id&#39;]>0){//只要该条数据的parent_id>0
        $row = D(&#39;cat_id&#39;)->fine($row[&#39;parent_id&#39;]);
        $tree[] = $row;
    }
    //array_reverse();返回翻转数据的数组
    return array_reverse($tree);//翻转数据
}

//修改对应的显示到模板中的方法
public function goods(){
    //获取该条数据的信息
    $goods_info = D(&#39;goods&#39;)->find( I (&#39;get.goods_id&#39;) );
    //打印该条信息的面包屑导航
    //var_dump($this->mbx($goods_info[&#39;cat_id&#39;]));
    $this->assign(&#39;mbx&#39; , $this->mbx($goods_info[&#39;cat_id&#39;]));
    $this->assign(&#39;goods&#39; , $goods_info);
    $this->play();
}

//显示到模板中
当前位置
<a href="">首页</a>
<foreach name=" mbx " item = " mb ">
<a href=" " >{}</a>
<code> > </code>
</foreach>
{$goods[&#39;goods_name&#39;]}
 ?>
}
Copy after login

Recommended learning: php training

The above is the detailed content of How to implement breadcrumb navigation in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template