search
HomeBackend DevelopmentPHP TutorialPHP mcDropdown implements a method in which the file path can be selected in the drop-down box

  1. //Get the file list in the specified directory
  2. //$path specifies the directory, the default is the current directory
  3. //$ifchild Whether to display the subdirectory file list, not displayed by default
  4. // $curpath displays the current path, which defaults to starting from the current directory; this is mainly to display and determine the href path
  5. function openpath($path=".",$ifchild=false,$curpath=".")
  6. {
  7. $handle = opendir($path);
  8. if($handle)
  9. {
  10. while(false !== ($file = readdir($handle)))
  11. {
  12. if ($file != "." && $file != "..")
  13. {
  14. $fullPath = $path.DIRECTORY_SEPARATOR.$file;
  15. if(is_dir($fullPath))//If it is a directory file
  16. {
  17. if($ifchild)//If the display subdirectory is set
  18. {
  19. //Recursion
  20. openpath($path.DIRECTORY_SEPARATOR.$file,$ifchild,$curpath.DIRECTORY_SEPARATOR.$file);
  21. }
  22. else
  23. {
  24. echo "
  25. $file
  26. n";
  27. }
  28. }
  29. else if($file != basename(__FILE__))//Exclude the currently executing script
  30. {
  31. echo "
  32. $file
  33. n";
  34. }
  35. else
  36. {
  37. echo $file ;
  38. }
  39. }
  40. }
  41. }
  42. closedir($handle);
  43. }
Copy code

Because we want to provide the path selection function, it would be great if there is a drop-down menu with the path to be selected displayed. .

2. Get the code of all sub-file paths under the current file:

  1. /*Get the file path list in the specified directory
  2. *$path specifies the directory, the default is the current directory
  3. *$ifchild whether to get the subdirectory file list, not obtained by default
  4. *$curpath display The current path starts from the current directory by default
  5. *&$pach_html_srt Pass a reference to an external variable, because this method may be called recursively, so save it in this way
  6. * Some information can also be used as a global variable Implementation, changes to variables inside the function also affect the outside.
  7. *&$path_ref_count The principle is the same as above, a counting flag. If it is recursive, the counter will increase from the last saved value
  8. */
  9. function openpath($path=".",$ifchild=false,&$path_html_str,&$ path_ref_count)
  10. {
  11. $handle = opendir($path);
  12. if($handle)
  13. {
  14. while(false !== ($file = readdir($handle)))
  15. {
  16. if ($file != " ." && $file != "..")
  17. {
  18. $fullPath = $path.DIRECTORY_SEPARATOR.$file;
  19. if(is_dir($fullPath))//If the file is a directory
  20. {
  21. $path_html_str.='< ;li rel="'.$path_ref_count++.'">';
  22. $path_html_str.=$file.'
      ';
    • if($ifchild)
    • {
    • //Recursive
    • openpath($path.DIRECTORY_SEPARATOR .$file,$ifchild,&$path_html_str,&$path_ref_count);
    • }
    • $path_html_str.='
  23. ';
  24. }
  25. }
  26. }
  27. }
  28. closedir($handle) ;
  29. }
Copy code

With the above method, you can use the jquery mcDropdown plug-in at the front desk to allow users to select the directory they want to enter through the drop-down menu, so it needs to be encapsulated into a specified format:

  1. $path_ref_count = 1;
  2. $path_html_str ='';
  3. openpath(".",true,&$path_html_str,&$path_ref_count);
  4. $path_html_str = '
      '.$path_html_str.'
    ';
  5. $path_html_str = str_replace ( "
      ", '', $path_html_str );
    • ?>
    Copy code

    In this way, $path_html_str is passed to the front desk, and it is displayed as an unordered list that meets the requirements of mcDropdown, and the corresponding candidate list can be displayed.

    The complete code is as follows: 1.test.html

    1. jquery mcDropdown implementation The file path can be selected in the drop-down box_bbs.it-home.org
    2. Please select a category:
    3. #categorymenu#
    Copy code

    2, test.php

    1. //Directory information processing
    2. $path_ref_count = 1;
    3. $path_html_str ='';
    4. openpath(".",true,&$path_html_str,&$path_ref_count);
    5. $path_html_str = '
        '.$path_html_str.'
      ';
    6. $path_html_str = str_replace ( "
        ", '' , $path_html_str );
      • //var_dump($path_info);
      • //var_dump($path_html_str);
      • $str_buffer = file_get_contents (dirname(__FILE__).DIRECTORY_SEPARATOR.'test.html');
      • $str_buffer = str_replace ( "#categorymenu#", $path_html_str, $str_buffer );
      • $str_buffer = str_replace ( "#delim#", DIRECTORY_SEPARATOR, $str_buffer );
      • echo $str_buffer;
      • /*Get the list of file paths in the specified directory
      • *$ The directory specified by path, defaults to the current directory
      • *$ifchild Whether to obtain the subdirectory file list, not obtained by default
      • *$curpath Displays the current path, the default is to start from the current directory
      • *&$pach_html_srt Pass a reference to an external variable , because this method may be called recursively, so saving
      • * some information in this way can also be implemented using global variables. Variable changes inside the function also affect the outside.
      • *&$path_ref_count The principle is the same as above, a counting flag. If it is recursive, the counter will increase from the last saved value
      • */
      • function openpath($path=".",$ifchild=false,&$path_html_str,&$ path_ref_count)
      • {
      • $handle = opendir($path);
      • if($handle)
      • {
      • while(false !== ($file = readdir($handle)))
      • {
      • if ($file != " ." && $file != "..")
      • {
      • $fullPath = $path.DIRECTORY_SEPARATOR.$file;
      • if(is_dir($fullPath))//If the file is a directory
      • {
      • $path_html_str.='< ;li rel="'.$path_ref_count++.'">';
      • $path_html_str.=$file.'
          ';
        • if($ifchild)
        • {
        • //Recursive
        • openpath($path.DIRECTORY_SEPARATOR .$file,$ifchild,&$path_html_str,&$path_ref_count);
        • }
        • $path_html_str.='
      • ';
      • }
      • }
      • }
      • }
      • closedir($handle) ;
      • }
      • ?>
      Copy code

      jquery mcDropdown plug-in download address: http://www.givainc.com/labs/mcdropdown_jquery_plugin.htm.



      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
      The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

      What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

      PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

      PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

      PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

      PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

      PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

      PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

      Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

      PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

      PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

      PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

      PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

      PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

      How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

      PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

      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

      AI Hentai Generator

      AI Hentai Generator

      Generate AI Hentai for free.

      Hot Tools

      ZendStudio 13.5.1 Mac

      ZendStudio 13.5.1 Mac

      Powerful PHP integrated development environment

      Notepad++7.3.1

      Notepad++7.3.1

      Easy-to-use and free code editor

      mPDF

      mPDF

      mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

      EditPlus Chinese cracked version

      EditPlus Chinese cracked version

      Small size, syntax highlighting, does not support code prompt function

      Dreamweaver CS6

      Dreamweaver CS6

      Visual web development tools