今天学习的成果,递归,无限分类,新手入门希望多多指教。 无 ?php//列出分类include '../include.php';session_start(); if(empty($_SESSION['user'])) header('location:login.php');function getList($pid=0,$result=array(),$spac=0){$spac = $spac+4; //
今天学习的成果,递归,无限分类,新手入门希望多多指教。
<?php //列出分类 include '../include.php'; session_start(); if(empty($_SESSION['user'])) header('location:login.php'); function getList($pid=0,&$result=array(),$spac=0) { $spac = $spac+4; //空格补位 $sql = "SELECT * FROM cate where pid = '$pid'"; //选择分类数据库,条件是对比pid $query = mysql_query($sql); while ($row = mysql_fetch_assoc($query)) { //取得数组 # 美化分类,清晰一级和二级分类 $row['catename'] = str_repeat(' ', $spac).'|--'.$row['catename']; $result[] = $row; //传值 getList($row['id'],$result,$spac); } return $result; } function displayCate($pid=0){ $rs = getList($pid); $str=""; $str.="<select name='cate'>"; foreach ($rs as $key => $value) { # 下拉式显示 $str.= "<option>{$value['catename']}</option>"; } return $str.= "</select>"; } echo displayCate(0); ?>
<?php session_start(); if(empty($_SESSION['user'])) header("location:login.php"); include '../include.php'; $sql = "SELECT * FROM cate order by id asc"; $query = mysql_query($sql); ?> <html> <meta http-equiv="content-type" content="text/html" charset="utf8"> <head> <title>添加分类</title> </head> <body> <h1 align="center">添加分类</h1> <hr> <form method="post" action="cate.php"> <table width="600" align="center" border="0" cellpadding="0" cellspacing="1" > <tr> <td width="80">请输入分类</td> <td width="40"><input type="text" name="catename" size="20"></td> <td width="50">分类id</td> <td width="30"><input type="text" name="pid"></td> <td><input type="submit" name="sub" value="提交"></td> </tr> </table> <!-- 方便自己查看做的实时表查看分类 --> <table align="center" border="1"> <h1 align="center">现有信息表</h1> <hr> <?php while($res = mysql_fetch_array($query)) { ?> <tr> <td>id:</td> <td><?php echo $res['id']; ?></td> <td>分类名称</td> <td><?php echo $res['catename']; ?></td> <td>pid:</td> <td><?php echo $res['pid']; ?></td> <br> </tr> <?php } echo '</table> </form> </body> </html>'; // 实时表到这里结束 //填写分类名称catename,pid if(isset($_POST['sub'])){ $catename = $_POST['catename']; $pid = $_POST['pid']; $table = 'cate'; if(empty($catename)) { echo "分类名不能为空哦"; }else{ cate($table,$catename,$pid);//调用 sql.func.php中的cate分类函数 } } ?>