php遞迴實現樹狀分類結構
阿新 • • 發佈:2019-02-05
如果設計一個遞迴的樹狀結構只需簡單的分類資料庫結構就可以了
實際樹狀結構在Model層和view層實現相應的業務邏輯就可以了
Model層程式碼(tree函式主要實現了遞迴思想):
<?php class CategoryModel extends Model{ //查詢所有分類的資訊 public function getCats(){ $sql = "select * from {$this->table}"; $cats = $this->db->getAll($sql); return $this->tree($cats); } //定義一個方法,用於形成樹狀結構 /** * @param $arr array 給定陣列 * @param $pid int 指定從哪個節點開始找 * @return array 構造好的陣列 */ public function tree($arr, $pid = 0, $level = 0){ static $tree = array(); foreach($arr as $v){ if($v['parent_id'] == $pid){ $v['level'] = $level; $tree[] = $v; $this->tree($arr, $v['cat_id'], $level + 1); } } return $tree; } }
controller實現業務跳轉:
class CategoryController extends Controller{
//顯示商品分類
public function indexAction(){
$categoryModel = new CategoryModel("category");
$cats = $categoryModel->getCats();
//載入顯示頁面
include CUR_VIEW_PATH . "cat_list.html";
}
view層顯示邏輯:
<!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> <title>SHOP 管理中心 - 商品分類 </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="application/views/admin/styles/general.css" rel="stylesheet" type="text/css" /> <link href="application/views/admin/styles/main.css" rel="stylesheet" type="text/css" /> </head> <body> <h1> <span class="action-span"><a href="index.php?p=admin&c=category&a=add">新增分類</a></span> <span class="action-span1"><a href="index.php?p=admin&c=index&a=index">SHOP 管理中心</a> </span><span id="search_id" class="action-span1"> - 商品分類 </span> <div style="clear:both"></div> </h1> <form method="post" action="" name="listForm"> <!-- start ad position list --> <div class="list-div" id="listDiv"> <table width="100%" cellspacing="1" cellpadding="2" id="list-table"> <tbody> <tr> <th>分類名稱</th> <th>商品數量</th> <th>數量單位</th> <th>是否顯示</th> <th>排序</th> <th>操作</th> </tr> <?php foreach($cats as $cat) : ?> <tr align="center" class="0" id="<?php echo $cat['parent_id']; ?>_<?php echo $cat['cat_id']; ?>"> <td align="left" class="first-cell"> <?php echo str_repeat(" ", $cat['level']); ?> <img src="application/views/admin/images/menu_minus.gif" id="icon_0_1" width="9" height="9" border="0" style="margin-left:0em" onclick="rowClicked(this)"> <span><a href="goods.php?act=list&cat_id=1"><?php echo $cat['cat_name']; ?></a></span> </td> <td width="10%">0</td> <td width="10%"><span onclick="listTable.edit(this, 'edit_measure_unit', 1)" title="點選修改內容" style=""><?php echo $cat['unit']; ?></span></td> <td width="10%"><?php echo $cat['is_show'] == 1 ? "√ " : "X"; ?></td> <td width="10%" align="right"><span onclick="listTable.edit(this, 'edit_sort_order', 1)" title="點選修改內容" style=""><?php echo $cat['sort_order']; ?></span></td> <td width="24%" align="center"> <a href="category.php?act=move&cat_id=1">轉移商品</a> | <a href="index.php?p=admin&c=category&a=edit&cat_id=<?php echo $cat['cat_id']; ?>">編輯</a> | <a href="index.php?p=admin&c=category&a=delete&cat_id=<?php echo $cat['cat_id']; ?>" onclick="listTable.remove(1, '您確認要刪除這條記錄嗎?')" title="移除">移除</a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </form> </table> </div> </form> <div id="footer"> 版權所有 © dancheng - </div> </div> <script> /** * 摺疊分類列表 */ var imgPlus = new Image(); imgPlus.src = "application/views/admin/images/menu_plus.gif"; function rowClicked(obj) { // 當前影象 img = obj; // 取得上二級tr>td>img物件 obj = obj.parentNode.parentNode; // 整個分類列表表格 var tbl = document.getElementById("list-table"); // 當前分類級別 var lvl = parseInt(obj.className); // 是否找到元素 var fnd = false; var sub_display = img.src.indexOf('menu_minus.gif') > 0 ? 'none' : 'table-row' ; // 遍歷所有的分類 for (i = 0; i < tbl.rows.length; i++) { var row = tbl.rows[i]; if (row == obj) { // 找到當前行 fnd = true; //document.getElementById('result').innerHTML += 'Find row at ' + i +"<br/>"; } else { if (fnd == true { var cur = parseInt(row.className); var icon = 'icon_' + row.id; if (cur > lvl) { row.style.display = sub_display; if (sub_display != 'none') { var iconimg = document.getElementById(icon); iconimg.src = iconimg.src.replace('plus.gif', 'minus.gif'); } } else { fnd = false; break; } } } } for (i = 0; i < obj.cells[0].childNodes.length; i++){ var imgObj = obj.cells[0].childNodes[i]; if (imgObj.tagName == "IMG" && imgObj.src != 'application/views/admin/images/menu_arrow.gif'){ imgObj.src = (imgObj.src == imgPlus.src) ? 'application/views/admin/images/menu_minus.gif' : imgPlus.src; } } } </script> </body> </html>