设为首页 收藏本站
查看: 995|回复: 0

[经验分享] php无限极分类两种方法 有图有真像

[复制链接]

尚未签到

发表于 2015-8-28 12:24:29 | 显示全部楼层 |阅读模式
  今天写了下无限极分类 下面就把代码贴上来了 写的不怎么样。
  method of classify one



<?php
/*
reader: 这是自己写的无限极分类实现方法 里面的编辑方法只是对分类名进行了编辑
没有进行移动操作 小弟能力有限忘大家多多包涵啊
第一种方法:
CREATE TABLE `types` (
`type_id` int(11) NOT NULL AUTO_INCREMENT,
`type_name` varchar(20) NOT NULL,
`type_p_id` varchar(64) NOT NULL DEFAULT '-',
PRIMARY KEY (`type_id`),
KEY `type_name` (`type_name`),
KEY `tname` (`type_name`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
注:
这里没做字段有效验证;
type_id     表示主键自增
type_name     表示分类名
type_p_id     表示分类路径 这里的分类路径是 上层父类的分类路径加上此类的主键id 用逗号隔开
*/
error_reporting(7);
header("Content:text/html;charset=utf-8");
//这里先进行操作的判断
$arr = array('list','add','del','edit','addok','edit_ok');
$act= in_array($_GET['ac'],$arr)?$_GET['ac']:$arr[0];
//连接数据库
$conn = mysql_connect("localhost","root","root")or die('数据库连接失败');
mysql_select_db("study",$conn);
mysql_query("set names utf8");
//根据分类层进行排序
$sql = "select * from types order by type_p_id";
$sql1 = mysql_query($sql);
//添加分类
if($act == "addok"){
$type_id = $_POST['type_id'];
$type_name = $_POST['type_name'];
//如果等于0表示是跟目录
if($type_id=="0"){
$sql = "insert into types set type_name = '{$type_name}'";
$res = mysql_query($sql);
$id = mysql_insert_id();
$sql = "update types set type_p_id = '$id,' where type_id=$id";
$res = mysql_query($sql);
if( mysql_affected_rows ()>0){
echo "<script>location.href='ok.php?act=list'</script>";
}else{
echo "<script>alert('添加失败');</script>";
}
}//如果不等于0
else{
//根据typeid 找到 该id下的type_p_id
$sql = "select type_p_id from `types` where type_id =  $type_id";
$res = mysql_query($sql);
$res = mysql_fetch_assoc($res);
$type_id = $res['type_p_id'];
//先将名称插入进去
$sql = "insert into types set type_name = '{$type_name}'";
$res = mysql_query($sql);
//获取最后执行的id 然后进行数据更新 主要更新 type_p_id
$id = mysql_insert_id();
$sql = "update types set type_p_id = '$type_id$id,' where type_id=$id";
$res = mysql_query($sql);
if($res){
echo "<script>location.href='ok.php?act=list'</script>";
}else{
echo "<script>alert('添加失败');</script>";
}
}
}elseif($act=="add"){
?>
<form method="post" action="?ac=addok">
新分类名称:    <input type="text" name="type_name"><br/>
当前分类:<select name="type_id" >
<option  value="0">顶级分类</option>
<?php
//循环遍历分类
while($res = mysql_fetch_assoc($sql1)){
//查找 ","号出现的次数
$m=substr_count($res['type_p_id'],",");
//使用空格替换 "空格"
$strpad = str_pad("",$m*8*2,"&#12288;");
if($m==1){
$sele = "disabled";
}else{
$sele = "";
}
?>
<option value="<?php echo $res['type_id']?>"><?php  echo $strpad.$res['type_name']?></option>
<?php
}
?>
</select><br />
<input type="submit"  value="添加"/>
</form>
<?php               
}elseif($act == "list"){
//获取列表根据 分类层进行排序
$sql = "select * from types order by type_p_id";
$res = mysql_query($sql);
?>
<table width="50%">
<tr><td>id</td><td>分类名</td><td>path路径</td><td>操作</td></tr>
<?php
while($arr = mysql_fetch_assoc($res)){?>
<tr>
<td><?php echo $arr['type_id']?></td>
<td><?php echo $arr['type_name']?></td>
<td><?php echo $arr['type_p_id']?></td>
<td ><a href="?ac=edit&type_id=<?php echo $arr['type_id'];?>">编辑</a> |
<a href="?ac=del&type_id=<?php echo $arr['type_id'];?>">删除</a></td>
</tr>
<?php
}
?>
</table>
<input type="button"  value="添加" >
<?php               
}elseif($act == "edit"){
$id = $_GET['type_id'];
$sql = "select *from  `types` where type_id=$id ";
$res = mysql_query($sql);
echo "<form  method='post'  action='?ac=edit_ok'>";
while($arr = mysql_fetch_assoc($res)){
echo "当前名称:{$arr['type_name']}"."<br />";
echo "新名称:<input type='text'  name='n_type_name'  >"."<br />";
echo "<input type='hidden'  value={$arr['type_id']} name='id'/>";
}
echo "<input type='submit'  value='更新'>";
echo "</form>";
}elseif($act == "edit_ok"){
$name = trim($_POST['n_type_name']);
$id = $_POST['id'];
if(!empty($name)){
$sql = "update  `types` set type_name='$name' where type_id=$id";
mysql_query($sql);
echo "<script>location.href('ok.php?act=list')</script>";
}else{
echo "<script>location.href('ok.php?act=list')</script>";
}
}elseif($act == 'del'){
//这里的删除是要把当前分类 和当前的子分类都要删除 所以用到$id%
$id = $_GET['type_id'];
$sql ="delete from `types` where type_p_id like '$id%' ";
$res = mysql_query($sql);
if($res){
echo "<script>location.href('ok.php?act=list')</script>";
}else{
echo "<script>alert('删除失败');</script>";
}
}
?>
  types表:
