Php+mysql Infinite class instances without recursive implementations (non-recursive), MySQL recursive _php tutorial

Source: Internet
Author: User
Tags php foreach

Php+mysql Unlimited class instances without recursive implementations (non-recursive), MySQL recursion


Recursion is generally the first and easiest to implement for infinite classification, but recursion is generally considered a resource-intensive approach, so many systems do not consider using recursion
This article is also through the design of the database, with a sentence of SQL statement implementation
The database fields are probably as follows:

Copy the Code code as follows: ID number
FID parent category number
Class_name category Name
Path classification paths, with IDs as nodes, consisting of similar, 1,2,3,4, such strings

You can assume the following data:
Copy the Code code as follows:
ID FID class_name Path
1 0 Category 1, 1,
2 0 Category 2, 2,
3 1 Category 1-1, 1, 3,
4 1 Category 1-2, 1, 4,
5 2 category 2-1, 2, 5,
6 4 Categories 1-2-1, 1,4,6,

Unlimited class operation code:

<?php $sql = "SELECT * from Tree order by path";  $result = $nbs->query ($sql); while ($rows = $nbs->fetch_array ($result)) {if (Substr_count ($rows [' Path '], ', ') >2) {for ($i =0; $i < (substr_    Count ($rows [' Path '], ', ')-2); $i + +) echo '; } echo $rows [' class_name ']. '  
'; }?> $conn = mysql_connect (' localhost ', ' root ', ' root '), mysql_select_db (' wanggou123 ', $conn); mysql_query (' Set Names UTF8 '); $sql = "Select Id,concat (Catpath, '-', id) as Abspath,name from the category order by Abspath"; $query = mysql_query ($sql), while ($row =mysql_fetch_array ($query)) {/** * First presentation method *//* $space = Str_repeat (', C Ount (Explode ('-', $row [' Abspath ')])-1); Echo $space. $row [' name ']. *//** second method of presentation */$space = Str_repeat ('--', Count (Explode ('-'), $row [' Abspath ']))-1); $option. = ". $space. $row [' name ']. '
';} Echo $option, exit (), Echo ' ';

Where $nbs is the database operation class, this method is simple and clear!


PHP recursion problem, want to display the data in the style of infinite pole classification

First, the JSON is decoded into the array, using the Json_decode function to note that the second parameter must be added otherwise he will return an object. The next step is to recursion. This is one of the simplest recursion as long as it is traversed individually.
The following is the complete code:
$data = Json_decode ($str, true), $options = GetChildren ($data), function GetChildren ($parent, $deep =0) {foreach ($parent As $row) {$data [] = array ("id" + = $row [' id '], "name" = + $row [' name '], "pid" + $row [' parentid '], ' deep ' = $deep) if ($row [' Childs ']) {$data = Array_merge ($data, GetChildren ($row [' Childs '], $deep + 1)}} return $data;}? >!--? php foreach ($options as $row) {?--></ option>!--? php}?--></select> The code above has been tested by the following  php recursion problem (infinite pole classification) foreach itself is equivalent to judging Ah, when $ When the ARR array is non-empty, foreach traverses and recursively accesses the child nodes, but for the leaf nodes, the $arr array is empty and will not be foreach at all, and return directly. Got it?  http://www.bkjia.com/phpjc/840754.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/840754. Htmltecharticlephp+mysql without recursive implementation of infinite class instances (non-recursive), MySQL recursive to achieve infinite class classification, recursion is generally the first and most easy to think of, but recursion is generally considered to occupy ... 

Related Article

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.