Thinkphp5 model class implements unlimited classification

    // Get a classification ID of all subcategories 
    public  function the getChildren ( $ catId ) 
    { 
        // Remove all free 
        $ Data = $ the this -> SELECT ();
         // recursive ID singled out from all the sub-categories of classification in 
        return  the this $ -> _ the getChildren ( $ data , $ catId , TRUE ); 
    } 
    / * * 
     * recursive data from the sub-categories to find 
     * / 
    Private  function _getChildren ( $ data , $ catId , $ isClear = FALSE ) 
    { 
        static  $ _RET= Array ();   // save Found ID subcategory 
        IF ( $ isClear )
             $ _RET = Array ();
         // for all sub-categories to find free circulating 
        the foreach ( $ Data  AS  $ K => $ V ) 
        { 
            IF ( $ v [ 'the parent_id'] == $ catId ) 
            { 
                $ _RET [] = $ v [ 'ID' ];
                 // find the subcategory $ v 
                $ the this -> _ the getChildren ( $ Data , $ v [ 'ID '  ]);
            }
        }
        return $_ret;
    }
    // 获取树形数据
    public function getTree()
    {
        $data = $this->select();
        return $this->_getTree($data);
    }
    private function _getTree($data, $parent_id=0, $level=0)
    {
        static $_ret = array();
        foreach ($data as $k => V $ )
        { 
            IF ( $ V [ 'the parent_id'] == $ the parent_id ) 
            { 
                $ V [ 'Level'] = $ Level ;   // used to mark this classification is the first few levels of 
                $ _RET [] = $ V ;
                 / / find Subcategories 
                $ the this -> _ getTree ( $ Data , $ V [ 'ID'], $ Level + 1'd ); 
            } 
        } 
        return  $ _RET ; 
    } 
    
    protected  function _before_delete (& $ Option ) 
    { 
        / ************** modify the original $ option, all subcategories of ID also add to the mix, this TP will be deleted together ****** * / 
        // first identify all subcategories the ID 
        $ Children = $ the this -> the getChildren ( $ Option [ 'WHERE'] [ 'ID' ]);
         $ Children [] = $ Option [ 'WHERE'] [ 'ID' ];
         $ Option [ 'WHERE'] [ 'ID'] = Array (
             0 => 'the IN', 
            . 1 => The implode ( ',', $ Children ), 
        ); 
    }
<form method="post" action="" name="listForm" onsubmit="">
    <div class="list-div" id="listDiv">
        <table cellpadding="3" cellspacing="1">
            <tr>
                <th>分类名称</th>
                <th>操作</th>
            </tr>
            <?php foreach ($data as $k => $v): ?>
            <tr class="tron">
                <td><?php echo str_repeat('-', 8*$v['level']) . $v['cat_name']; ?></td>
                <td align="center">
                    <a href="<?php echo U('edit?id='.$v['id']); ?>">修改</a>
                    <a onclick="return confirm('确定要删除吗?');" href="<?php echo U('delete?id='.$v['id']); ?>">删除</a>
               </td>
            </tr>
            <?php endforeach; ?>
        </table>
    </div>
</form>

 

Guess you like

Origin www.cnblogs.com/fksdy/p/11487623.html