當前位置:首頁 » 編程語言 » php無限分類代碼

php無限分類代碼

發布時間: 2023-01-29 16:39:13

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吧

熱點內容
造物者編程 發布:2024-03-29 08:50:27 瀏覽:533
sql技能 發布:2024-03-29 08:50:23 瀏覽:55
希沃安卓下載安裝應用在哪裡 發布:2024-03-29 08:22:51 瀏覽:630
python和excel 發布:2024-03-29 07:47:03 瀏覽:860
postfix源碼下載 發布:2024-03-29 07:42:03 瀏覽:142
怎麼在電腦上玩手機伺服器 發布:2024-03-29 07:30:13 瀏覽:141
倍福加密 發布:2024-03-29 07:24:42 瀏覽:844
如何用密碼鎖住並隱藏工作表 發布:2024-03-29 07:03:28 瀏覽:327
按鍵精靈滑鼠腳本 發布:2024-03-29 06:47:41 瀏覽:20
pythonhome 發布:2024-03-29 06:47:36 瀏覽:170