search
HomeBackend DevelopmentPHP TutorialPHP interview questions and answers for novices - fill-in-the-blank 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 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. [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. {
  42. echo "< ;option value=”.$category['id'].””;
  43. }
  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] => Secondary category [level] => 1 [ParentID] => 1 )
  62. [9] => Array ( [id] => 9 [ name] => haha ​​[level] => 2 [ParentID] => 4 )
  63. [3] => Array ( [id] => 3 [name] => Secondary category [level] = > 1 [ParentID] => 1 )
  64. [8] => Array ( [id] => 8 [name] => aqqqqqd [level] => 2 [ParentID] => 3 )
  65. [2] => Array ( [id] => 2 [name] => Secondary 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] => Third-level category [level] => 2 [ParentID] => 2 )
  69. [10] => Array ( [id] => 10 [name] => 66333666 [level] => 3 [ParentID] => 5 )
  70. )
  71. */
  72. / /Specify the category id, and then 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); p>

  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. ?>
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. $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. ?>
复制代码


Statement
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
What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

How do you optimize PHP applications for performance?How do you optimize PHP applications for performance?May 08, 2025 am 12:08 AM

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

What is dependency injection in PHP?What is dependency injection in PHP?May 07, 2025 pm 03:09 PM

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

Best PHP Performance Optimization TechniquesBest PHP Performance Optimization TechniquesMay 07, 2025 pm 03:05 PM

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

PHP Performance Optimization: Using Opcode CachingPHP Performance Optimization: Using Opcode CachingMay 07, 2025 pm 02:49 PM

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.