PHP implementation of infinite-pole classification and graphics tutorial, _php tutorial

Source: Internet
Author: User

PHP implementation of infinite-pole classification and graphics tutorial,


In general, the realization of infinite pole classification is the use of recursion or iterative approach, the small partners to see the implementation of this article.

1, Database design:

2, Code:

Copy CodeThe code is as follows:/**
* @author Koma
* @todo php Infinite Pole classification
*/$CN = mysql_connect (' localhost ', ' root ', ') or Die (Mysql_error ());
mysql_select_db (' t ', $CN) or Die (Mysql_error ());
mysql_query (' Set names UTF8 ');
/**
* Step down from the top level to get the sub-class
* @param number $pid
* @param array $lists
* @param number $deep
* @return Array
*/function getlists ($pid = 0, & $lists = Array (), $deep = 1) {
$sql = ' SELECT * from category WHERE pid= '. $pid;
$res = mysql_query ($sql);
while (($row = Mysql_fetch_assoc ($res))!== FALSE) {
$row [' catename '] = Str_repeat (", $deep). ' | ---'. $row [' Catename '];
$lists [] = $row;
GetLists ($row [' id '], $lists, + + $deep); Depth +1-$deep before entering subclasses; Depth-1} after exit from subclass
return $lists;
}
function displaylists ($pid = 0, $selectid = 1) {
$result = GetLists ($pid);
$str = ' '; foreach ($result as $item) {$selected = ""; if ($selectid = = $item [' id ']) {$selected = ' selected '; } $str. = ''. $item [' Catename ']. '; } return $str. = '';
} /**
* Start from subclass to get its parent class
* @param number $cid
* @param array $category
* @return Array:
*/function GetCategory ($cid, & $category = Array ()) {
$sql = ' SELECT * from category WHERE id= '. $cid. ' LIMIT 1 ';
$result = mysql_query ($sql);
$row = Mysql_fetch_assoc ($result);
if ($row) {
$category [] = $row;
GetCategory ($row [' pid '], $category);
}
Krsort ($category); Reverse, to achieve the effect from the parent class to the subclass return $category;
}
function DisplayCategory ($cid) {
$result = GetCategory ($cid);
$str = "";
foreach ($result as $item) {
$str. = '. $item [' Catename ']. ' > ';
}
Return substr ($str, 0, strlen ($str)-1);
}
Echo displaylists (0, 3);
Echo displaycategory (13);

3,:

is not very simple, small partners can directly take to use Ha, do not charge royalties ^_^

http://www.bkjia.com/PHPjc/917041.html www.bkjia.com true http://www.bkjia.com/PHPjc/917041.html techarticle PHP implementation of infinite Pole classification and graphics tutorial, in general, the realization of infinite pole classification is the use of recursion or iterative way, small partners look at the implementation of this article. 1, database design: ...

  • 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.