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.