无限级分类迭代方法的编辑操作方法

文章原创:安徽php社区
文章来源: http://bbs.hfphp.org/thread-768-1-1.html
转载请说明出处

无限级分类使用迭代的方法时,在显示编辑页面的时候,需要将编辑的分类所对应的本身及其所有子分类从分类列表中清楚,该代码的edit动作即完成此工作。同时还加入了css控制的显示效果。
    1. /*
   2. * 迭代sql查询,生成无限级分类树
   3. * <span href="tag.php?name=%E6%95%B0%E6%8D%AE" onclick="tagshow(event)" class="t_tag">数据</span>结构
   4.     CREATE TABLE `cat` (
   5.         `cid` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
   6.         `cname` VARCHAR(50) NOT NULL,
   7.         `lev` INT(5) UNSIGNED NOT NULL COMMENT '分类层数',
   8.         `pid` INT(5) UNSIGNED NOT NULL COMMENT '上级cid',
   9.         PRIMARY KEY (`cid`)
  10.     )
  11. */
  12. error_reporting(7);
  13. <span href="tag.php?name=mysql" onclick="tagshow(event)" class="t_tag">mysql</span>_connect("localhost", "hfphp", "hfphp");
  14. mysql_select_db("test");
  15. mysql_query("set names utf8");
  16.  
  17. $act = $_GET['act'];
  18. if($act == "edit") {
  19.     $edited_cid = intval($_GET['cid']);
  20.     if(empty($edited_cid)) {
  21.         //出错提示
  22.         die("cid is not empty");
  23.     }
  24.     $query = "select * from cat where cid='$edited_cid'";
  25.     $res = mysql_query($query);
  26.     $edited_cat = @mysql_fetch_assoc($res);
  27.  
  28.     $okArr = array();//保存<span href="tag.php?name=%E7%9B%AE%E5%BD%95" onclick="tagshow(event)" class="t_tag">目录</span>树的数组
  29.     $pid = 0;
  30.     getCat($pid);
  31.     ?>
  32.     <form method="post" action="cat.php?act=insert">
  33.         分类名:<input type="text" name="cname"/>
  34.         所属分类:<select name="cid">
  35.             <option value="0">根目录</option>
  36.             < ?php
  37.             $jump = false;
  38.             foreach($okArr as $row) {
  39.                 if($row['cid'] == $edited_cid) {
  40.                     $jump = true;//跳过开关打开
  41.                     continue;
  42.                 }elseif($row['lev'] <= $edited_cat['lev']) {
  43.                     /*
  44.                      * 根据迭代结果数组的分类顺序
  45.                      * 只要当前循环分类记录的lev等级小于等于所编辑的分类,即跳过阶段结束
  46.                      */
  47.                     $jump = false;//跳过开关关闭
  48.                 }
  49.                
  50.                 if($jump == false) {
  51.                     echo "<option value='".$row['cid']."'>";
  52.                     for($i=1;$i< $row['lev'];$i++) {
  53.                         echo " ";
  54.                     }
  55.                     echo $row['cname'];
  56.                     echo "</option>";
  57.                 }
  58.             }
  59.             ?>
  60.         </select>
  61.         <input type="submit" name="提交"/>
  62.     </form>
  63.     < ?php
  64. }elseif($act == "insert") {
  65.     $cname = trim($_POST['cname']);
  66.     $pid = intval($_POST['cid']);
  67.  
  68.     $query = "select * from cat where cid='$pid'";
  69.     $res = mysql_query($query);
  70.     $row = @mysql_fetch_assoc($res);
  71.     $parent_lev = $row['lev'];
  72.  
  73.     $query = "insert into cat set cname='$cname',lev='".($parent_lev+1)."',pid='$pid'";
  74.     mysql_query($query);
  75.  
  76.     echo "<script>alert('增加新分类成功');";
  77.     echo "location.href='".$_SERVER['<span href="tag.php?name=PHP" onclick="tagshow(event)" class="t_tag">PHP</span>_SELF']."';";
  78.     die;
  79. }else{
  80.     $okArr = array();//保存目录树的数组
  81.     $pid = 0;
  82.     getCat($pid);
  83.    
  84.     ?>
  85.     <h2>目录树</h2>
  86.     <style>
  87.         <span href="tag.php?name=div" onclick="tagshow(event)" class="t_tag">div</span>{margin:2px;border:1px solid gray;padding:5px;}
  88.     </style>
  89.     <script src="<span href="tag.php?name=jquery" onclick="tagshow(event)" class="t_tag">jquery</span>-1.3.2.min.js"></script>
  90.     <script>
  91.         function display(cid) {
  92.             if($(".dd"+cid).css("display") == "block") {
  93.                 $(".dd"+cid).css("display", "none");
  94.                 $("#btn"+cid).val("查看子分类");
  95.             }else{
  96.                 $(".dd"+cid).css("display", "block");
  97.                 $("#btn"+cid).val("收起子分类");
  98.             }
  99.         }
 100.     </script>
 101.     < ?php
 102.     //debug
 103.     $lastLev = 0;//上一个分类的层级
 104.     $<span href="tag.php?name=class" onclick="tagshow(event)" class="t_tag">class</span>Arr = array();//div的class数组,按层级计算
 105.  
 106.     foreach($okArr as $row) {
 107.         echo "<div ";
 108.        
 109.         if($row['lev'] > $lastLev){//debug
 110.             $classArr[$row['pid']] = 1;
 111.         }elseif($row['lev'] < = $lastLev) {
 112.             for($i=0;$i<($lastLev - $row['lev']);$i++) {//关键*减去当前层级与上一次层级间的多余class
 113.                 array_pop($classArr);
 114.             }
 115.         }
 116.         echo " class='dd".str_replace(" ", " dd", implode(" ", array_keys($classArr)))."' ";
 117.         $lastLev = $row['lev'];
 118.        
 119.  
 120.         if($row['lev'] != 1) {
 121.             echo "style='display:none;'";
 122.         }
 123.         echo ">";
 124.  
 125.         //多层级的占位符
 126.         for($i=1;$i< $row['lev'];$i++) {
 127.             echo " ";
 128.         }
 129.  
 130.         echo $row['cname'].',lev:'.$row['lev'];
 131.  
 132.         echo "  <a href='".$_SERVER['PHP_SELF']."?act=edit&cid=".$row['cid']."' style='font-size:12px;color:red;'>编辑";
 133.  
 134.         echo " <input type='button' id='btn".$row['cid']."' onclick='display(".$row['cid'].");' value='查看子分类'/>";
 135.         echo "";
 136.     }
 137.     echo "<hr />";
 138.     echo "<h4>创建新分类</h4>";
 139. ?>
 140.     <form method="post" action="<?php echo($_SERVER['PHP_SELF']);?>?act=insert">
 141.         分类名:<input type="text" name="cname"/>
 142.         所属分类:<select name="cid">
 143.             <option value="0">根目录</option>
 144.             < ?php
 145.             foreach($okArr as $row) {
 146.                 echo "<option value='".$row['cid']."'>";
 147.                 for($i=1;$i< $row['lev'];$i++) {
 148.                     echo " ";
 149.                 }
 150.                 echo $row['cname'];
 151.                 echo "</option>";
 152.             }
 153.             ?>
 154.         </select>
 155.         <input type="submit" name="提交"/>
 156.     </form>
 157. < ?php
 158. }
 159.  
 160. function getCat($pid) {
 161.     global $okArr, $n;
 162.    
 163.     $sql = "select * from cat where pid='$pid'";
 164.     $res = mysql_query($sql);
 165.  
 166.     while($row = @mysql_fetch_assoc($res)) {
 167.         if(!empty($row)) {
 168.             $okArr[] = $row;
 169.  
 170.             getCat($row['cid']);
 171.         }
 172.     }
 173. }

欢迎大家来到交流
http://bbs.hfphp.org

你可能感兴趣的:(jquery,sql,PHP,mysql,css)