DSC0000.png
  下面是效果图:
DSC0001.jpg
  method of classify two



<?php
/*
reader:
这是自己写的无限极分类实现方法 里面的编辑很简单的(你们懂得。) 就没写了
没有进行移动操作 小弟能力有限忘大家多多包涵啊
第二种方法:
CREATE TABLE `types` (
`type_id` int(11) NOT NULL AUTO_INCREMENT,
`type_name` varchar(20) NOT NULL,
`type_p_id` varchar(64) NOT NULL,
PRIMARY KEY (`type_id`),
KEY `type_name` (`type_name`),
KEY `tname` (`type_name`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
注:
这里没做字段有效验证;
type_id     表示主键自增
type_name     表示分类名
type_p_id     表示分类路径 这里的分类路径是 上层的分类id 如果是当前分类 这个值为 0
*/
error_reporting(7);
header("Content-type:text/html;charset=utf-8");
//这里先进行操作的判断
$arr = array('list','add','del','edit','addok','edit_ok');
$act= in_array($_GET['ac'],$arr)?$_GET['ac']:$arr[0];
//连接数据库
$conn = mysql_connect("localhost","root","root")or die('数据库连接失败');
mysql_select_db("study",$conn);
mysql_query("set names utf8");
if($act=="add"){
/**
*    @access public
* @param array $types 数组 这里的数组要传引用&
*    @param interal $pid 所属的分类上层分类id
*    @param int $path 分类层 默认从0开始每次循环加一
* @return array();
*/
function getTypes(&$types=array(),$pid=0,$path=0){
$sql = "select * from `type` where type_p_id = $pid";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)){
$str = "";
for($i=0;$i<$path;$i++){
$str.="&#12288;";
}
$row['new_type_name'] = $str.$row['type_name'];
$types[] = $row;
getTypes($types,$row['type_id'],($path+1));
}
return $types;
}
//获取分类 调用函数
getTypes($types);
//获取列表根据 分类层进行排序
$sql1 = "select * from type order by type_id";
$sqll = mysql_query($sql1);
?>
<form method="post" action="?ac=addok">
新分类名称:    <input type="text" name="type_name"><br/>
当前分类:<select name="type_id" >
<option  value="0">顶级分类</option>
<?php
//循环这个数组将分类正确输出
for($i=0;$i<count($types);$i++){
?>
<option value="<?php echo $types[$i]['type_id']?>"><?php  echo $types[$i]['new_type_name']?></option>
<?php
}
?>
</select><br />
<input type="submit"  value="添加"/>
</form>
<?php
}elseif($act == "addok"){
$type_name = $_POST['type_name'];
$type_id = $_POST['type_id'];
$sql = "insert into `type`(type_name,type_p_id) values ('$type_name','$type_id')";
$res = mysql_query($sql);
if(mysql_insert_id()>0){
echo "<script>location.href='two.php?act=list'</script>";
}else{
echo "<script>alert('添加失败');</script>";
}
}elseif($act == "list"){
//获取列表根据 分类层进行排序
$sql = "select * from type order by concat(type_id,type_p_id)";
$res = mysql_query($sql);
?>
<table width="50%">
<tr><td>id</td><td>分类名</td><td>path路径</td><td>操作</td></tr>
<?php
while($arr = mysql_fetch_assoc($res)){?>
<tr>
<td><?php echo $arr['type_id']?></td>
<td><?php echo $arr['type_name']?></td>
<td><?php echo $arr['type_p_id']?></td>
<td ><a href="?ac=edit&type_id=<?php echo $arr['type_id'];?>">编辑</a> |
<a href="?ac=del&type_id=<?php echo $arr['type_id'];?>">删除</a></td>
</tr>
<?php
}
?>
</table>
<input type="button"  value="添加" >
<?php
}elseif($act == "del"){
/***
这里要删除大分类的时候必须要删除该大分类下的子分类
所以这里开始mysql事务 mysql 事务开启的方式 事务详细说明请参考
(http://www.cnblogs.com/wlgaojin/archive/2012/10/26/2741600.html)
*/
mysql_query("SET AUTOCOMMIT=1");//开启事务
$type_id = $_GET['type_id'];
//删除该分类
$sqlone = "delete from `type` where type_id=$type_id";
//删除该分类下的子分类
$sqltwo = "delete from `type`where type_p_id=$type_id";
$res1 = mysql_query($sqlone);
$res2 =    mysql_query($sqltwo);
if($res1 && $res2)
{
mysql_query("COMMIT");
echo "<script>location.href='two.php?act=list'</script>";
}else{
mysql_query("ROLLBACK");
echo "<script>alert('删除失败');</script>";
}
}
?>
  type表:
  
DSC0002.png
  
  下面是效果图
DSC0003.jpg
  写的确实不怎么样啊 往大家不要喷啊

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-105525-1-1.html 上篇帖子: php文件上传及mime类型大全 下篇帖子: vs.php 调试 php
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表