PHP interview questions and answers for novices - fill-in-the-blank questions and programming questions

WBOY
Release: 2016-07-25 08:59:18
Original
1045 people have browsed it
  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 principle of unlimited classification in the forum. answer:

  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. (0, 'First-level category'),
  10. (1, 'Second-level category'),
  11. (1, 'Second-level category'),
  12. (1, 'Second-level category'),
  13. (2, 'Third-level category'),
  14. (2, '333332'),
  15. (2, '234234'),
  16. (3, 'aqqqqqd'),
  17. ( 4, 'Haha'),
  18. (5, '66333666′);

  19. */

  20. //Specify the category id variable $category_id, and then return the All subcategories of the category

  21. //$default_category is the default selected category
  22. function Get_Category($category_id = 0,$level = 0, $default_category = 0)
  23. {
  24. global $DB;
  25. $sql = “SELECT * FROM category ORDER BY categoryID DESC”;
  26. $result = $DB->query( $sql );
  27. while ($rows = $DB->fetch_array($result))
  28. {
  29. $category_array[$rows[categoryParentID] ][$rows[categoryID]] = array('id' => $rows[categoryID], 'parent' => $rows[categoryParentID], 'name' => $rows

  30. < p>[categoryName]);
  31. }
  32. if (!isset($category_array[$category_id]))
  33. {
  34. return “”;
  35. }
  36. foreach($category_array[$category_id] AS $key => $category)
  37. {
  38. if ($category['id'] == $default_category)
  39. {
  40. echo "
  41. }else
  42. {
  43. echo "< ;option value=”.$category['id'].””;
  44. }

  45. if ($level > 0)

  46. {
  47. echo “>” . str_repeat( ” “ , $level ) . ” ” . $category['name'] . “n”;
  48. }
  49. else
  50. {
  51. echo “>” . $category['name'] . “n”;
  52. }
  53. Get_Category( $key, $level + 1, $default_category);
  54. }
  55. unset($category_array[$category_id]);
  56. }

  57. /*

  58. The array format returned by the function is as follows:
  59. Array
  60. (
  61. [1] => Array ( [id] => 1 [name] => Level 1 category [level] => 0 [ParentID] => 0 )
  62. [4] => Array ( [id] => 4 [name] => Secondary category [level] => 1 [ParentID] => 1 )
  63. [9] => Array ( [id] => 9 [ name] => haha ​​[level] => 2 [ParentID] => 4 )
  64. [3] => Array ( [id] => 3 [name] => Secondary category [level] = > 1 [ParentID] => 1 )
  65. [8] => Array ( [id] => 8 [name] => aqqqqqd [level] => 2 [ParentID] => 3 )
  66. [2] => Array ( [id] => 2 [name] => Secondary category [level] => 1 [ParentID] => 1 )
  67. [7] => Array ( [id ] => 7 [name] => 234234 [level] => 2 [ParentID] => 2 )
  68. [6] => Array ( [id] => 6 [name] => 333332 [level] => 2 [ParentID] => 2 )
  69. [5] => Array ( [id] => 5 [name] => Third-level category [level] => 2 [ParentID] => 2 )
  70. [10] => Array ( [id] => 10 [name] => 66333666 [level] => 3 [ParentID] => 5 )
  71. )
  72. */
  73. / /Specify the category id, and then return the array
  74. function Category_array($category_id = 0,$level=0)
  75. {
  76. global $DB;
  77. $sql = “SELECT * FROM category ORDER BY categoryID DESC”;
  78. $result = $DB ->query($sql);
  79. while ($rows = $DB->fetch_array($result))
  80. {
  81. $category_array[$rows['categoryParentID']][$rows['categoryID']] = $rows;
  82. }

  83. foreach ($category_array AS $key=>$val)

  84. {
  85. if ($key == $category_id)
  86. {
  87. foreach ($val AS $k => $v)
  88. {
  89. $options[$k] =
  90. array(
  91. 'id' => $v['categoryID'], 'name' => $v['categoryName'], 'level ' => $level, 'ParentID'=>$v['categoryParentID']
  92. );

  93. $children = Category_array($k, $level+1);

  94. if (count($children) > 0)

  95. {
  96. $options = $options + $children;
  97. }
  98. }
  99. }
  100. }
  101. unset($category_array[$category_id]);
  102. return $options;
  103. }
  104. ?>

Copy code

  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’ => ’1111′),
  9. ’2′ => array(‘id’ => 2, ‘parent’ => 0, ‘name’ => ’2222′),
  10. ’4′ => array(‘id’ => 4, ‘parent’ => 0, ‘name’ => ’4444′)
  11. ),
  12. ’1′ => array(
  13. ’3′ => array(‘id’ => 3, ‘parent’ => 1, ‘name’ => ’333333′),
  14. ’5′ => array(‘id’ => 5, ‘parent’ => 1, ‘name’ => ’555555′)
  15. ),

  16. ’3′ => array(

  17. ’6′ => array(‘id’ => 6, ‘parent’ => 3, ‘name’ => ’66666′),
  18. ’7′ => array(‘id’ => 7, ‘parent’ => 3, ‘name’ => ’77777′)
  19. ),
  20. ’4′ => array(
  21. ’8′ => array(‘id’ => 8, ‘parent’ => 4, ‘name’ => ’8888′),
  22. ’9′ => array(‘id’ => 9, ‘parent’ => 4, ‘name’ => ’9999′)
  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 = “
  34. }else{
  35. $txt = “
  36. }

  37. if ($level > 0)

  38. {
  39. $txt1 = “>” . str_repeat( “-”, $level ) . ” ” . $cate['name'] . “n”;
  40. }else{
  41. $txt1 = “>” . $cate['name'] . “n”;
  42. }
  43. $val = $txt.$txt1;
  44. echo $val;
  45. self::Get_Category($key, $level + 1, $default_category);
  46. }
  47. }

  48. function getFlush($category_id = 0,$level = 0, $default_category = 0)

  49. {
  50. ob_start();
  51. self::Get_Category($category_id ,$level, $default_category);
  52. $out = ob_get_contents();
  53. ob_end_clean();
  54. return $out;
  55. }
  56. }
  57. $id =$_GET['id'];
  58. echo “”;
  59. $c = new cate();
  60. //$c->Get_Category();
  61. $ttt= $c->getFlush($id,’0′,’3′);
  62. echo $ttt;
  63. echo “”;
  64. ?>

复制代码


source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!