<?php include 'yanzheng.php';?>
<!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>
<link rel="stylesheet" type="text/css" href="css/css.css" />

<script language="javascript" type="text/javascript">
function preg_match(regStr, str)
{
     var objRegExp = new RegExp(regStr);
     return objRegExp.test(str);
}
function SelectAll(form)
{
for(var j = 0;j < form.elements.length;j++)
     {
         var e = form.elements[j];
         if(preg_match(/del_id/, e.name))
         {
             e.checked = form.select_all.checked;
         }
         else
         {
             continue;
         }
     }
}
</script>
</head>

<body>
<?php require_once 'head.php';?>
<div class="box">

	
		
			<form action="" method="post">
	 <table width="100%" height="111" class="tbdd">		 
		  <tr>
			<td width="143" height="30">批量添加顶级栏目</td>
			<td width="628">
			 <textarea name="typename" style="height:60px; width:600px;font-family: Microsoft YaHei; ">栏目1|栏目2|栏目3 </textarea>    <font color="red">用竖线分割 开头结尾不能有竖线</font>
			</td>			
		  </tr>	
		  <tr>
			<td width="143" height="30"></td>
			<td width="628">			
			 <input name="submitadd" type="submit" class="anniu"  value="添加" /></td>			
		  </tr>	 
	  </table>
	 </form>
	 <!--上面是插入分类--> 
	 	<form name="del_form" action="" method="post"> 
	
	<table width="100%" height="63" border="0" cellpadding="0" cellspacing="0"   class="borer">
	  <tr style="background-color:#EEEEEE;">
		<td width="40" height="31" >全选</td>
		<td width="30">栏目ID</td>
		<td width="152">栏目名称</td>		
		<td width="94">添加子栏目</td>
		<td width="57">修改</td>
		<td width="57">删除</td>
		
	  </tr>

	<!--递归栏目-->	
     <?php	
		    $result = mysql_query('select * from news_type order by id asc');  
			while($arr = mysql_fetch_array($result)){  
				$data[] = array(  
					'id' => $arr['id'],   
					'fid' => $arr['fid'],  
					'typename' => $arr['typename'],   
				);  
			}  
			   
			// 将数据按照缩进简单排列 见图1  
			function data2arr($tree, $rootId = 0, $level = 0) {  
				foreach($tree as $leaf) {  
					if($leaf['fid'] == $rootId) {  
					   ?>
						
						<tr>
							<td width="40" ><input name="del_id[]" class="checkbox" type="checkbox" id="del_id[]" value="<?=$leaf['id']?>" /></td>
							<td><?=$leaf['id']?></td>
							<td width="152">
							<?php 
							if($leaf['fid']>0){$fuhao='├';}
							echo str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level)  . ' ' .$fuhao.$leaf['typename']; 
							?>							 
							</td>		
							<td width="94" ><a href="news_type_add.php?add_id=<?=$leaf[id]?>">添加子栏目</a></td>
							<td width="57">
							   <input name="typename<?=$leaf['id']?>"  type="text" style=" text-align:center;" value="<?=$leaf[typename]?>" />
		                       <input name="benpath<?=$leaf['id']?>"  type="hidden" style=" text-align:center;" value="<?=$leaf[benpath]?>" />
							</td>
							<td><a href="?del=<?=$leaf[id]?>">删除</a></td>		
						 </tr>
					   <?php
						foreach($tree as $l) {  
							if($l['fid'] == $leaf['id']) {  
								data2arr($tree, $leaf['id'], $level + 1);  
								break;  
							}  
						}  
					}  
				}  
			}  
			   
			data2arr($data);  

        ?> 
	 <!--递归栏目-->	
	 
 </table>
 
   
	<table width="50%" height="28" border="0" cellpadding="0" cellspacing="0" class="tbdd">
	  <tr>
	    <td width="49">全选</td>
		<td width="119" style="text-align:center;"><input id="select_all" name="select_all" type="checkbox" class="checkbox" value="1" onClick="SelectAll(this.form)"/></td>	
		<td width="129"><input name="submitxg" type="submit" value="批量修改" class="anniu" /></td>
		<td width="126"><input name="submitdel" type="submit" value="批量删除" class="anniu" /></td>
		<td width="188"></td>
	  </tr>
	</table>
	 <!--list end-->
	 </form>




 <?php
if($_POST['submitadd']){
     
        $typename=$_POST['typename'];
		$typename=trim($typename);
	    $typename = str_replace(array("\r\n", "\r", "\n"), "", $typename);	   
	    $namec=explode('|',$typename);
	   
		$tongji= count(explode('|',$typename));
		for($i=0;$i<$tongji;$i++){ 	   
		 
				mysql_query("INSERT INTO news_type (fid,typename) VALUES ('0','$namec[$i]')");  
				 //如果是顶级分类
				$u=query("SELECT * FROM news_type order by id desc");				
				$uid=$u[id]; //取得最新插入的ID号
				$benpath="0-".$uid.'-';//设置本身路径
				mysql_query("update news_type set benpath='$benpath' where id='$uid'");
				//如果是顶级分类 设置benpath 本身路径	   
		 
		}
	msg('添加成功','news_type.php');
}
?>
<!--批量添加顶级栏目-->   
   
<?php
if($_POST['submitxg']){
$del_id=$_POST['del_id']; //数组


     if($del_id!=""){ 
             $del_num=count($del_id); 
             for($i=0;$i<$del_num;$i++){ 
			     $benpath=$_POST['benpath'.$del_id[$i]];
				 $typename=$_POST['typename'.$del_id[$i]];		 
                 mysql_query("update news_type set typename='$typename' where id='$del_id[$i]'"); 
				 mysql_query("update news set typename='$typename' where benpath='$benpath' ");				
             }  
            msg('修改成功','news_type.php');
      }else{ 
           msg('请选择','news_type.php');
      }

  }
?>  

<!--批量修改-->






  <?php
if($_POST['submitdel']){

$del_id=$_POST['del_id'];
     if($del_id!=""){ 
             $del_num=count($del_id); 
             for($i=0;$i<$del_num;$i++){ 
                 mysql_query("Delete from news_type where id='$del_id[$i]'"); 				
             }  
            msg('删除成功','news_type.php');
      }else{ 
            msg('请选择','news_type.php');
      }

  }
?>  

<!--批量删除-->
		
		

<?php
$del=$_GET['del'];
if($del){
 mysql_query("Delete from news_type where id='$del'"); 		
  msg('删除成功','news_type.php');
}

?>		
		
		

</div>

<?php require_once 'foot.php';?>
</body>
</html>



		
		 
   
