怎么用php做填空题,面向新手的php 面试题及答案-填空题与编程题

/*

数据表结构如下:

CREATE TABLE `category` (

`categoryID` smallint(5) unsigned NOT NULL auto_increment,

`categoryParentID` smallint(5) unsigned NOT NULL default ’0′,

`categoryName` varchar(50) NOT NULL default ”,

PRIMARY KEY (`categoryID`)

) ENGINE=MyISAM DEFAULT CHARSET=gbk;

INSERT INTO `category` ( `categoryParentID`, `categoryName`) VALUES

(0, ‘一级类别’),

(1, ‘二级类别’),

(1, ‘二级类别’),

(1, ‘二级类别’),

(2, ‘三级类别’),

(2, ’333332′),

(2, ’234234′),

(3, ‘aqqqqqd’),

(4, ‘哈哈’),

(5, ’66333666′);

*/

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

//$default_category为默认的选中的分类

function Get_Category($category_id = 0,$level = 0, $default_category = 0)

{

global $DB;

$sql = “SELECT * FROM category ORDER BY categoryID DESC”;

$result = $DB->query( $sql );

while ($rows = $DB->fetch_array($result))

{

$category_array[$rows[categoryParentID]][$rows[categoryID]] = array(‘id’ => $rows[categoryID], ‘parent’ => $rows[categoryParentID], ‘name’ => $rows

[categoryName]);

}

if (!isset($category_array[$category_id]))

{

return “”;

}

foreach($category_array[$category_id] AS $key => $category)

{

if ($category['id'] == $default_category)

{

echo “

}else

{

echo “

}

if ($level > 0)

{

echo “>” . str_repeat( ” “, $level ) . ” ” . $category['name'] . “\n”;

}

else

{

echo “>” . $category['name'] . “\n”;

}

Get_Category($key, $level + 1, $default_category);

}

unset($category_array[$category_id]);

}

/*

函数返回的数组格式如下所示:

Array

(

[1] => Array ( [id] => 1 [name] => 一级类别 [level] => 0 [ParentID] => 0 )

[4] => Array ( [id] => 4 [name] => 二级类别 [level] => 1 [ParentID] => 1 )

[9] => Array ( [id] => 9 [name] => 哈哈 [level] => 2 [ParentID] => 4 )

[3] => Array ( [id] => 3 [name] => 二级类别 [level] => 1 [ParentID] => 1 )

[8] => Array ( [id] => 8 [name] => aqqqqqd [level] => 2 [ParentID] => 3 )

[2] => Array ( [id] => 2 [name] => 二级类别 [level] => 1 [ParentID] => 1 )

[7] => Array ( [id] => 7 [name] => 234234 [level] => 2 [ParentID] => 2 )

[6] => Array ( [id] => 6 [name] => 333332 [level] => 2 [ParentID] => 2 )

[5] => Array ( [id] => 5 [name] => 三级类别 [level] => 2 [ParentID] => 2 )

[10] => Array ( [id] => 10 [name] => 66333666 [level] => 3 [ParentID] => 5 )

)

*/

//指定分类id,然后返回数组

function Category_array($category_id = 0,$level=0)

{

global $DB;

$sql = “SELECT * FROM category ORDER BY categoryID DESC”;

$result = $DB->query($sql);

while ($rows = $DB->fetch_array($result))

{

$category_array[$rows['categoryParentID']][$rows['categoryID']] = $rows;

}

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

{

if ($key == $category_id)

{

foreach ($val AS $k=> $v)

{

$options[$k] =

array(

‘id’ => $v['categoryID'], ‘name’ => $v['categoryName'], ‘level’ => $level, ‘ParentID’=>$v['categoryParentID']

);

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

if (count($children) > 0)

{

$options = $options + $children;

}

}

}

}

unset($category_array[$category_id]);

return $options;

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值