A PHP/MySQL HierMenus Generation Interface - (3/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.
Save hm4_interface.php in the same directory as the HierMenus programs.
Open LoadMe.html in notepad or your favorite editor. Insert the following just before the end SCRIPT Tag -
//-></SCRIPT>
. Save the file as LoadMe.php (NOT .html).//===========Include this before the end Script Tag=====
<?
include ("hm4_interface.php");
?>
//==================Include upto this===================
I have used the LoadMe.html as this is given with the HierMenus pack. You may call it index.html or any other name for the page where you want the menus to appear. But the file must have the required portions from LoadMe.html with the above changes and it must be saved as a .php file.Open hm4_interface.php in the editor, configure it as follows, and save the file:
//===========================CONFIGURE==========================
//==============Input the following details as shown============
$config->dbhost = " MySQL host here e.g. www.dbhost.com ";
$config->dbname = " Database name here ";
$config->dbuser = " Your user name here ";
$config->dbpass = " Your password here ";
//================Do not make any change from here==============You do not need the "HM_Arrays.js" file any more. Instead, you will have to build up the tables with all data for parameters as well as tree information. Erase the following line in HM_Loader.js or if you feel shaky at least comment it out for the time being.
document.write("<SCR" + "IPT LANGUAGE='JavaScript1.2' SRC='HM_Arrays.js' TYPE='text/javascript'><\/SCR" + "IPT>");
Open LoadMe.php or index.php (whatever you called it) in your browser.
Play around with the data in the database changing color, dimension, text etc. reloading the page every time to see the menus dance to your tune. Great fun isn't it?
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:
$param
- This array temporarily stores the parameter name-value pairs in a two-dimension array.$levels
- This stores all the menu suffixes e.g. 1, 2, 3_1_2 etc.$data
- This array takes a snap shot of themenu_tree
in a two-dimensional array for future use.
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.
[previous] [next] |
Written by J. Chakrabarty and
Created: September 24, 2001
Revised: September 24, 2001
URL: https://webreference.com/programming/php/hierphp/3.html