数据库表
创建item表
CREATE TABLE `item` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`parentid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
表中数据:
系统截图
详细代码
数据库连接文件
conn.php
<?php
$mysqli = new mysqli("localhost","root","","test");
$mysqli->set_charset('utf8');
列表页
list.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>目录列表</title>
<style type="text/css">
div{
width: 800px;
margin: 50px auto;
line-height: 30px;
}
</style>
</head>
<?php
include_once 'conn.php';
include 'getdir.php';
if(isset($_GET['id']) && $_GET['id'] > 0){
$id = intval($_GET['id']);
$query = "SELECT * FROM item WHERE parentid=$id";
$mysqli_res = $mysqli->query($query);
$items = $mysqli_res->fetch_all(MYSQLI_ASSOC);
$query = "SELECT * FROM item WHERE id=$id";
$res = $mysqli->query($query);
$line = $res->fetch_assoc();
$parentid = $line['parentid'];
$dir = getdir($id);
$mysqli->close();
}else{
$query = "SELECT * FROM item WHERE parentid=0";
$mysqli_res = $mysqli->query($query);
$items = $mysqli_res->fetch_all(MYSQLI_ASSOC);
$mysqli->close();
$dir = './';
$parentid = '.';
$id = 0;
}
?>
<body>
<div>
<table border="1" width="800px">
<tr>
<td align='center'>当前目录: <?php echo $dir;?></td><td colspan="2" align='center'><a href='list.php?id=<?php echo $parentid;?>'>上一级</a></td>
</tr>
<?php
foreach ($items as $item){
echo "<tr align='center'>";
echo '<td><a href="./list.php?id='.$item['id'].'">'.$item['name'].'</a></td><td><a href="./add.php?id='.$item['id'].'">添加</a></td><td><a href="./del.php?id='.$item['id'].'">删除</a></td>';
echo "</tr>";
}
?>
<tr>
<td align='center' colspan='3'><a href='add.php?id=<?php echo $id?>'>添加目录</a></td>
</tr>
</table>
</div>
</body>
</html>
添加子目录页面
add.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>添加目录</title>
<style type="text/css">
div{
width: 800px;
margin: 50px auto;
line-height: 30px;
}
</style>
</head>
<?php
include 'conn.php';
if(isset($_GET['id'])){
$id = intval($_GET['id']);
$query = "SELECT * FROM item WHERE id=$id";
$res = $mysqli->query($query);
$line = $res->fetch_assoc();
$dir = $line['name'];
}
?>
<body>
<div>
<table border="1" width="800px">
<tr>
<td align='center'>在目录: <?php echo $dir;?>/ 下创建子目录</td>
</tr>
<tr>
<td align='center'>
<form method='POST' action='./insert.php'>
<input type='hidden' name='parentid' value="<?php echo $id;?>">
<input type='text' name='name' /> <input type='submit' value='提交' />
</form>
</td>
</tr>
</table>
</div>
</body>
</html>
执行添加目录操作
insert.php
<meta charset='utf-8'>
<?php
include_once 'conn.php';
if(isset($_POST['name'])&&isset($_POST['parentid'])){
$parentid = intval($_POST['parentid']);
$name = addslashes($_POST['name']);
$query = "INSERT INTO item(name,parentid) VALUES('$name',$parentid)";
$res = $mysqli->query($query);
if($res){
header("Location:list.php?id=$parentid");
}else{
echo "<script>alert('插入失败');history.go(-1);</script>";
}
}
获取当前目录地址
getdir.php
include_once 'conn.php';
$dir = array();
function getdir($id){
$query = "SELECT * FROM item WHERE id=$id";
global $mysqli;
global $dir;
$res = $mysqli->query($query);
$line = $res->fetch_assoc();
$tempdir = $line['name']."/";
array_unshift($dir, $tempdir);
if($line['parentid']>0){
return getdir($line['parentid']);
}else{
$str = implode($dir);
return $str;
}
}