Reference: http://www.phpchina.com/bbs/thread-25541-1-15.html
In this example, Eval is used to store array elements on a pay-as-you-go basis for unlimited classification.
- Class m_layer {
- /**
- * Unlimited Classification
- *
- * @ Param object $ MDB
- * @ Param string $ table
- * @ Param string $ label_colum
- * @ Return Array
- */
- Function get_layer ($ layer, $ label_colum = 'label '){
- $ New_layer = array ();
- If (! Is_array ($ layer) |! Is_array (current ($ layer) |! Array_key_exists ($ label_colum, current ($ layer ))){
- Return array ();
- }
- Foreach ($ layer as $ row ){
- $ Row_str = '';
- Foreach ($ row as $ key => $ value ){
- $ Row_str. = ", '$ key' =>' $ value '";
- }
- $ Row_str = "array (". substr ($ row_str, 1 ).')';
- $ Label_arr = explode ('-', $ row [$ label_colum]);
- $ Layer_str = '$ new_layer ';
- Foreach ($ label_arr as $ label ){
- If (intval ($ label )){
- $ Layer_str. = "['child '] [' $ label']";
- }
- }
- Eval ("$ layer_str = $ row_str; echo strip_tags (/"/$ layer_str =/$ row_str /");");
- Echo '<br/> ';
- }
- Return $ new_layer ['child '];
- }
- /**
- * Getting a new layer is equivalent to mysql_insert_id.
- *
- * @ Param object $ MDB
- * @ Param string $ table
- * @ Param string $ father_label
- * @ Param string $ label_colum
- * @ Return Array
- */
- Function get_new_layer ($ mdb, $ table, $ father_label, $ label_colum = 'labe '){
- $ COUNT = $ father_label? Count (explode ('-', $ father_label): 0;
- $ Size = strlen ($ father_label );
- $ SQL = "select $ label_colum from $ table where left ($ label_colum, $ size) = '$ father_label' order by $ label_colum DESC ";
- $ Rs = $ MDB-> fetch_row ($ SQL );
- $ Last_label_arr = explode ('-', $ Rs [$ label_colum]);
- $ Last_label = '';
- For ($ I = 0; $ I <= $ count; $ I ++ ){
- $ Last_label. = '-'. $ last_label_arr [$ I];
- }
- $ Last_label = substr ($ last_label, 1 );
- $ New_label = explode ('-', $ last_label );
- $ Num = array_pop ($ new_label );
- $ New_label = implode ('-', $ new_label). '-'. ++ $ num;
- Return $ new_label;
- }
- }
- $ Label [] = array ('name' => 'family of games ', 'lay' => '01 ');
- $ Label [] = array ('name' => 'Online game', 'lay' => '01-01 ');
- $ Label [] = array ('name' => 'kart ', 'lay' => '01-01-01 ');
- $ Label [] = array ('name' => 'bubble thup', 'lay' => '01-01-02 ');
- $ Label [] = array ('name' => 'standalone game', 'lay' => '01-02 ');
- $ Label [] = array ('name' => 'resident evil ', 'lay' => '01-02-01 ');
- $ Label [] = array ('name' => 'death squads ', 'lay' => '01-02-02 ');
- $ Label [] = array ('name' => 'super Mary', 'lay' => '01-02-02 ');
- $ Label [] = array ('name' => 'entertainment & Leisure ', 'lay' => '02 ');
- $ Label [] = array ('name' => 'music home', 'lay' => '02-01 ');
- $ Label [] = array ('name' => 'Jay Chou ', 'lay' => '02-01-01 ');
- $ Label [] = array ('name' => 'Liu ruoying ', 'layer' => '02-01-02 ');
- $ Label [] = array ('name' => 'scholarly door', 'lay' => '02-02 ');
- $ Label [] = array ('name' => 'fiction ', 'lay' => '02-02-01 ');
- $ Label [] = array ('name' => 'novels ', 'lay' => '02-02-01-01 ');
- $ Label [] = array ('name' => 'short story ', 'lay' => '02-02-01-02 ');
- $ Label [] = array ('name' => 'journal ', 'lay' => '02-02-02 ');
- $ Layeray = m_layer: get_layer ($ label, 'layer ');
- Print_r ($ layeray );
- ?>