ホームページ  >  記事  >  バックエンド開発  >  php無限レベルSelectTree class_PHPチュートリアル

php無限レベルSelectTree class_PHPチュートリアル

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

コードをコピーします 如 コードは次のとおりです: /*
作成者: nick
date: 2009.05.17
関数: Selettree を生成する
プロパティ:
$ result results
$ ID_Field ID フィールド
$ Parent_field 親 ID ID フィールド
$ option_text オプションの表示名
$select_name ドロップダウン メニューの名前
$elected デフォルトで選択されている
$no_top 最上位のオプションが必要かどうか
$level レイヤーの深さ
$parent_id 同じレイヤー内の ID
*/
class SelectTree{
パブリック$結果;
パブリック$選択_フィールド;
パブリック$親_id;
function __construct( $result,$id_field,$parent_field,$option_text,$select_name='',$elected=0,$no_top=0,$level=0,$parent_id=0){
$this->result =$結果;
$this->id_field =$id_field;
$this->select_name =$select_name; this->選択されました
$this->no_top =$no_top;
$this->parent_id =$parent_id; self::getArray ();
/*
関数: ツリーの 2 次元配列を返す
*/
function getArray(){
$arrays=array()
while($row=mysql_fetch_array($this->) ;result)){
$arrays[$row[$this->parent_field]][$row[$this->id_field]]=$row;
}
return $arrays}
/*
関数: SelectTree を取得します
* /
function getSelectTree(){
$tree = ''
return $tree ;
/*
関数: ツリー構造を再帰的に構築します
*/
function buildTree($array,&$tree,$option_value,$option_text,$selected,$level=0,$parent_id=0){
if(is_array( $array[$parent_id])){
for($i=0;$i$space .= ' '; //オプションのインデントの深さ
foreach($array[$parent_id] as $ key => ; $value){
if($value[$option_value] == $selected){
$tree .= '";
}else{
$tree .= '"
}
$tree .=self::buildTree($array,&$tree,$option_text,$selected, $level+ 1,$key);
}
}else{
$tree .= ''
}
}
}
/************************************************* **************************/
header("CONTENT-TYPE:TEXT/HTML;CHARSET =UTF -8");
mysql_connect("localhost","root","root");
mysql_select_db("tree");
mysql_query('set names utf8');
$result = mysql_query("select * TVmenu から ");
$tree=new SelectTree($result,'id','bid','name','tree');
echo $tree->getSelectTree();






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

www.bkjia.com

tru​​e

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

技術記事

次のようにコードをコピーします。 /* author: nick date: 2009.05.17 Function: SeletTree 属性の生成: $result 結果セット $id_field 自身の ID フィールド $parent_field 親クラス ID フィールド $option_text オプション...


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。