Unlimited classification: queries data from databases and recursively sorts data in arrays.
Unlimited classification: query data from the database and perform custom sorting recursively in the array. it also realizes that there are many deficiencies and the processing is not good. There may be better ways to talk about more than 100 rows. Incomplete
- Class Tree {
- Protected $ arr = array (); // sort an array
- Protected $ info = array (); // stores error messages
- Protected $ tree = array (); // stores the generated class array
- Protected $ flag = true; // flag
- Public function _ construct ()
- {
- }
- // Obtain data
- Public function data ($ fid, $ arr)
- {
- $ This-> checkArr ($ arr );
- $ This-> checking ();
- $ This-> chindAll ($ fid );
- Return $ this-> tree;
- }
- // Determine whether the parameter meets the requirements
- Protected function checkArr ($ val)
- {
- Static $ num = 0;
- If (is_array ($ val )){
- Foreach ($ val as $ Varr ){
- $ This-> isKeyVal ('id', $ Varr, $ num );
- $ This-> isKeyVal ('parentid', $ Varr, $ num );
- $ This-> isKeyVal ('name', $ Varr, $ num );
- $ Num ++;
- }
- }
- $ This-> arr = $ val;
- }
- // Whether the saved id is an array
- Protected function checkNum ($ val)
- {
- If (! Is_numeric ($ val )){
- $ This-> info [] = 'input parameter '. $ val.' is not a numerical value ';
- $ This-> flag = false;
- $ This-> checking ();
- }
- }
- // Verify and output the error message
- Protected function checking (){
- If (! $ This-> flag ){
- Echo'
'; - Var_dump ($ this-> info );
- Exit ();
- }
- }
- // Determine whether the array key exists and whether it has a value
- Protected function isKeyVal ($ key, $ arr, $ num)
- {
- If (! Array_key_exists ($ key, $ arr ))
- {
- $ This-> info [] = $ num. 'Array key'. $ key. 'does not exist ';
- $ This-> flag = false;
- }
- }
- // Obtain the son
- Public function getChind ($ fid)
- {
- Static $ num = 0;
- $ Arr = array ();
- $ This-> checkNum ($ fid );
- Foreach ($ this-> arr as $ key => $ row ){
- If ($ row ['parentid'] = $ fid ){
- $ Arr [] = $ row;
- Unset ($ this-> arr [$ key]);
- }
- }
- If (! Empty ($ arr )){
- $ Num ++;
- Return $ this-> sortArr ($ arr );
- } Else {
- Return null;
- }
- }
- // Get my son and grandson
- Public function chindAll ($ fid, $ input = null)
- {
- Static $ n = 0;
- $ N ++;
- $ Arr = $ this-> getChind ($ fid );
- If (! Empty ($ arr )){
- $ Count = count ($ arr );
- If (empty ($ input )){
- For ($ I = 0; $ I <$ count; $ I ++ ){
- $ This-> tree [$ I] = $ arr [$ I];
- }
|