Php interview questions and answers for beginners-fill in questions and programming questions

Source: Internet
Author: User
Php interview questions and answers for beginners-fill in questions and programming questions

  1. Function my_scandir ($ dir)
  2. {
  3. $ Files = array ();
  4. If ($ handle = opendir ($ dir )){
  5. While ($ file = readdir ($ handle ))! = False ){
  6. If ($ file! = "..." & $ File! = ".") {
  7. If (is_dir ($ dir. "/". $ file )){
  8. $ Files [$ file] = scandir ($ dir. "/". $ file );
  9. } Else {
  10. $ Files [] = $ file;
  11. }
  12. }
  13. }
  14. Closedir ($ handle );
  15. Return $ files;
  16. }
  17. }

14. briefly describe the implementation principles of unlimited classification in the forum. A:

  1. /*

  2. The data table structure is as follows:
  3. Create table 'Category '(
  4. 'Categoryid' smallint (5) unsigned not null auto_increment,
  5. 'Categoryparentid' smallint (5) unsigned not null default '0 ′,
  6. 'Categoryname' varchar (50) not null default ",
  7. Primary key ('categoryid ')
  8. ) ENGINE = MyISAM default charset = gbk;

  9. Insert into 'category' ('categoryparentid', 'categoryname') VALUES

  10. (0, 'level 1 category '),
  11. (1, 'level 2 category '),
  12. (1, 'level 2 category '),
  13. (1, 'level 2 category '),
  14. (2, 'third-level category '),
  15. (2, '000000 ′),
  16. (2, '000000 ′),
  17. (3, 'aqqqqqd '),
  18. (4, 'Haha '),
  19. (5, '000000 ′);

  20. */

  21. // Specify the category id variable $ category_id and return all subclasses of the category

  22. // $ Default_category is the default selected category.
  23. Function Get_Category ($ category_id = 0, $ level = 0, $ default_category = 0)
  24. {
  25. Global $ DB;
  26. $ SQL = "SELECT * FROM category ORDER BY categoryID DESC ";
  27. $ Result = $ DB-> query ($ SQL );
  28. While ($ rows = $ DB-> fetch_array ($ result ))
  29. {
  30. $ Category_array [$ rows [categoryParentID] [$ rows [categoryID] = array ('id' => $ rows [categoryID], 'parent' => $ rows [categoryParentID], 'name' => $ rows

  31. [CategoryName]);

  32. }
  33. If (! Isset ($ category_array [$ category_id])
  34. {
  35. Return "";
  36. }
  37. Foreach ($ category_array [$ category_id] AS $ key => $ category)
  38. {
  39. If ($ category ['id'] = $ default_category)
  40. {
  41. Echo"} Else
  42. {
  43. Echo"}

  44. If ($ level> 0)

  45. {
  46. Echo ">". str_repeat ("", $ level). "". $ category ['name']. "\ n ";
  47. }
  48. Else
  49. {
  50. Echo ">". $ category ['name']. "\ n ";
  51. }
  52. Get_Category ($ key, $ level + 1, $ default_category );
  53. }
  54. Unset ($ category_array [$ category_id]);
  55. }

  56. /*

  57. The array format returned by the function is as follows:
  58. Array
  59. (
  60. [1] => Array ([id] => 1 [name] => level 1 category [level] => 0 [ParentID] => 0)
  61. [4] => Array ([id] => 4 [name] => level 2 category [level] => 1 [ParentID] => 1)
  62. [9] => Array ([id] => 9 [name] => haha [level] => 2 [ParentID] => 4)
  63. [3] => Array ([id] => 3 [name] => level 2 category [level] => 1 [ParentID] => 1)
  64. [8] => Array ([id] => 8 [name] => aqqqqqd [level] => 2 [ParentID] => 3)
  65. [2] => Array ([id] => 2 [name] => level 2 category [level] => 1 [ParentID] => 1)
  66. [7] => Array ([id] => 7 [name] => 234234 [level] => 2 [ParentID] => 2)
  67. [6] => Array ([id] => 6 [name] => 333332 [level] => 2 [ParentID] => 2)
  68. [5] => Array ([id] => 5 [name] => level 3 category [level] => 2 [ParentID] => 2)
  69. [10] => Array ([id] => 10 [name] => 66333666 [level] => 3 [ParentID] => 5)
  70. )
  71. */
  72. // Specify the Category id and return the array
  73. Function Category_array ($ category_id = 0, $ level = 0)
  74. {
  75. Global $ DB;
  76. $ SQL = "SELECT * FROM category ORDER BY categoryID DESC ";
  77. $ Result = $ DB-> query ($ SQL );
  78. While ($ rows = $ DB-> fetch_array ($ result ))
  79. {
  80. $ Category_array [$ rows ['categoryparentid'] [$ rows ['categoryid'] = $ rows;
  81. }

  82. Foreach ($ category_array AS $ key => $ val)

  83. {
  84. If ($ key = $ category_id)
  85. {
  86. Foreach ($ val AS $ k => $ v)
  87. {
  88. $ Options [$ k] =
  89. Array (
  90. 'Id' => $ v ['categoryid'], 'name' => $ v ['categoryname'], 'level' => $ level, 'parentid' => $ v ['categoryparentid']
  91. );

  92. $ Children = Category_array ($ k, $ level + 1 );

  93. If (count ($ children)> 0)

  94. {
  95. $ Options = $ options + $ children;
  96. }
  97. }
  98. }
  99. }
  100. Unset ($ category_array [$ category_id]);
  101. Return $ options;
  102. }
  103. ?>

  1. Class cate

  2. {

  3. Function Get_Category ($ category_id = 0, $ level = 0, $ default_category = 0)

  4. {
  5. Echo $ category_id;
  6. $ Arr = array (
  7. '0' => array (
  8. '1' => array ('id' => 1, 'parent' => 0, 'name' => '123 ′),
  9. '2' => array ('id' => 2, 'parent' => 0, 'name' => '123 ′),
  10. '4' => array ('id' => 4, 'parent' => 0, 'name' => '123 ′)
  11. ),
  12. '1' => array (
  13. '3' => array ('id' => 3, 'parent' => 1, 'name' => '123 ′),
  14. '5' => array ('id' => 5, 'parent' => 1, 'name' => '123 ′)
  15. ),

  16. '3' => array (

  17. '6' => array ('id' => 6, 'parent' => 3, 'name' => '123 ′),
  18. '7' => array ('id' => 7, 'parent' => 3, 'name' => '123 ′)
  19. ),
  20. '4' => array (
  21. '8' => array ('id' => 8, 'parent' => 4, 'name' => '123 ′),
  22. '9' => array ('id' => 9, 'parent' => 4, 'name' => '123 ′)
  23. )
  24. );

  25. If (! Isset ($ arr [$ category_id])

  26. {
  27. Return "";
  28. }

  29. Foreach ($ arr [$ category_id] AS $ key => $ cate)

  30. {
  31. If ($ cate ['id'] = $ default_category)
  32. {
  33. $ Txt ="} Else {
  34. $ Txt ="}

  35. If ($ level> 0)

  36. {
  37. $ Txt1 = ">". str_repeat ("-", $ level). "". $ cate ['name']. "\ n ";
  38. } Else {
  39. $ Txt1 = ">". $ cate ['name']. "\ n ";
  40. }
  41. $ Val = $ txt. $ txt1;
  42. Echo $ val;
  43. Self: Get_Category ($ key, $ level + 1, $ default_category );
  44. }
  45. }

  46. Function getFlush ($ category_id = 0, $ level = 0, $ default_category = 0)

  47. {
  48. Ob_start ();
  49. Self: Get_Category ($ category_id, $ level, $ default_category );
  50. $ Out = ob_get_contents ();
  51. Ob_end_clean ();
  52. Return $ out;
  53. }
  54. }
  55. $ Id = $ _ GET ['id'];
  56. Echo "";
  57. $ C = new cate ();
  58. // $ C-> Get_Category ();
  59. $ Ttt = $ c-> getFlush ($ id, '0', '3 ′);
  60. Echo $ ttt;
  61. Echo "";
  62. ?>

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.