php无限分类代码
A. 如何使用php实现无限级分类
你还在用浪费时间又浪费内存的递归遍历无限极分类吗,看了该篇文章,我觉得你应该换换了。
这是我在OSChina上看到的一段非常精简的PHP无限极分类生成树方法,巧在引用,整理分享了。
复制代码代码如下:
function generateTree($items){
$tree = array();
foreach($items as $item){
if(isset($items[$item['pid']])){
$items[$item['pid']]['son'][] = &$items[$item['id']];
}else{
$tree[] = &$items[$item['id']];
}
}
return $tree;
}
$items = array(
1 => array('id' => 1, 'pid' => 0, 'name' => '安徽省'),
2 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'),
3 => array('id' => 3, 'pid' => 1, 'name' => '合肥市'),
4 => array('id' => 4, 'pid' => 3, 'name' => '长丰县'),
5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'),
);
print_r(generateTree($items));
可以看到下面打印的结果:
复制代码代码如下:
Array
(
[0] => Array
(
[id] => 1
[pid] => 0
[name] => 安徽省
[son] => Array
(
[0] => Array
(
[id] => 3
[pid] => 1
[name] => 合肥市
[son] => Array
(
[0] => Array
(
[id] => 4
[pid] => 3
[name] => 长丰县
)
)
)
[1] => Array
(
[id] => 5
[pid] => 1
[name] => 安庆市
)
)
)
[1] => Array
(
[id] => 2
[pid] => 0
[name] => 浙江省
)
)
上面生成树方法还可以精简到5行:
复制代码代码如下:
function generateTree($items){
foreach($items as $item)
$items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
return isset($items[0]['son']) ? $items[0]['son'] : array();
}
上面这种无限极分类数据树形结构化的方法值得借鉴。但是我觉得这段代码实际用途并不明显啊,你想取出格式化的树形数据还是要递归啊:
复制代码代码如下:
/**
* 如何取数据格式化的树形数据
*/
$tree = generateTree($items);
function getTreeData($tree){
foreach($tree as $t){
echo $t['name'].'<br>';
if(isset($t['son'])){
getTreeData($t['son']);
}
}
}
getTreeData($tree);
B. PHP无限级分类
主要是利用函数的递归,然后在无下级的条件下退出递归!
这个是我自己写的一个函数,无限树型的你参考下吧!
function getCategoryTree($TID, $iTable,$url,$urlPar="1=1") {
if ($TID == 0) {
$treeStr = $treeStr."<ul id=\"tree\" class=\"lightTreeview treeview-black\">";
}
$sql = "select * from `$iTable` where TID1 = ".$TID." order by orderid";
$rs = mysql_query($sql);
while ($ary = mysql_fetch_array($rs)) {
$treeStr = $treeStr."<li>";
$rs2 = mysql_query("select count(*) from `$iTable` where TID1 = ".$ary["id"]);
$Vcount = @mysql_result($rs2, 0);
if ($Vcount != 0) {
$treeStr = $treeStr."<div class=\"treeview-folder\">".$ary["title"]."</div><ul style=\"display:none\">";
$treeStr = $treeStr.getCategoryTree($ary["id"], $iTable,$url,$urlPar);
$treeStr = $treeStr."</ul>";
} else {
$treeStr = $treeStr."<div class=\"treeview-file\"><a href=\"".$url."?T=".$ary["id"]."&".$urlPar."\" target=\"pro\">".$ary["title"]."</a></div>";
}
$treeStr = $treeStr."</li>";
}@mysql_free_result($rs);
if ($TID == 0) {
$treeStr = $treeStr."</ul>";
}
return $treeStr;
}
C. PHP如何通过传引用的思想实现无限分类(代码
在我的Simpla中,用到了无限分类,使用了PHP的传引用思想实现无限分类的方法,可以完美展示类似这样的分类模式。
id pid name
1 0 四川
2 0 重庆
3 1 成都
4
1 绵阳
5 3 高新区
代码如下所示:
/**
* 数组变成无限级分类--传引用思想
* @param array $items
* @return array
*/
public static function get_tree($orig) {
//解决下标不是1开始的问题
$items = array();
foreach ($orig as $key => $value) {
$items[$value[‘id‘]] = $value;
}
//开始组装
$tree = array();
foreach ($items as $key => $item) {
if ($item[‘pid‘] == 0) { //为0,则为1级分类
$tree[] = &$items[$key];
} else {
if (isset($items[$item[‘pid‘]])) { //存在值则为二级分类
$items[$item[‘pid‘]][‘child‘][] = &$items[$key]; //传引用直接赋值与改变
} else { //至少三级分类
//由于是传引用思想,这里将不会有值
$tree[] = &$items[$key];
}
}
}
return $tree;
}
以上内容很简单吧,如有错误或者更好的方法,希望可以相互交流。谢谢。!
D. PHP无限级分类怎么写啊
<?php
/*========================================================
类名:catalog
功能:无限分级类
方法:
树形显示分类
catalog_show($id) //参数$id 递归调用
流程:找到父分类为0所有根分类-> 一直递归取得所有分类并显示
添加分类
catalog_add($uid,$name) //$uid 父id //$name 分类名
流程:依据$uid,在此id下添加一个新子id
删除分类
catalog_del($uid)//参数 $uid 数要删除的分类
修改分类
catalog_set($id,$name) //参数 $id 要修改的分类 //参数 $name 新的分类名
变量:
$config //数据库信息-> host,user,pass,dbname
$catalog_dbname //分类数据库名
数据库:
catalog_id //分类的自然序号
catalog_uid //分类的父分类
catalog_name //分类名
catalog_path_number //亲缘树数字形式 0:1:2
catalog_path_char //亲缘树字符形式 分类1:分类1.1:分类1.1.1
参照文章 http://www.phpchina.com/12823/viewspace_4468.html
========================================================*/
class catalog{
var $config;
var $catalog_dbname;
var $links;
private function connect(){
$this->links = mysql_connect($this->config['host'],$this->config['user'],$this->config['pass']) or die("错误: 第".__LINE__."行<br>".mysql_error());
mysql_select_db($this->config['dbname'],$this->links);
mysql_query("SET NAMES gb2312");
}
function catalog_show($uid = 0){
$this->connect();
$sql = "Select * FROM ".$this->catalog_dbname. "
Where catalog_uid = ". $uid ."
orDER BY catalog_id ";
$result = mysql_query($sql,$this->links) or die("错误: 第".__LINE__."行<br>".mysql_error());
if(mysql_num_rows($result) > 0){
while ($row = mysql_fetch_assoc($result)){
if($this->sun_catalog($row['catalog_id'])){//判断有没有子分类
$cata_img = "<img id = 'img".$row['catalog_id']."' src='./img/last_node.jpg' ōnclick='click_catalog(".$row['catalog_id'].")'/>";
}else{
$cata_img = "<img src='./img/sp.jpg'/>";
}
$path = explode(":",$row['catalog_path_number']);
if(count($path) > 1){
for($i=1;$i<count($path);$i++){
$path_img .= "<img src='./img/sp.jpg'/>";
}
}
echo $path_img.$cata_img;
echo "<a class='menu' href = 'javascrīpt:send_id(".$row['catalog_id'].")'>";
echo $row['catalog_name']."</a><br>";
$path_img = "";
if($this->sun_catalog($row['catalog_id'])){
$hidden_div = "style='display:none'";
echo "<div id = 'div".$row['catalog_id']."' ".$hidden_div.">";
$this->catalog_show($row['catalog_id']);
echo "</div>";
}
}
}
}
private function sun_catalog($uid){//判断是否有子分类
$sql = "Select * FROM ".$this->catalog_dbname. "
Where catalog_uid = ". $uid ."
orDER BY catalog_id ";
$result = mysql_query($sql,$this->links) or die("错误: 第".__LINE__."行<br>".mysql_error());
if(mysql_num_rows($result) > 0){
return true;
}else{
return false;
}
}
function catalog_add($uid,$name){
//获取父id的亲缘树
$this->connect();
$sql = "Select * FROM ".$this->catalog_dbname."
Where catalog_id = '".$uid."'";
$result = mysql_query($sql,$this->links)
or die("错误: 第".__LINE__."行<br>".mysql_error());
$row = mysql_fetch_assoc($result);
$fid_path_number = $row['catalog_path_number'];//id的数字亲缘树
$fid_path_char = $row['catalog_path_char'];//id的字符亲缘树
//插入数据 先插入行->再找到最新插入的id, 在依据这个id进行修改
$sql = "Insert INTO ".$this->catalog_dbname."(catalog_uid,catalog_name)
VALUES(".$uid.",'".$name."')";
$result = mysql_query($sql,$this->links)
or die("错误: 第".__LINE__."行<br>".mysql_error());
$catalog_id = mysql_insert_id();//获取自己的id
$catalog_path_number = $fid_path_number.":".$catalog_id;//得到自己的数字亲缘数
$catalog_path_char = $fid_path_char.":".$name;//得到自己的字符亲缘数
$sql = "Update '".$this->catalog_dbname."'
SET
catalog_path_number = '".$catalog_path_number."',
catalog_path_char = '".$catalog_path_char."'
Where
catalog_id = ".$catalog_id;
mysql_query($sql,$this->links)
or die("错误: 第".__LINE__."行<br>".mysql_error());
}
function catalog_del($id){
$this->connect();
$sql = "Delete FROM ".$this->catalog_dbname."
Where catalog_id = ".$id;
mysql_query($sql,$this->links)
or die("错误: 第".__LINE__."行<br>".mysql_error());
}
function catalog_set($id,$name){
$this->connect();
$sql = "Update ".$this->catalog_dbname."
SET
catalog_name = '".$name."'
Where
catalog_id = ".$id;
mysql_query($sql,$this->links)
or die("错误: 第".__LINE__."行<br>".mysql_error());
}
}
?>
E. php 几种常用的递归 无限极分类
/**
*递归实现无限极分类
*@param$array分类数据
*@param$pid父ID
*@param$level分类级别
*@return$list分好类的数组直接遍历即可$level可以用来遍历缩进
*/
functiongetTree($array,$pid=0,$level=0){
//声明静态数组,避免递归调用时,多次声明导致数组覆盖
static$list=[];foreach($arrayas$key=>$value){//第一次遍历,找到父节点为根节点的节点也就是pid=0的节点
if($value['pid']==$pid){//父节点为根节点的节点,级别为0,也就是第一级
$value['level']=$level;//把数组放到list中
$list[]=$value;//把这个节点从数组中移除,减少后续递归消耗
unset($array[$key]);//开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
getTree($array,$value['id'],$level+1);
}
}
F. 求用php实现无限分类的源码
<?php
require_once("../inc/inc.php");
if ($_GET["parent_id"] == "") {
$parent_id = -1;
} else {
$parent_id = (integer)$_GET["parent_id"];
}
if (!$_GET["category_level"]) {
$category_level = 0;
} else {
$category_level = (integer)$_GET["category_level"];
}
if (!$_GET["pageno"]) {
$pageno = 1;
} else {
$pageno = (integer)$_GET["pageno"];
}
$array_level[0] = "一";
$array_level[1] = "二";
$array_level[2] = "三";
$array_level[3] = "四";
$array_level[4] = "五";
$array_level[5] = "六";
$array_level[6] = "七";
$array_level[7] = "八";
$array_level[8] = "九";
$array_level[9] = "十";
$pagesize = $pagesize_config;if(!$pagesize){$pagesize = 30;}
$category = $categorysql->select_category_by_category_id($parent_id);
if (!trim($_GET["wd"])){
$commons = $categorysql->select_category_by_parent_id($parent_id);
}
else{
$commons = $categorysql->select_category_by_parent_id_and_word($parent_id,trim($_GET["wd"]));
}
if($commons){
$rscount = $commons->recordcount();
}
$pagestart = $pagesize * ($pageno - 1);
$pageend = $pagestart + $pagesize;
if (!$rscount%$pagesize) {
$totalpage = $rscount / $pagesize;
} else {
$totalpage = ($rscount - $rscount%$pagesize) / $pagesize + 1;
}
?>
<html lang="zh">
<meta http-equiv="Content-Type" content="text/html;charset=GB2312">
<head>
<title>分类列表</title>
<link rel=stylesheet type="text/css" href="../css.css">
<script language="javascript" src="../js/window.js"></script>
</head>
<body style="margin:10 10 10 10" onload=document.search.wd.focus()>
<table class="tborder" cellpadding="6" cellspacing="1" border="0" width="100%" align="center">
<tr><td class="alt1" width="100%">
<a title="无框架模式" href="../main.php?cols=0&file=<?php echo $PHP_SELF;?>" target="_top"><img src="../images/navbits_start.gif" border="0"></a><a title="框架模式" href="../main.php?cols=15&file=<?php echo $PHP_SELF;?>" target="_top"><img src="../images/xlink.gif" border="0"></a>
<span class="navbar">
<a href="../category">分类管理</a> -> <?php if(!$category->EOF && $category){echo $category->name;}?>
</span>
</td></tr>
</table>
<form name="search" action="<?php echo $PHP_SELF;?>" method="get">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr valign="middle" height="40">
<td width="190" nowrap>
<a href="addcategory.php?parent_id=<?php echo $parent_id;?>" target="_blank"><font color=#CC0000>添加<?php echo $array_level[$category_level];?>级分类</font></a>
</td>
<td nowrap>
搜索关键字: <input type="text" name="wd" value="<?php echo trim($_GET["wd"]);?>"">
<input type="hidden" name="category_level" value="<?php echo $category_level;?>">
<input type="hidden" name="parent_id" value="<?php echo $parent_id;?>">
<input type="hidden" name="rscount" value="<?php echo $rscount;?>">
<input type="submit" name="submit" value="搜索">
</td>
<td align="right" nowrap>
共有<font color=#CC0000><?php echo $rscount;?></font>个<font color=#CC0000><?php echo $array_level[$category_level];?></font>级分类
每页<font color=#CC0000><?php echo $pagesize;?></font>个
本页<font color=#CC0000><?php if($totalpage>$pageno){echo $pagesize;}else{echo ($rscount-($pageno-1)*$pagesize);}?></font>个
</td>
</tr>
</table>
</form>
<form name="frm" action="mdo.php" method="POST">
<table width=90% align=center cellSpacing=1 cellPadding=3 class="tborder">
<tr valign="middle" class="T1" style="color:#FFFFFF; font-weight: bold">
<td align="center"><div align="center">分类编号</div></td>
<td align="center"><div align="center">分类名称</div></td>
<td align="center"><div align="center">顺序</div></td>
<td align="center"><div align="center">是否置顶</div></td>
<td align="center"><div align="center">是否热门</div></td>
<td align="center"><div align="center">最后更新时间</div></td>
<td align="center"><div align="center">编辑选项</div></td>
</tr>
<?php
while(!$commons->EOF){
if($i>=$pagestart && $i<$pageend){
if(!$j){
$j=$pagestart;
}
?>
<tr valign="middle" class="T<?php echo $i%2+2;?>">
<td align="center"><div align="center">
<?php echo $commons->Fields("CATEGORY_ID");?>
</div></td>
<td align="center"><div align="center">
<?php
$categorys = $categorysql->select_category_by_parent_id($commons->Fields("CATEGORY_ID"));
if (!$categorys->EOF) {
?>
<a href="<?php echo $PHP_SELF;?>?parent_id=<?php echo $commons->Fields("CATEGORY_ID");?>&category_level=<?php echo ($category_level+1);?>" title="<?php while(!$categorys->EOF){echo $categorys->Fields("NAME")."\n"; $categorys->movenext();}?>"><?php }?><font color="#003300"><b><?php echo $commons->Fields("NAME");?></b></font><?php if (!$categorys->EOF) {?></a><?php }?>
</div></td>
<td align="center"><div align="center"><font color="#999999"><?php echo $commons->Fields("SORT_ORDER");?></font></div></td>
<td align="center"><div align="center"><font color="#999999"><?php echo $commons->Fields("IS_TOP");?></font></div></td>
<td align="center"><div align="center"><font color="#999999"><?php echo $commons->Fields("IS_HOT");?></font></div></td>
<td align="center"><div align="center">
<font color="#999999"><?php $time_last=split(" ",$commons->Fields("LAST_MODIFIED"),2);echo $time_last[0];?></font>
</div></td>
<td align="center"><div align="center">
<?php if ($categorys->EOF) {?><a href="../brand/index.php?category_id=<?php echo $commons->Fields("CATEGORY_ID");?>"><font color="#6699FF">品牌</font></a><?php }?>
<?php if ($categorys->EOF) {?><a href="../attribute/index.php?category_id=<?php echo $commons->Fields("CATEGORY_ID");?>"><font color="#FF0000">属性</font></a><?php }?>
<a href="#" onclick="<?php if ($categorys->EOF){echo "if(tishiadd()){";}?>new_window('addcategory.php?parent_id=<?php echo $commons->Fields("CATEGORY_ID");?>');<?php if ($categorys->EOF){echo "}";}?>"><font color="#FF9933">添加子分类</font></a>
<a href="editcategory.php?category_id=<?php echo $commons->Fields("CATEGORY_ID");?>" target="_blank"><font color=green>编辑</font></a>
<input name="checkbox<?php echo (integer)$i?>" type="checkbox" value="<?php echo $commons->Fields("CATEGORY_ID");?>">
</div></td>
</tr>
<?php
$j++;
}
$i++;
$commons->movenext();
}
?>
<tr><td align="right" colspan="6">
<input type="hidden" name="category_level" value="<?php echo $category_level;?>">
<input type="hidden" name="parent_id" value="<?php echo $parent_id;?>">
<input type="hidden" name="pageno" value="<?php echo $pageno;?>">
<input type="hidden" name="rscount" value="<?php echo $rscount;?>">
<input type="button" name ="true" value="全选" onclick="javascript:
<?php for($i=$pagestart;$i<$j;$i++){echo "document.frm.checkbox".$i.".checked=true;";}?>">
<input type="button" name ="false" value="全否" onclick="javascript:
<?php for($i=$pagestart;$i<$j;$i++){echo "document.frm.checkbox".$i.".checked=false;";}?>">
<input type="button" name ="false" value="反选" onclick="javascript:
<?php for($i=$pagestart;$i<$j;$i++){echo "if(document.frm.checkbox".$i.".checked==false){document.frm.checkbox".$i.".checked=true;}else{document.frm.checkbox".$i.".checked=false;}";}?>">
<input type="submit" name ="submit" value="删除所选" onclick="javascript:return tishi();">
<?php if($category_level!=0){?>
<input type="submit" name ="submit" value="更改父分类">
<?php }?>
</form>
</td><td align="right" colspan="2">
<form name="page" method="GET" action="<?php echo $PHP_SELF;?>">
<select name="pageno" onchange="javascript:window.location=('<?php echo $PHP_SELF;?>?parent_id=<?php echo $parent_id;?>&category_level=<?php echo $category_level;?>&pageno='+this.options[this.selectedIndex].value)">
<?php
for($i=1;$i<=$totalpage;$i++){
echo "\t\t\t<option value=".$i;
if($pageno==$i){echo " selected";}
echo ">第".$i."页</option>\n";
}
?>
</select>
<input type="hidden" name="category_level" value="<?php echo $category_level;?>">
<input type="hidden" name="parent_id" value="<?php echo $parent_id;?>">
</form>
</td></tr>
</table>
</body>
</html>
G. php无限分类怎么弄
无限分类?是指自己可以随意添加分类,然后全部显示出来?
加个条件循环就可以了!
$type=mysql_query("select * from type order by id desc"); //查询表type,也就是分类的表
<?php do { ?>
需要输出的内容如:<?php echo $row['type']; ?>
<?php } while ($row= mysql_fetch_assoc($type)); //在数据库中取值?>
H. php递归无限极分类怎么弄
给个函数你,调用get_category()就是一个数组格式的结果
function get_category($parent_id=0){
$arr=array();
$sql = "select * from category where parent_id=$parent_id";//查询子级数据
$result = array(a_object,b_object,,,)=sql_query($sql);//查询结果一个数组或列表格式,自己完善。
if($result){
foreach($result as $re){//循环数组
if(get_category($re.id))//如果子级不为空
$re['child'] = get_category($re.id);
$arr[] = $re;
}
return $arr;
}
I. php 无限分类
可以在表A中增加一个字段,该字段关联表B的SID,或者可以新建一张关联表C (ID,NEWSID,SID) 起到关联作用
select * from b2b_news as a,b2b_news_sort as b where a.sid= b.sid
是关联查询吗 ,如果有其他条件的话,在后面加and吧