Example of implementing breadcrumb navigation using php _ php instance

Source: Internet
Author: User
This article mainly shares examples of implementing the breadcrumb navigation in php. If you are interested, you can refer to this example to explain how to implement the breadcrb navigation in php. It is very practical in the project, write an implementation in this regard.


Path indicates all ancestor IDs, fullpath indicates all ancestor IDs and their own IDS

---- TABLE structure 'tp _ likecate' -- create table if not exists 'tp _ likecate' ('id' int (10) unsigned not null AUTO_INCREMENT, 'catename' varchar (24) not null, 'path' varchar (10) not null, 'fullpath' varchar (20) not null, primary key ('id ')) ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 9;

Data

---- Data in the table 'tp _ likecate' -- insert into 'tp _ likecate' ('id', 'catename', 'path', 'fullpath') VALUES (1, 'Cell phone ', '',', 1'), (2, 'function cell phone ', '1', '1, 2'), (3, 'old man cell phone ', '1, 2', '1, 2, 3 '), (4, 'Children's cell phone', '1, 2', '1, 2, 4 '), (5, 'Smartphone ', '1', '1, 5'), (6, 'android phone', '1, 5', '1,'), (7, 'ios phone', '1, 5', '1, '), (8, 'winphoto phone', '1, 5', '1 ');

Database connection:

<?php $db_host = 'localhost';$db_user = 'root';$db_password = '';$db_name = 'test';$con = mysql_connect($db_host, $db_user, $db_password) or die(mysql_error());mysql_select_db($db_name, $con) or die(mysql_error());mysql_query('set names utf8') or die(mysql_error());?>

Main function:

Function likecate ($ path = '') {// concat () connection field $ SQL =" select id, catename, path, concat (path, ',', id) as fullpath from tp_likecate order by fullpath asc "; $ res = mysql_query ($ SQL); $ result = array (); while ($ row = mysql_fetch_assoc ($ res )) {$ deep = count (explode (',', trim ($ row ['fullpath'], ','); // convert an explode string to an array implode array to a string $ row ['catename'] = @ str_repeat ('', $ deep ). '| --'. $ row ['catename']; $ result [] = $ row;} return $ result ;}

Output:

// Simple output $ res = likecate (); echo""; Foreach ($ res as $ key => $ val) {echo"{$ Val ['catename']}";} Echo""; Echo"
"; // Encapsulation method function getPathCate ($ cateid) {$ SQL =" select *, concat (path, ',', id) fullpath from tp_likecate where id = $ cateid "; $ res = mysql_query ($ SQL); $ row = mysql_fetch_assoc ($ res); $ ids = $ row ['fullpath']; $ SQL = "select * from tp_likecate where id in ($ ids) order by id asc"; $ res = mysql_query ($ SQL); $ result = array (); while ($ row = mysql_fetch_assoc ($ res) {$ result [] = $ row;} return $ result ;}/ /Added the function displayCatePath ($ cateid, $ link = 'cate. php? Cid = ') {// You can also assemble $ res = getPathCate ($ cateid); $ str = ''; foreach ($ res as $ k => $ v) {$ str. = "{$ v ['catename']}>" ;}return $ str;} echo displayCatePath (4 );

Effect:

The above are the detailed steps for php to implement the breadcrumb navigation. I hope it will help you to learn about php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.