首页 > 后端开发 > php教程 > 面向新手的php 面试题及答案-填空题与编程题

面向新手的php 面试题及答案-填空题与编程题

WBOY
发布: 2016-07-25 08:59:18
原创
1104 人浏览过
  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. 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, ‘一级类别’),
  11. (1, ‘二级类别’),
  12. (1, ‘二级类别’),
  13. (1, ‘二级类别’),
  14. (2, ‘三级类别’),
  15. (2, ’333332′),
  16. (2, ’234234′),
  17. (3, ‘aqqqqqd’),
  18. (4, ‘哈哈’),
  19. (5, ’66333666′);
  20. */

  21. //指定分类id变量$category_id,然后返回该分类的所有子类

  22. //$default_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 “
  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. 函数返回的数组格式如下所示:
  58. Array
  59. (
  60. [1] => Array ( [id] => 1 [name] => 一级类别 [level] => 0 [ParentID] => 0 )
  61. [4] => Array ( [id] => 4 [name] => 二级类别 [level] => 1 [ParentID] => 1 )
  62. [9] => Array ( [id] => 9 [name] => 哈哈 [level] => 2 [ParentID] => 4 )
  63. [3] => Array ( [id] => 3 [name] => 二级类别 [level] => 1 [ParentID] => 1 )
  64. [8] => Array ( [id] => 8 [name] => aqqqqqd [level] => 2 [ParentID] => 3 )
  65. [2] => Array ( [id] => 2 [name] => 二级类别 [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] => 2 [ParentID] => 2 )
  69. [10] => Array ( [id] => 10 [name] => 66333666 [level] => 3 [ParentID] => 5 )
  70. )
  71. */
  72. //指定分类id,然后返回数组
  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’ => ’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. ?>
复制代码


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板