Location: PHPKode > scripts > TreeMenu > treemenu/Set_Menu.php
<?PHP
CLASS tree_menu {
FUNCTION sort_array( $array_tmp ) {		// -v- FUNCTION sort_array( $array_tmp )
	//ECHO "Count Before = ".COUNT( $array_tmp )."<BR>";
	$array_tmp_tmp = $array_tmp;
	WHILE  ( LIST( $key, $val ) = EACH( $array_tmp_tmp ) ) {
		$array_tmp[ $key ][ "sort_id" ] = ( $val["parent_id"].$val["order"] ) * 1;
	}
	UNSET( $key, $val );
	//ECHO "Count Before = ".COUNT( $array_tmp_tmp )."<BR>";
	RETURN $array_tmp;
}														// -^- FUNCTION sort_array( $array_tmp )
//------------------------------------------------------------------------------------------------------
/*
	Create by ROZEBAB 31/08/2004
	===============================================================================
	¿Ñ§¡ìªÑ¹·Õèãªé㹡ÒèѴ¡Òà ¢éÍÁÙŢͧÃÒ¡ÒÃàÁ¹Ù ãËéÍÂÙèã¹ÃٻẺ·Õè¾ÃéÍÁ¨Ð¹Óä»ãªé㹡ÒÃÊÃéÒ§ Tree Menu

	¤èÒ·Õè¶Ù¡Ê觤׹¨Ò¡¿Ñ§¡ìªÑ¹ ÁÕ 2 Ẻ
		1. Ẻ Line		- ¢éÍÁÙÅã¹ÃٻẺ Line ¨Ðà»ç¹¢éÍÁÙÅ·ÕèµèÍà¹×èͧ¡Ñ¹µÅÍ´ àËÁÒÐ㹡ÒÃáÊ´§àÁ¹Ù·ÕèàËç¹·Õèà´ÕÂÇ·Ñé§ËÁ´
		2. Ẻ Link		- ¢éÍÁÙÅã¹ÃٻẺ Link à»ç¹¢éÍÁÙÅ·Õè¶Ù¡¨Ñ´ãËéÍÂÙèµÒÁ¡ÅØèÁ â´Âãªé ParentID à»ç¹ÃËÑÊ¡ÅØèÁ
		2. Ẻ Table	- ¢éÍÁÙÅã¹ÃٻẺ Table à»ç¹¢éÍÁÙÅ·ÕèÍÂÙèã¹ÃٻẺµÒÃÒ§ ¾ÃéÍÁ·Õè¹Óä»áÊ´§ã¹ <TABLE> ¢Í§ HTML
	===============================================================================
 */
 FUNCTION set_menu ( $data_menu_array, $param1 = FALSE, $param2 = FALSE ) {	
		// -v- FUNCTION set_menu ( $data_menu_array, $param1 = FALSE, $param2 = FALSE )
	/*
		--------------------------------------------------------------------------------------------------------------------------------------
		Meaning of Variables ( ¤ÇÒÁËÁÒ¢ͧµÑÇá»Ã )
			- $data_menu_array  ¤×Í ¢éÍÁÙŢͧÃÒ¡ÒÃàÁ¹Ù·Õè¹ÓÁÒ·Ó¡Òà Set Menu à»ç¹¢éÍÁÙÅã¹ÃٻẺ¢Í§ÍÐàÃÂì ( Array ) ÁÕ Properties ´Ñ§¹Õé
										1 ) [ menu_id ]		¤×Í ID ¢Í§ÃÒ¡ÒÃàÁ¹Ù   áÅÐ ÃÒ¡ÒÃàÁ¹ÙµÑÇááµéͧäÁèà·èҡѺ 0 ( ÈÙ¹Âì )
										2 ) [ parent_id ]	¤×Í ID ¢Í§¡ÅØèÁ·ÕèÃÒ¡ÒÃàÁ¹Ù¹Ñé¹ÍÂÙè ¶éÒÃÒ¡ÒÃã´äÁèÁÕ¡ÅØèÁ¨Ð¶Ù¡¨Ñ´ãËéÍÂÙè·Õè¡ÅØèÁ 0 â´ÂÍѵâ¹ÁѵÔ
										3 ) [ order ]			¤×Í ËÁÒÂàÅ¢ÅӴѺ¢Í§ÃÒ¡ÒÃã¹áµèÅСÅØèÁ
										*** ËÁÒÂà赯 0 ¤×Í ¤èÒ·Õè¨Ð¶Ù¡ãªé㹿ѧ¡ìªÑ¹ ´Ñ§¹Ñé¹¢éÍÁÙ§·ÕèÊè§ÁÒ¨Ö§ËéÒÁÁÕ¤èÒ 0 
-----------********************ÍÒ¨¨Ðà¢Õ¹à¾×èÍ»éͧ¡Ñ¹¤èÒ 0**************************--------
			- $param1 ¤×Í  ¡Ó˹´ÃٻẺ¢éÍÁÙÅ·Õèµéͧ¡ÒÃãËé¿Ñ§¡ìªÑ¹ ¤×¹¤èÒ¡ÅѺ
			- $param2 ¤×Í	-----------------------------------------
		--------------------------------------------------------------------------------------------------------------------------------------
	*/
	//$data_menu_array_tmp = sort_array( $data_menu_array );
	$data_menu_array_tmp = $data_menu_array;
	$data_menu_array2		= ARRAY();
	$child_of_menu			= ARRAY();
	$child_of_menu[ 0 ]		= ARRAY();				// ¡ÅØèÁáá¢Í§ÃÒ¡ÒÃàÁ¹Ù àÃÔèÁ 0
	$table_of_menu			= ARRAY();				// µÒÃÒ§à¡çºµÓá˹觢ͧÃÒ¡ÒÃàÁ¹ÙáµèÅÐÃÒ¡ÒÃ
	$table_of_menu2			= ARRAY();				// µÒÃÒ§à¡çºµÓá˹觢ͧÃÒ¡ÒÃàÁ¹ÙáµèÅÐÃÒ¡Òà 2
	$table_of_menu_by_col			= ARRAY();		// µÒÃÒ§à¡çºµÓá˹觢ͧÃÒ¡ÒÃàÁ¹ÙáµèÅÐÃÒ¡Òà áµèàÃÕ§µÒÁ ¤ÍÅÑÁ¹ì
						/*
								0  Start  Join Top
								1   I		Line
								2   +		Join
								3    L		Join Botttom
						*/
	$number_of_menu		= COUNT($data_menu_array_tmp) + 1;
	
	/*
	ECHO "<TABLE Border = 1>\n";
	ECHO "<TR>\n";
	ECHO "<TD>ID</TD>\n";
	ECHO "<TD>Menu ID</TD>\n";
	ECHO "<TD>Parent ID</TD>\n";
	ECHO "<TD>Order</TD>\n";
	ECHO "<TD>Sort ID</TD>\n";
	ECHO "<TD>Name</TD>\n";
	ECHO "<TD>L Link</TD>\n";
	ECHO "<TD>R Link</TD>\n";
	ECHO "<TD>Parent ID</TD>\n";
	ECHO "<TD>Child ID</TD>\n";
	ECHO "</TR>\n";
	
*/
	WHILE ( LIST( $key, $val ) = EACH( $data_menu_array_tmp ) ) {	// -v- WHILE ( LIST( $key, $val ) = EACH( $data_menu_array_tmp ) )
		// ÊÃéÒ§¡ÅØèÁ¢Í§ÃÒ¡ÒáÒÃàÁ¹Ù â´Âãªé ID ¢Í§ Parent à»ç¹ ID ¢Í§ ¡ÅÙèÁ
		IF ( !ISSET( $child_of_menu[ $val["parent_id"] ] ) ) {		// -v- IF ( !ISSET( $child_of_menu[ $val["parent_id"] ] ) )
			$child_of_menu[ $val["parent_id"] ] = ARRAY();
		}																		// -^- IF ( !ISSET( $child_of_menu[ $val["parent_id"] ] ) )

		IF ( !ISSET( $child_of_menu[ $val["parent_id"] ][ $val[ "order" ] ] ) ) {		
			// -v- IF ( !ISSET( $child_of_menu[ $val["parent_id"] ][ $val[[ "order" ] ] ) )
			$child_of_menu[ $val["parent_id"] ][ $val[ "order" ] ]  = $key;
		}	// -^- IF ( !ISSET( $child_of_menu[ $val["parent_id"] ][ $val[[ "order" ] ] ) )
/*
		$data_menu_array2["menu_id"]	[ $key ]	= $val["menu_id"];
		$data_menu_array2["parent_id"]	[ $key ]	= $val["parent_id"];
		$data_menu_array2["order"]		[ $key ]	= $val["order"];
		$data_menu_array2["sort_id"]		[ $key ]	= $val["sort_id"];
		$data_menu_array2["name"]		[ $key ]	= $val["name"];
		$data_menu_array2["l_link"]				[ $key ]	= NULL;
		$data_menu_array2["r_link"]				[ $key ]	= NULL;
		$data_menu_array2["parent_id_link"]	[ $key ]	= NULL;
		$data_menu_array2["child_id_link"]		[ $key ]	= NULL;
*/
/*
		ECHO "<TR>\n";
		ECHO "<TD>".$key."</TD>\n";
		ECHO "<TD>".$val["menu_id"]."</TD>\n";
		ECHO "<TD>".$val["parent_id"]."</TD>\n";
		ECHO "<TD>".$val["order"]."</TD>\n";
		ECHO "<TD>".$val["sort_id"]."</TD>\n";
		ECHO "<TD>".$val["name"]."</TD>\n";
		ECHO "<TD>".$val["l_link"]."</TD>\n";
		ECHO "<TD>".$val["r_link"]."</TD>\n";
		ECHO "<TD>".$val["parent_id_link"]."</TD>\n";
		ECHO "<TD>".$val["child_id_link"]."</TD>\n";
		ECHO "</TR>\n";
*/
	}																						// -^- WHILE ( LIST( $key, $val ) = EACH( $data_menu_array_tmp ) )
//	 ECHO "</TABLE>";
	UNSET( $key, $val );

	$child_of_menu_tmp = $child_of_menu;
	WHILE ( LIST( $key, $val ) = EACH( $child_of_menu_tmp ) ) {
			// -v- WHILE ( LIST( $key, $val ) = EACH( $child_of_menu_tmp ) )
			$val_child_tmp = $val;
		//	ECHO $key." =>> ".$val." = ".$data_menu_array[ $key ][ "name" ]." = ".COUNT( $val_child_tmp )."<BR>";
			KSORT( $val_child_tmp );
			RESET( $val_child_tmp );
		
			$l_link					= NULL;  // Left Link
			$r_link					= NULL;  // Right Link
			$parent_id_link		= NULL;  // ParentID Link
			$child_id_link			= NULL;  // ChildID Link

			WHILE ( LIST( $key1, $val1 ) = EACH( $val_child_tmp ) ) {		// -v- WHILE ( LIST( $key1, $val1 ) = EACH( $val_child_tmp ) )
				
				IF  ( ISSET( $data_menu_array[ $val1 ]	 ) ) {		// -v- IF  ( ISSET( $data_menu_array[ $val1 ] ) )
						$data_menu_array[ $val1 ][ "l_link" ]				= $l_link;
				}																// -^- IF  ( ISSET( $data_menu_array[ $val1 ] ) )
		
				IF ( !IS_NULL( $l_link ) ) {	// -v- IF ( !IS_NULL( $l_link ) )
					IF  ( ISSET( $data_menu_array[ $l_link ] ) ) {		// -v- IF  ( ISSET( $data_menu_array[ $l_link ] ) )
						$data_menu_array[ $l_link ][ "r_link" ]					= $val1;
					}																// -^- IF  ( ISSET( $data_menu_array[ $l_link ] ) )
				}									// -^- IF ( !IS_NULL( $l_link ) )

				IF  ( ISSET( $data_menu_array[ $val1 ] ) ) {		// -v- IF  ( ISSET( $data_menu_array[ $val1 ] ) )
						$data_menu_array[ $val1 ][ "parent_id_link" ]		= $key;
				}																// -^- IF  ( ISSET( $data_menu_array[ $val1 ] ) )
				
				IF  ( ISSET( $data_menu_array[ $key ] ) ) {		// -v- IF  ( ISSET( $data_menu_array[ $key ][ "child_id_link" ] ) )
					IF ( IS_NULL( $data_menu_array[ $key ][ "child_id_link" ] ) ) {		
							// -v- IF ( IS_NULL( $data_menu_array[ $key ][ "child_id_link" ] ) )
						$data_menu_array[ $key ][ "child_id_link" ]	= $val1;
					}		// -^- IF ( IS_NULL( $data_menu_array[ $key ][ "child_id_link" ] ) )
				}															// -^- IF  ( ISSET( $data_menu_array[ $key ][ "child_id_link" ] ) )

			//	ECHO "====================".$key1." =>> ".$val1." = ".$data_menu_array[ $val1 ][ "name" ]."<BR>";

				$l_link = $val1;
			}																					// -^- WHILE ( LIST( $key1, $val1 ) = EACH( $val_child_tmp ) )
	}		// -^- WHILE ( LIST( $key, $val ) = EACH( $child_of_menu_tmp ) )
	UNSET( $key, $val );
	
	$child_of_menu_start_tmp = $child_of_menu[ 0 ];
//	KSORT( $child_of_menu_start_tmp );
//	RESET( $child_of_menu_start_tmp );
	LIST( $key, $val ) = EACH( $child_of_menu_start_tmp );
	
	$data_menu_array[ 0 ][ "menu_id" ]				= 0;
	$data_menu_array[ 0 ][ "parent_id" ]				= NULL;
	$data_menu_array[ 0 ][ "name" ]					= NULL;
	$data_menu_array[ 0 ][ "order" ]					= NULL;
	$data_menu_array[ 0 ][ "l_link" ]					= NULL;
	$data_menu_array[ 0 ][ "r_link" ]					= NULL;
	$data_menu_array[ 0 ][ "parent_id_link" ]		= NULL;
	$data_menu_array[ 0 ][ "child_id_link" ]		= $val;
	UNSET( $key, $val );
/*
	ECHO "<TABLE Border = 1>\n";
	ECHO "<TR>\n";
	ECHO "<TD>ID</TD>\n";
	ECHO "<TD>Menu ID</TD>\n";
	ECHO "<TD>Parent ID</TD>\n";
	ECHO "<TD>Name</TD>\n";
	ECHO "<TD>Sort ID</TD>\n";
	ECHO "<TD>Order</TD>\n";
	ECHO "<TD>L Link</TD>\n";
	ECHO "<TD>R Link</TD>\n";
	ECHO "<TD>Parent ID</TD>\n";
	ECHO "<TD>Child ID</TD>\n";
	ECHO "</TR>\n";

	$menu_data_array1_tmp = $data_menu_array;
	KSORT( $menu_data_array1_tmp );
	RESET( $menu_data_array1_tmp );
	WHILE ( LIST( $key, $val ) = EACH( $menu_data_array1_tmp ) ) {
			// -v- WHILE ( LIST( $key, $val ) = EACH( $menu_data_array1_tmp ) )
		ECHO "<TR>";
		ECHO "<TD>".$key."</TD>";
		ECHO "<TD>".$val[ "menu_id" ]."</TD>";
		ECHO "<TD>".$val[ "parent_id" ]."</TD>";
		ECHO "<TD>".$val[ "name" ]."</TD>";
		ECHO "<TD></TD>";
		ECHO "<TD>".$val[ "order" ]."</TD>";
		ECHO "<TD>".$val[ "l_link" ]."</TD>";
		ECHO "<TD>".$val[ "r_link" ]."</TD>";
		ECHO "<TD>".$val[ "parent_id_link" ]."</TD>";
		ECHO "<TD>".$val[ "child_id_link" ]."</TD>";
		ECHO "</TR>";
	}		// -^- WHILE ( LIST( $key, $val ) = EACH( $menu_data_array1_tmp ) )
	ECHO "</TABLE>";
	UNSET( $key, $val );
*/
//--------------------------------------------------------------------------------------------------------------------------
	//ECHO "<TABLE Border = 1>\n";
	$menu_data_array2_tmp = $data_menu_array;
	KSORT( $menu_data_array2_tmp );
	RESET( $menu_data_array2_tmp );
	$col_index = 0;
	$row_index = 0;
	$break_i=1;
	$loop_status = TRUE;
	$menu_pointer = 0;
	$menu_relation = "";
	$sign_no = "";
	WHILE ( $loop_status ) {
			// -v- WHILE ( $loop_status )

		$menu_pointer_tmp = $menu_pointer;
		$menu_relation_tmp = $menu_relation;

		IF ( !IS_NULL( $menu_data_array2_tmp[ $menu_pointer ][ "child_id_link" ] ) ) {
			$menu_pointer = $menu_data_array2_tmp[ $menu_pointer ][ "child_id_link" ];
			$menu_relation = "Child";
			IF ( !IS_NULL( $menu_data_array2_tmp[ $menu_pointer_tmp ][ "r_link" ] ) ) {
						// -v- IF ( !IS_NULL( $menu_data_array2_tmp[ $menu_pointer_tmp ][ "r_link" ] ) )
				$sign_no = "2";
			}ELSE {	// --- IF ( !IS_NULL( $menu_data_array2_tmp[ $menu_pointer_tmp ][ "r_link" ] ) )
				$sign_no = "3";
			}			// -^- IF ( !IS_NULL( $menu_data_array2_tmp[ $menu_pointer_tmp ][ "r_link" ] 

		}ELSE IF ( !IS_NULL( $menu_data_array2_tmp[ $menu_pointer ][ "r_link" ] ) ) {
			$menu_pointer = $menu_data_array2_tmp[ $menu_pointer ][ "r_link" ];
			$menu_relation = "Right";	$sign_no = "2";
		}ELSE {
			$menu_data_array2_tmp[ $menu_data_array2_tmp[ $menu_pointer_tmp ][ "parent_id_link" ] ][ "child_id_link" ] = NULL;
			$menu_pointer = $menu_data_array2_tmp[ $menu_pointer_tmp ][ "parent_id_link" ];
			$menu_relation = "Parent";
			$sign_no = "3";
		}
		
		SWITCH ( $menu_relation_tmp ) {	// -v- SWITCH ( $menu_relation_tmp )
			CASE "Child" :	$col_index++;
				BREAK;
			CASE "Right" :
				BREAK;
			CASE "Parent" :	$col_index--;
				BREAK;
		}												// -^- SWITCH ( $menu_relation_tmp )

		IF ( $menu_relation_tmp != "Parent" ) {
			$table_of__menu[ $row_index ]			= ARRAY();
			//ECHO "<TR>\n";
			FOR ( $i_col_index = 1; $i_col_index <= $col_index; $i_col_index++ ) {
					// -v- FOR ( $i_col_index = 1; $i_col_index <= $col_index; $i_col_index++ )
				IF ( $i_col_index == $col_index ) {	// -v- IF ( $i_col_index == $col_index )
					//ECHO "<TD><FONT Color=green>".$sign_no."</FONTt>Row =".$row_index."*".$menu_data_array2_tmp[ $menu_pointer_tmp ][ "menu_id" ]."=".$menu_data_array2_tmp[ $menu_pointer_tmp ][ "name" ]."=".$menu_pointer_tmp." => ".$menu_pointer." # ".$menu_relation_tmp."-".$menu_relation."++".$col_index."</TD>\n";

					//$table_of_menu[ $row_index ][ $i_col_index ][ "menu_id" ] = 1;
					$table_of_menu[ $row_index ][ $i_col_index ][ "menu_id" ]	= $menu_data_array2_tmp[ $menu_pointer_tmp ][ "menu_id" ];
					$table_of_menu[ $row_index ][ $i_col_index ][ "sign_no" ]		= $sign_no;

					$table_of_menu_by_col[ $i_col_index ][ $row_index ][ "sign_no" ] = $sign_no;
				}ELSE {										// --- IF ( $i_col_index == $col_index )
				//	ECHO "<TD>".$row_index.",".$i_col_index."</TD>\n";
					$table_of_menu[ $row_index ][ $i_col_index ][ "menu_id" ]	= NULL;
					$table_of_menu[ $row_index ][ $i_col_index ][ "sign_no" ]		= NULL;
				}												// -^- IF ( $i_col_index == $col_index )
			}		// -^- FOR ( $i_col_index = 1; $i_col_index <= $col_index; $i_col_index++ )
			//ECHO "<TD>menu_pointer = ".$menu_pointer."</TD>";
			//ECHO "</TR>\n";
			$row_index++;
		}
		
		//$break_i++;
		//IF ( $break_i == $number_of_menu ) { BREAK; }

		IF ( IS_NULL( $menu_pointer ) ) {  BREAK; }
	}		// -^- WHILE ( $loop_status )
	//ECHO "</TABLE>\n";
	UNSET( $key, $val );

	$old_row			= 0;
	$old_sign_no	= "";
	//ECHO "<TABLE Border = 1>\n";
	WHILE ( LIST( $key, $val ) = EACH( $table_of_menu_by_col )	) {
	//	ECHO "<TR>\n";
		$old_row			= 0;
		$old_sign_no	= "";
		WHILE ( LIST( $key1, $val1 ) = EACH( $val )	) {
		//	ECHO "<TD>&nbsp;\n";
		//	ECHO $key1." = ";
		//	ECHO $val1[ "sign_no" ];
		//	ECHO "</TD>\n";
			IF ( ( $old_sign_no != "" )  AND ( $old_sign_no != "3" ) ) {
				FOR ( $i_row = ( $old_row+1 ); $i_row <= ( $key1-1 ); $i_row++ ) {
					$table_of_menu[ $i_row ][ $key ][ "sign_no" ]		= "1";
				}
			}
			$old_row			= $key1;
			$old_sign_no	= $val1[ "sign_no" ];
		}
	//	ECHO "</TR>\n";
	}
	UNSET( $key, $val );
//	ECHO "</TABLE>\n";


	$table_of_menu_tmp = $table_of_menu;
	//ECHO "<TABLE Border = 1>\n";
	WHILE ( LIST( $key, $val ) = EACH( $table_of_menu_tmp )	) {
	//	ECHO "<TR>\n";
		WHILE ( LIST( $key1, $val1 ) = EACH( $val )	) {
	/*		
			ECHO "<TD>&nbsp;".$key." = <BR>";
			ECHO $val1[ "sign_no" ]." =&nbsp;<BR>";
			ECHO " &nbsp;Menu ID = ".$val1[ "menu_id" ]." =&nbsp;<BR>";
			ECHO " &nbsp;Parent ID = ".$data_menu_array[ $val1[ "menu_id" ] ][ "parent_id" ]." =&nbsp;<BR>";
			ECHO $data_menu_array[ $val1[ "menu_id" ] ][ "name" ];
			ECHO "</TD>\n";
		*/
			IF ( $val1[ "menu_id" ] > 0 )	{	// -v- IF ( $val1[ "menu_id" ] > 0 )
				$table_of_menu2[ $val1[ "menu_id" ] ][ "menu_id" ]			= $val1[ "menu_id" ];
				$table_of_menu2[ $val1[ "menu_id" ] ][ "parent_id" ]		= $data_menu_array[ $val1[ "menu_id" ] ][ "parent_id" ];
				$table_of_menu2[ $val1[ "menu_id" ] ][ "name" ]				= $data_menu_array[ $val1[ "menu_id" ] ][ "name" ];
			}											// -^- IF ( $val1[ "menu_id" ] > 0 )
		}	
		//ECHO "</TR>\n";
	}
//ECHO "</TABLE>\n";
	UNSET( $key, $val );
	UNSET( $key1, $val1 );

	SWITCH ( $param1 ) {	// -v- SWITCH ( $param1 )
		CASE "Link"		: RETURN $child_of_menu;						BREAK;
		CASE "Table2"	: RETURN $table_of_menu2;					BREAK;
		DEFAULT			: RETURN $table_of_menu;						BREAK;
	}								// -^- SWITCH ( $param1 )

 }		// -^- FUNCTION set_menu ( $data_menu_array, $param1 = FALSE, $param2 = FALSE )
}
?>
Return current item: TreeMenu