PHP Simple implementation of infinite-level classification method _php skills

Source: Internet
Author: User

This article describes the simple implementation of PHP to achieve the infinite level of classification methods. Share to everyone for your reference, specific as follows:

Database structure:

CREATE TABLE IF not EXISTS ' city ' (
 ' id ' int (one) not NULL auto_increment,
 ' name ' varchar () Character Set UTF8 Co Llate utf8_unicode_ci NOT null default ' 0 ',
 ' parentid ' int (one) not null default ' 0 '
 PRIMARY KEY (' id ')
engin E=myisam DEFAULT charset=latin1 auto_increment=7;

PHP File:

$DB =new db ($Config [' Host '], $Config [' User '], $Config [' Password '], $Config [' Port '], $Config [' db '], $Config [' CharSet '] );
function findcity ($table, $id =0, $level =1) {
  global $db;
  $FINDSQL = "Select Id,name,parentid from $table where parentid={$id} ORDER by id";
  $findResult = $db->getarray ($FINDSQL);
  $num = $db->numrows;
  $LOGOSTR = "|";
  for ($i =0; $i < $level; $i + +) {
  $logoStr. = "--";
  }
   if ($num!=0) {for
   ($j =0; $j < $num $j + +) {
     echo "<option value={$findResult [$j] [' id ']}>{$logoStr}{$ findresult[$j][name]}</option> ";
        Findcity ($table, $findResult [$j] [' ID '], $level + 1);
    }}
Findcity (city);

More about PHP Interested readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Mathematical Calculation Skills Summary, "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program 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.