php分类问题!
我想做个三级分类,如图所示,我想给裤衩和36D在加上分类的话程序应该怎么写给输出出来?<?phpinclude"lianjie.php";//添加新的分类if(isset...
我想做个三级分类,如图所示,我想给 裤衩 和 36D 在加上分类的话 程序应该怎么写 给输出 出来?
<?php
include "lianjie.php";
//添加新的分类
if (isset($_POST['submit'])) {
$sql = "INSERT INTO `fenlei` (`f_id`,`flname`) VALUE ('$_POST[f_id]','$_POST[flname]')";
$query = mysql_query($sql,$conn);
$res = mysql_affected_rows($conn);
if ($res) {
echo "添加分类成功";
} else {
echo "添加分类失败";
}
}
//添加新的分类结束
//列出所有顶级分类
$sql = "SELECT * FROM `fenlei` WHERE f_id='0'";
$query = mysql_query($sql,$conn);
$djfl = array();
while ($tmp = mysql_fetch_assoc($query)) {
$djfl[] = $tmp;
}
//列出所有的顶级分类结束
//列出所有子类,并以他们的f_id做为一级数组的下标
$sql_sub = "SELECT * FROM `fenlei` WHERE f_id<>0";
$query_sub = mysql_query($sql_sub,$conn);
$sub_fl = array();
while ($tmp_sub = mysql_fetch_assoc($query_sub)) {
$sub_fl[$tmp_sub['f_id']][] = $tmp_sub;
}
function dump($msg) {
echo "<pre>";
print_r($msg);
echo "</pre>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<form action="" method="post" name="">
<select name="f_id">
<option value="0">顶级分类</option>
<?php foreach ($djfl as $v) {?>
<option value="<?php echo $v['id']?>"><?php echo $v['flname']?></option>
<?php foreach ($sub_fl[$v['id']] as $sfv) { ?>
<option value="<?php echo $sfv['id']?>"> <?php echo $sfv['flname']; ?></option>
<?php } ?>
<?php } ?>
</select>
<input type="text" name="flname" />
<input type="submit" name="submit"value="添加" />
</form>
</body>
</html> 展开
<?php
include "lianjie.php";
//添加新的分类
if (isset($_POST['submit'])) {
$sql = "INSERT INTO `fenlei` (`f_id`,`flname`) VALUE ('$_POST[f_id]','$_POST[flname]')";
$query = mysql_query($sql,$conn);
$res = mysql_affected_rows($conn);
if ($res) {
echo "添加分类成功";
} else {
echo "添加分类失败";
}
}
//添加新的分类结束
//列出所有顶级分类
$sql = "SELECT * FROM `fenlei` WHERE f_id='0'";
$query = mysql_query($sql,$conn);
$djfl = array();
while ($tmp = mysql_fetch_assoc($query)) {
$djfl[] = $tmp;
}
//列出所有的顶级分类结束
//列出所有子类,并以他们的f_id做为一级数组的下标
$sql_sub = "SELECT * FROM `fenlei` WHERE f_id<>0";
$query_sub = mysql_query($sql_sub,$conn);
$sub_fl = array();
while ($tmp_sub = mysql_fetch_assoc($query_sub)) {
$sub_fl[$tmp_sub['f_id']][] = $tmp_sub;
}
function dump($msg) {
echo "<pre>";
print_r($msg);
echo "</pre>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<form action="" method="post" name="">
<select name="f_id">
<option value="0">顶级分类</option>
<?php foreach ($djfl as $v) {?>
<option value="<?php echo $v['id']?>"><?php echo $v['flname']?></option>
<?php foreach ($sub_fl[$v['id']] as $sfv) { ?>
<option value="<?php echo $sfv['id']?>"> <?php echo $sfv['flname']; ?></option>
<?php } ?>
<?php } ?>
</select>
<input type="text" name="flname" />
<input type="submit" name="submit"value="添加" />
</form>
</body>
</html> 展开
1个回答
展开全部
<?php
$opts = array();
function opts($f_id=0,$layer=0){
global $opts;
$rs = mysql_query(sprintf('SELECT * FROM fenlei WHERE f_id=%d',$f_id));
while($row = mysql_fetch_assoc($rs)){
$opts[]=spirntf('<option value="%d">%s</option>' , $row['id'] , str_repeat(' -',$layer *4) . $row["flname"]);
opts($row["id"] , $layer + 1);
}
mysql_free_result($rs);
}
opts();
?>
<select>
<?php echo implode("\n",$opts);?>
</select>
$opts = array();
function opts($f_id=0,$layer=0){
global $opts;
$rs = mysql_query(sprintf('SELECT * FROM fenlei WHERE f_id=%d',$f_id));
while($row = mysql_fetch_assoc($rs)){
$opts[]=spirntf('<option value="%d">%s</option>' , $row['id'] , str_repeat(' -',$layer *4) . $row["flname"]);
opts($row["id"] , $layer + 1);
}
mysql_free_result($rs);
}
opts();
?>
<select>
<?php echo implode("\n",$opts);?>
</select>
追问
`````小弟新人 你写的这些 我都看不懂 能在我的程序上 做下改动吗? 麻烦你了
追答
%s' , $row['id'] , str_repeat(' -',$layer *4) . $row["flname"]);
opts($row["id"] , $layer + 1);
}
mysql_free_result($rs);
}
opts();
function dump($msg) {
echo "";
print_r($msg);
echo "";
}
?>
无标题文档
顶级分类
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询