How to implement the function of adding categories
The category management page was created earlier to display the categories of videos. Here we will implement how to add categories.
We used the cate.html file in the previous chapter. The display and added categories in the original file are combined on the same page.
Here we separate the "add content" page from cate.html and create a cateadd.php file to implement the category addition page. Simply modify the html page to the following interface:
Select the content of the category in the "Superior Category". Here you need to use recursion to implement the classification, and loop out the database on the html page. Classification of tables.
Infinitus classification code:
<?php function getList($pid=0,&$result=array(),$spac=0) { global $link; $spac +=8; $sql = "select * from cate where pid = $pid"; $res = mysqli_query($link,$sql); while($rows=mysqli_fetch_array($res)) { $rows["cate_name"] = str_repeat(' ',$spac).'--'.$rows["cate_name"]; $result[] = $rows; getList($rows['id'],$result,$spac); } return $result; } $rs=getList(); ?>
Classification loop code:
<div class="form-group"> <div class="label"> <label>上級(jí)分類(lèi):</label> </div> <div class="field"> <select name="pid" class="input w50"> <option value="">請(qǐng)選擇分類(lèi)</option> <?php foreach($rs as $key => $val){?> <option value="<?php echo $val['id'];?>"><?php echo $val["cate_name"]?></option> <?php }?> </select> <div class="tips">不選擇上級(jí)分類(lèi)默認(rèn)為一級(jí)分類(lèi)</div> </div> </div>
Then add data to the cate table through SQL statements: add the superior classification pid and classification name cate_name here and classification level rank. After successful addition, it will be displayed directly to the cate.php category display page.
<?php if($_POST) { $pid = isset($_POST['pid'])?$_POST['pid']:""; $cate_name=isset($_POST['cate_name'])?$_POST['cate_name']:""; $rank=isset($_POST['rank'])?$_POST['rank']:""; if($pid && $cate_name && $rank) { $sql = "insert into cate(pid,cate_name,rank)values('".$pid."','".$cate_name."','".$rank."')"; $rel = mysqli_query($link,$sql); if($rel) { echo "<script language=javascript>alert('添加成功!');window.location='cate.php'</script>"; } }else{ echo "<script>alert('添加失敗,請(qǐng)重新添加');</script>"; } } ?>