PHP to achieve breadcrumbs example sharing _php examples

Source: Internet
Author: User
Tags learn php

This example explains the PHP implementation of breadcrumbs, breadcrumbs navigation in the project is very practical, here to write a realization of this.


Path indicates that all ancestor Id,fullpath represent all ancestor IDs and their own IDs

-
----table structure ' tp_likecate '-
-

CREATE table IF not EXISTS ' tp_likecate ' (
 ' id ' int (a) unsigned not NULL AUTO _increment,
 ' catename ' varchar NOT NULL,
 ' path ' varchar is not NULL, ' fullpath ' varchar ' is not
 null,< C8/>primary KEY (' id ')
) Engine=innodb DEFAULT Charset=utf8 auto_increment=9;

Data

---The
data in the Dump table ' tp_likecate '-
-

INSERT into ' tp_likecate ' (' id ', ' catename ', ' path ', ' FullPath ') values< c15/> (1, ' Mobile ', ' ', ', 1 '),
(2, ' functional cell phone ', ' 1 ', ' 1,2 '),
(3, ' old person 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,5,6 '),
(7, ' iOS phone ', ' 1,5 ', ' 1,5,7 '),
(8, ' Winphoto mobile phone ', ' 1,5 ', ' 1,5,8 ');

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 to FullPath ASC ";
  $res = mysql_query ($sql);
  $result = Array ();
  while ($row =mysql_fetch_assoc ($res)) {
    $deep = count (', ', Trim ($row [' FullPath '], ', '));// The explode string converts an array of implode arrays to strings
    $row [' catename '] = @str_repeat (', $deep). ' | -'. $row [' Catename '];
    $result [] = $row;
  }
  return $result;
}

Output:

Simple output
$res = Likecate ();

echo "<select name= ' cate ' >";
foreach ($res as $key => $val) {
  echo "<option>{$val [' Catename ']}</option>;
}
echo "</select>";
echo "<br/>";

Encapsulate 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;
}

The linked parameter
function Displaycatepath ($cateid, $link = ' cate.php?cid= ') {//can also be assembled
  $res = getpathcate ($cateid);
  $str = ';
  foreach ($res as $k => $v) {
    $str. = "<a href= ' {$link} {$v [' id ']} ' >{$v [' Catename ']}</a> >];
  } return
  $str;
}
Echo Displaycatepath (4);

Effect:

The above is the PHP to achieve the detailed steps of breadcrumbs, hope to learn PHP program design help.

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.