首頁 > 後端開發 > php教程 > 新手的php 面試題目及答案-填空題與程式設計題

新手的php 面試題目及答案-填空題與程式設計題

WBOY
發布: 2016-07-25 08:59:18
原創
1087 人瀏覽過
  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.簡述論壇中無限分類的實現原理。 答:

  1. /*

  2. 資料表結構如下:
  3. ATEATE TABLE `category` (
  4. `categoryID` smallint(5) unsigned NOT NULL auto_increment,
  5. `categoryParentID` smallint(5) unsigned NOT NULL default '0′,
  6. `categoryName` varchar(50) Ndefault '0′,
  7. `categoryName` varchar(50) Ndefault ' PRIMARY KEY (`categoryID`)
  8. ) ENGINE=MyISAM DEFAULT CHARSET=gbk;
  9. INSERT INTO `category` ( `categoryParentID`, `categoryName`) VALUES(1, '二級類別'),

  10. (1, '二級類別'),
  11. (1, '二級類別'),
  12. ( 2, '三級類'),
  13. (2, '333332′),
  14. (2, '234234′),
  15. (3, 'aqqqqqd'),
  16. (4, '哈哈'),
  17. (5, '66333666′);
  18. */

  19. //指定分類id變數$category_id,然後傳回該分類的所有子類別

  20. //$default_category為預設的選取的分類
  21. function Get_Category($category_id = 0,$level = 0, $default_category = 0)
  22. {
  23. global $DB
  24. $sql = “SELECT * FROM category ORDER BY categoryID DESC”;
  25. $result = $DB->query( $sql );
  26. while ($rows = $DB->fetch_array($result))
  27. {
  28. $category_array[$rows[categoryParentID]][$rows[categoryID]] = array('id' => $rows[categoryID], 'parent' => $rows[categoryParentID], 'name' => $rows
  29. [categoryName]);

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

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

  56. 函數回傳的陣列格式如下所示:
  57. Array
  58. (
  59. [1] => Array ( [id] => 1 [name] => 一級類別[level] => 0 [ParentID] => 0 )
  60. [4] => Array ( [id] => 4 [name] => 二級類別[level] => 1 [ParentID] => 1 )
  61. [9] => Array ( [ id] => 9 [name] => 哈哈[level] => 2 [ParentID] => 4 )
  62. [3] => Array ( [id] => 3 [name] => 二級類別[level ] => 1 [ParentID] => 1 )
  63. [8] => Array ( [id] => 8 [name] => aqqqqqd [level] => 2 [ParentID] => 3 )
  64. [ 2] => Array ( [id] => 2 [name] => 二級類別[level] => 1 [ParentID] => 1 )
  65. [7] => Array ( [id] => 7 [ name] => 234234 [level] => 2 [ParentID] => 2 )
  66. [6] => Array ( [id] => 6 [name] => 333332 [level] => 2 [ParentID] = > 2 )
  67. [5] => Array ( [id] => 5 [name] => 三級類別[level] => 2 [ParentID] => 2 )
  68. [10] => Array ( [id] => 10 [name] => 66333666 [level] => 3 [ParentID] => 5 )
  69. )
  70. */
  71. //指定分類id,然後傳回陣列
  72. function Category_array($category_id = 0,$level=0)
  73. {
  74. global $DB;
  75. $sql = “SELECT * FROM category ORDER BY categoryID DESC”;
  76. $result = $DB-> query($sql);
  77. while ($rows = $DB->fetch_array($result))
  78. {
  79. $category_array[$rows['categoryParentID']][$rows['categoryID'] ] = $rows;
  80. }
  81. foreach ($category_array AS $key=>$val)

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

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

  93. {
  94. $options = $options + $children;
  95. }
  96. }
  97. }
  98. }
  99. unset($category_array[$category_id]);
  100. return $options;
  101. }
  102. ?>>return $options;
  103. }
?> >
複製程式碼

  1. 班級美食

  2. {
  3. < ;p> function Get_Category($category_id = 0,$level = 0, $default_category = 0)
  4. {
  5. echo $category_id;
  6. $arr = array(
  7. '0′ => array (
  8. $arr>
  9. '0′ => array (
  10. '1′ => array('id' => 1, 'parent' => 0, 'name' => '1111'),
  11. '2' => array( 'id' => 2, '父項' => 0, '姓名' => '2222'),
  12. '4' => 陣列('id' => 4, '父項' = > 0, '姓名' => '4444')
  13. ),
  14. '1' => 陣列(
  15. '3' => 陣列('id' => 3, '父級=> 1, '姓名' => '333333'),
  16. '5' => 陣列('id' => 5, '父級' => 1, '姓名' => ; '555555')
  17. ),
  18. ' 3' => 陣列(

  19. '6' => 陣列('id' => 6, '父級=> 3, '姓名' => '66666'),
  20. '7' => 陣列( 'id' => 7, '父級' => 3, '姓名' => ; '77777')
  21. ),
  22. '4' => 陣列(
  23. '8' => 陣列( 'id' => 8, '父級' => 4, '姓名' => '8888'),
  24. '9' => 陣列('id' => 9, '父級' => 4, '名稱' => '9999')
  25. )
  26. );
  27. if (!isset($arr[$category_id]))

  28. {
  29. return “ ”;
  30. }
  31. foreach($arr[$category_id] AS $key =>; $cate)

  32. {
  33. if ($cate['id'] = = $default_category)
  34. {
  35. $txt = “}else{
  36. $txt = “}
  37. 0)

  38. {
  39. $txt1 = “>” 。 str_repeat(“-”, $level) 。 」 「。 $cate['名稱'] 。 “n”;
  40. }其他{
  41. $txt1 =“>” 。 $cate['名稱'] 。 「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. return $out;
  58. }
  59. }
  60. $ = $_GET['id'];
  61. echo “”;
  62. $c = new cate();
  63. //$c->Get_Category();
$ttt= $c ->getFlush ($id,'0′,'3′);
echo $ttt;echo “”;?>
複製代碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板