ホームページ  >  記事  >  バックエンド開発  >  PHP 無限分類クラス_PHP チュートリアル

PHP 無限分類クラス_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-21 15:58:14722ブラウズ

コードをコピーします コードは次のとおりです:

/**
* @author YangHuan
* @datetime
* @version 1.0.0
*/

/**
* Short description.
*
* Detail description
* @author
* @version 1.0
* @copyright
* @access public
*/
class Tree
{
/**
* Description
* @var
* @since 1.0
* @access private
*/
var $data = array();

/**
* Description
* @var
* @since 1.0
* @access private
*/
var $child = array(-1=>array());

    /**
     * Description
     * @var       
     * @since     1.0
     * @access    private
     */
    var $layer    = array(-1=>-1);

    /**
     * Description
     * @var       
     * @since     1.0
     * @access    private
     */
    var $parent    = array();

    /**
     * Short description. 
     *
     * Detail description
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function Tree ($value)
    {
        $this->setNode(0, -1, $value);
    } // end func

    /**
     * Short description. 
     *
     * Detail description
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function setNode ($id, $parent, $value)
    {
        $parent = $parent?$parent:0;

        $this->data[$id]            = $value;
        $this->child[$id]            = array();
        $this->child[$parent][]        = $id;
        $this->parent[$id]            = $parent;

        if (!isset($this->layer[$parent]))
        {
            $this->layer[$id] = 0;
        }
        else
        {
            $this->layer[$id] = $this->layer[$parent] + 1;
        }
    } // end func

    /**
     * Short description. 
     *
     * Detail description
     * @param      none
     * @global     none 
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getList (&$tree, $root= 0)
    {
        foreach ($this->child[$root] as $key=>$id)
        {
            $tree[] = $id;

            if ($this->child[$id]) $this->getList($tree, $id);
        }
    } // end func

    /**
     * Short description. 
     *
     * Detail description
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getValue ($id)
    {
        return $this->data[$id];
    } // end func

    /**
     * Short description. 
     *
     * Detail description
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getLayer ($id, $space = false)
    {
        return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
    } // end func

/**
* 短い説明。 
*
* 詳細説明
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update 日付時刻
   */
function getParent ($id)
{
return $this->parent[$id];
} // end func

/**
* 短い説明。 
*
* 詳細説明
* @param なし
* @global なし
* @since 1.0
* @access private
* @return void
* @update 日付時刻
   */
function getParents ($id)
{
while ($this->parent[$id] != -1)
{
$ ID = $親[$this->レイヤー[$id]] = $this->親[$id];
}

ksort($parent);
リセット($parent);

$parent を返します。
} // end func

/**
* 短い説明。 
*
* 詳細説明
* @param なし
* @global なし
* @since 1.0
* @access private
* @return void
* @update 日付時刻
   */
function getChild ($id)
{
return $this->child[$id];
} // 関数を終了します


/**
* 短い説明。 
*
* 詳細説明
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update 日付時刻
   */
function getChilds ($id = 0)
{
$child = array($id);
$this->getList($child, $id);

$child を返します。
} // func を終了
} // クラスを終了

?>


使用方法

PHP代:

复制代码代码如下:

//new Tree(ルートディレクトリの名前);
//ルートディレクトリのIDは自動的に0に割り当てられます
$Tree = new Tree('ルートディレクトリ');

//setNode(ディレクトリID, 上位ID, ディレクトリ名);

$Tree->setNode(1, 0, 'ディレクトリ1');
$Tree->setNode(2, 0, 'ディレクトリ2'); ;
$Tree->setNode(3, 0, 'ディレクトリ 3');
$Tree->setNode(4, 3, 'ディレクトリ 3.1');ディレクトリ 3.2');
$Tree->setNode(6, 3, 'ディレクトリ 3.3');
$Tree->setNode(7, 2, 'ディレクトリ 2.1')​​; , 2, 'ディレクトリ 2.2');
$Tree->setNode(9, 2, 'ディレクトリ 2.3');
$Tree->setNode(10, 6, 'ディレクトリ 3.3.1'); ->setNode(11, 6, 'ディレクトリ 3.3.2');
$Tree->setNode(12, 6, 'ディレクトリ 3.3.3');
//getChilds (指定されたディレクトリ ID);
// ディレクトリが指定されていない場合は、ルート ディレクトリから開始します
$category = $Tree->getChilds();
//Traverse Outputforeach ($category as $key=>$id)

{
echo $Tree->getLayer($id, '|-').$Tree->getValue($id). "
"
}




PHP 無制限分類-bkJia コード

コードをコピーします

コードは次のとおりです:

//無制限のカテゴリ、サブカテゴリからすべての親カテゴリを検索//$id サブカテゴリIDfunction php100_xd($id){
$sql ="select * from fl where id='$id'";
$q=mysql_query($sql);
$rs=mysql_fetch_array($q);
$rs['fid']==0 ? "" : fl($rs['fid']);
echo $rs['name']."-";
}

//親クラスの下にあるすべてのサブクラスを読み取ります
//トップレベルは何をしますか$f のカテゴリは? ,$s スタイル
function php100_dx($f=0,$s=""){
$sql="select * from fl where fid=$f";
$q=mysql_query($ sql);
$s= $s."-";
while($rs=mysql_fetch_array($q)){
echo "
$s".$rs['name'];
flt($ rs['id'], $s);
}
}






http://www.bkjia.com/PHPjc/317685.html
www.bkjia.com

tru​​e

技術記事次のようにコードをコピーします: ?php /*** @作者YangHuan * @datetime * @version 1.0.0*/ /** * 簡単な説明 * * 詳細説明 * @author * @version 1.0 * @copyright * @acce...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。