Let’s introduce to you some usage of unlimited classification in PHP, including direct unlimited classification database and array operations. Friends in need can refer to it.
Let’s first look at the infinite classification of php and mysql databases
Create database:
id, fid, fname (both id and fid must be numeric types and the default value of fid must be set to 0;
The code is as follows | Copy code | ||||||||||||||||||||||||||||||||
$db=@mysql_connect("localhost","root","micronsky.net") or die("Database connection lost");
"; echo subfl($myrow["id"]," "); //Call the sub-level classification function here } while ($myrow=mysql_fetch_array($result)); } } //Define sub-categories function subfl($fid,$strdis) { global $db; $result1=mysql_query("select id,fid,fname from wxjfl where fid=$fid order by id desc",$db); if ($myrow1=mysql_fetch_array($result1)) { do { //echo $strdis; echo $strdis.$myrow1["fname"]; echo " "; Subfl($myrow1["id"]," ".$strdis); //Be sure to note here that there is no need to echo like the function above...just call the sub-level classification function directly and pay the value! At the same time, this is also the recursive part } while ($myrow1=mysql_fetch_array($result1)); } } echo mainfl(); $ltime=microtime(); echo " "; echo number_format($ltime-$stime,4); //Statistical execution time, this is much faster than ASP, but this has little to do with the writing method, mainly because PHP itself has added an accelerator! ?> Now we directly use arrays for paging
Example:
The above two methods are to flatten all nodes into an array by ID, then find their parent or children, and attach the flat elements to parent and children by reference, But the referenced elements still exist in the flat array. Therefore, in practical applications, it is best to mark those referenced elements to avoid starting the traversal with them as the root, resulting in duplication.
Recursive method, after the PHP array element is deleted, the array cursor will return to zero, so some elements that have found their "home" during the traversal process have to stay in the array, and the search range for subsequent elements cannot be reduced:
$mylist = array(array( 'parent_id'=>0,'id'=>1), array('parent_id'=>0,'id'=>2), array('parent_id'=>0,'id'=>3), array('parent_id'=>2,'id'=>4), array('parent_id'=>2,'id'=>5), array('parent_id'=>3,'id'=>6), array('parent_id'=>3,'id'=>7), array('parent_id'=>4,'id'=>8), array('parent_id'=>5,'id'=>9), array('parent_id'=>5,'id'=>10) ); http://www.bkjia.com/PHPjc/631571.html www.bkjia.com truehttp: //www.bkjia.com/PHPjc/631571.htmlTechArticle Let’s introduce to you some usage of unlimited classification in php, including direct unlimited classification database For operations with arrays, friends in need can refer to it. Let’s take a look first...
Related labels:
source:php.cn
Previous article:PHP city-linked classification sorting program_PHP tutorial
Next article:Registration and automatic loading of php classes__autoload_PHP tutorial
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|