A PHP/MySQL HierMenus Generation Interface - (3/4) | WebReference

A PHP/MySQL HierMenus Generation Interface - (3/4)

To page 1To page 2current pageTo page 4
[previous] [next]

A PHP - MySQL Interface for HierMenus 4

Installing the Interface

Step 2: Installing the PHP Interface

The hard part is over once you have created the database tables and populated them. The rest is taken care of by the PHP interface, which I have called "hm4_interface.php." This is an include file to keep the HierMenus files clean. Later, I will briefly explain how the PHP routine works. However, for using this interface all you need to do are the following. It is assumed you are running your site on a host having PHP and MySQL support properly configured.

Brief explanation of how the hm4_interface.php works

The first part of the program configures the routine with necessary database access codes. This has already been explained earlier. Then a few PHP arrays and variables are initialized. The notable among them are:

The rest of the variables and arrays are required for the program logic as explained subsequently.

A database connection is set up using the details in the configuration variables. The menu parameters are read and stored in the $param arrays against the corresponding array suffixes. The menu_tree table is then read and while storing the records in the $data array a few things are done.

$i = 0;
while($myrow = mysql_fetch_array($result)) {
	$thislevel = trim($myrow["array_suffix"]);
	for ($n=0; $n < sizeof($levels); ++$n) {
		if ($thislevel == $levels[$n]) {
			$new_level = false;
			break;
		}
	}
	if ($new_level) {
		++$level_no;
		$levels[$level_no-1] = $thislevel;
		echo "HM_Array" . $thislevel . " = new Array();\n";
	}
	$new_level = true;
	$x = 0;
	$data[$link_no][$x]  = trim($myrow["array_suffix"]);
	++$x;
	$data[$link_no][$x]  = trim($myrow["link_name"]);
	++$x;
	.
	.
	.
	++$i;
}
mysql_free_result($result);

The variable $new_level is set to "true" at the beginning. Each pass through the outer "while" loop, a record of the menu_tree table is read. The first "for" loop checks whether the array suffix in that record is already present in the $levels array. If present, it sets $new_level to "false." The next "if" statement checks $new_level and if it is "true" an element is added to the $levels array. It also simultaneously initiates an HM_Array with correct suffixes, e.g. HM_Array1, HM_Array2 etc. $new_level is set back to "true" for the next pass. This way the $levels array is built up with all exclusive array suffixes in the database.

The outer "while" loop in the subsequent part of the program uses the $levels array for building the necessary JavaScript arrays with the correct array suffixes according to the needs of HierMenus.

$i = 0;
while ($i < sizeof($levels)) {
	$thislevel = $levels[$i];
	$has_param = false;
.
.
  ++$i;	
}

It first assumes there is no parameter for this menu level by setting $has_param to "false." The first "for" loop thereafter is as follows:

for ($n = 0; $n < sizeof($param); ++$n) {
  if ($param[$n][0] == $thislevel) {
     echo "HM_Array" . $thislevel . "[" . $first . "] = new Array()\n";
     for ($x=1; $x < sizeof($param[$n]); ++$x) {
       $thisparam = $param[$n][$x];
       $sub_element = $x - 1;
       echo "HM_Array" . $thislevel . "[" . $first . "][" . $sub_element . "]  = \"" . $thisparam. "\"\n"; 
     }
     $has_param = true;
     break;
  }
}

$param array is checked to verify if the current level has any parameters associated with it. If so, it fills up the first element of the HierMenus array with the parameter values. Otherwise, the first element is made a blank.

The next "while" loop then fills up the other elements of the HierMenus array with the menu tree information from the $data array.

Once the HierMenus arrays are correctly formed, the control is handed over to the wonderful powers of the HierMenus application.


To page 1To page 2current pageTo page 4
[previous] [next]

Written by J. Chakrabarty and
Created: September 24, 2001
Revised: September 24, 2001


URL: http://webreference.com/programming/php/hierphp/3.html