Unlimited classification: queries data from databases and recursively sorts data in arrays.

Source: Internet
Author: User
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

  1. Class Tree {
  2. Protected $ arr = array (); // sort an array
  3. Protected $ info = array (); // stores error messages
  4. Protected $ tree = array (); // stores the generated class array
  5. Protected $ flag = true; // flag
  6. Public function _ construct ()
  7. {
  8. }
  9. // Obtain data
  10. Public function data ($ fid, $ arr)
  11. {
  12. $ This-> checkArr ($ arr );
  13. $ This-> checking ();
  14. $ This-> chindAll ($ fid );
  15. Return $ this-> tree;
  16. }
  17. // Determine whether the parameter meets the requirements
  18. Protected function checkArr ($ val)
  19. {
  20. Static $ num = 0;
  21. If (is_array ($ val )){
  22. Foreach ($ val as $ Varr ){
  23. $ This-> isKeyVal ('id', $ Varr, $ num );
  24. $ This-> isKeyVal ('parentid', $ Varr, $ num );
  25. $ This-> isKeyVal ('name', $ Varr, $ num );
  26. $ Num ++;
  27. }
  28. }
  29. $ This-> arr = $ val;
  30. }
  31. // Whether the saved id is an array
  32. Protected function checkNum ($ val)
  33. {
  34. If (! Is_numeric ($ val )){
  35. $ This-> info [] = 'input parameter '. $ val.' is not a numerical value ';
  36. $ This-> flag = false;
  37. $ This-> checking ();
  38. }
  39. }
  40. // Verify and output the error message
  41. Protected function checking (){
  42. If (! $ This-> flag ){
  43. Echo'
    ';
  44. Var_dump ($ this-> info );
  45. Exit ();
  46. }
  47. }
  48. // Determine whether the array key exists and whether it has a value
  49. Protected function isKeyVal ($ key, $ arr, $ num)
  50. {
  51. If (! Array_key_exists ($ key, $ arr ))
  52. {
  53. $ This-> info [] = $ num. 'Array key'. $ key. 'does not exist ';
  54. $ This-> flag = false;
  55. }
  56. }
  57. // Obtain the son
  58. Public function getChind ($ fid)
  59. {
  60. Static $ num = 0;
  61. $ Arr = array ();
  62. $ This-> checkNum ($ fid );
  63. Foreach ($ this-> arr as $ key => $ row ){
  64. If ($ row ['parentid'] = $ fid ){
  65. $ Arr [] = $ row;
  66. Unset ($ this-> arr [$ key]);
  67. }
  68. }
  69. If (! Empty ($ arr )){
  70. $ Num ++;
  71. Return $ this-> sortArr ($ arr );
  72. } Else {
  73. Return null;
  74. }
  75. }
  76. // Get my son and grandson
  77. Public function chindAll ($ fid, $ input = null)
  78. {
  79. Static $ n = 0;
  80. $ N ++;
  81. $ Arr = $ this-> getChind ($ fid );
  82. If (! Empty ($ arr )){
  83. $ Count = count ($ arr );
  84. If (empty ($ input )){
  85. For ($ I = 0; $ I <$ count; $ I ++ ){
  86. $ This-> tree [$ I] = $ arr [$ I];
  87. }

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.