[Top]
   
Look at also other topic groups, which I have done!
You can return to this topic group by using this menu and the link Table of topic groups in the top of the page.
 
 
Search:
[Help]

DynamicMenus

I have made dynamic menus quite static. They can be generated also dynamic with PHP. Compared to JavaScript encoding the advance is that menus exist even if the JavaScript support has been disabled.

The end and bottom of the dynamic menus is reasonable to write as ordinary HTML and or write it with functions. Functions are not however necessary in this purpose.

Menu items

Functions are always needed only in generating menu items. PHP generates the structure of menus items and the content of each menu item has been defined elsewhere by using variables. For example the following mItem() function generates sub-menu items:

function mItem(){

global $classD,$idD,$mOver,$classD2,$classA,$idA,$title,$href,$name;

print "<div class=\"".$classD."\" ".$idD." onmouseover=\"".$mOver."><div class=\"".$classD2."\"><a class=\"".$classA."\" id=\"".$idA."\" title=\"".$title."\" href=\"".$href."\"\">".$name."</a></div></div>";
}
Using individual global variables

In this connection has been used individual global variables. Variables don't need to list neither inside the argument lists of the function definition nor function calls. All variables are not necessary to define but if some of them has been defined the defined value concerns automatic all preceding function calls if variables has not been redefined. In the end of the function definition file can be set some default values and a function, which resets default values, for example:

$classD="noArrow";
$classD2="itemD";
$idD=""; /* The value of the variable must write in the format $idD="id=\"idValue"\"; because the value must be unique and empty value is illegal. The default value can't be empty attribute but instead not at all attribute. By Other variables generated id-attributes are such, which have been generated for all elements and the author must always set unique values for them. This problem can be avoided by using an additional function (I have later a model how to do that). */
$classA="itemA";
$mOver="";

function setDefaults(){

global $classD,$idD,$classD2,$classA,$mOver;

$classD="noArrow";
$idD="";
$classD2="itemD";
$classA="itemA";
$mOver="";
}

Below is an example, which defines a sub-menu. Because the definition has an exceptional class values in the end is the function, which returns original values:

$idA="uniqueId";
$title="Some link";
$href="link1.php";
$classD2="someClass";
$name="Link 1";

smItem();setDefaults();

I define in general four from the nine variables, which define attribute values. For example

$idA="CSSCover";
$title="CSS-oppaan aloitussivu";
$href="
index1.php3";
$name="Aloitussivu";

mItem();
generates together with the function mItem() the following final source code:
<div class="noArrow" ><div class="itemD"><a class="itemA" id="CSSCover" title="CSS-oppaan aloitussivu" href=" index1.php3" onmouseover="">Aloitussivu</a></div></div>

Instead of listing variable before the function call they could also be set inside function calls separating variables with commas (mItem($idA="CSSCover",...)).

The problem of the code above is that it generates much empty onmouseover attributes. They can be taken off with more complex encoding, but this is not essential. Concerning those attributes and some id attributes it could be possible to check, if variables are empty. If they would be empty, attributes would not be generated at all. Otherwise would be printed attributes and values of the attributes. Below is the code to implement this:

checkIfEmpty(){

	global $mOver,$idD;
	
	if (empty($idD)){ } /* The function definition can be empty. */
	
	else {
		print " id=\"".$idD."\"";
	}
	
	if (empty($mOver)){ }
	
	else {
		print " onmouseover=\"".$mOver."\"";
	}
}


function mItem(){
...
	print "<div class=\"".$classD."\"";
	checkIfEmpty();
	print "> class=\"".$classD2."...";

Using arrays

The system, which I have proposed above doesn't follow in one respect recommended programming conventions. The information of variables leak leak from one target to another targets. Instead of using individual variables could be use an array (for example $aItem) and for sub-variables (they are inside brackets), which correspond above used ordinary variables at the following way:

function mItem(){

global $aItem;

... ".$aItem["classD"]." ... ".$aItem["href"]." ... ".$aItem["name"]"...

Values of sub-variables could be defined with the following way by using the => operator:

mItem($aItem = array("classD" => "noArrow", ..., "href" => " index1.php3", "name" => "Aloitussivu", ... ));

Now values of individual variable don't leak but the disadvantage is that all individual values must be redefined inside the array. Of courser it is possible to use functions, which check if attribute values are empty (they are empty if values has not been set at all) and if they are empty predefined default values has been set. In my mind would be more reasonable to create an own array for non-empty default values, for example:

$aItemDefaults = array("classD" => "noArrow", "classD2" => "itemD", "classA" => "itemA", "href" => "#");

These default values can be combined with the array $aItem by using the function array_merge(). Below is an example:

mItem(array_merge($aItemDefaults,$aItem = array(
"idD" => "GenericItem",
"idA" => "GenericA",
"classD" => $arrow,
"title" => "Generic",
"mOver" => "hideThlMenus(); MM_showHideLayers('indexPages','','hide','allSites','','hide','Generic','','show'...);"
)));

If values are different values of the latter array wins. In the case above "classD => $arrow" wins the default setting "classD" => "noArrow".

In all previous mentioned example usages of arrays have been created more code than using individual variables. Indeed I have seen compressed JavaScript arrays, where arrays have been defined so that the user must only add information between commas (for example ,,,noArrow,,,). I believe that it could be possible to create the same kind of solution also by using PHP. The problem of those kinds of solutions is that it is difficult to remember, what value belong between each comma. That's why I don't recommend to use so compressed code. In my mind the easiest system for amateurs is to use individual variables. Maintaining complex arrays is for professionals.

Container blocks

The container elements of sub-menus is reasonable to set at least one variable, which sets an unique id-attribute for the sub-menu. In the example below I have used individual variables (also arrays could be used):

function smTop(){

global $id,...;

print "<div id=\"".$id."\" class=\"pageGroup\"...
}

The related function call is the following:

...
$id = "MainPages";

...

smTop();

With these solution it is possible to automatise generating menu items, but generating menu blocks and the entire menu is just partially automated.

Presentations

I have defined almost all presentational features in outside CSS-files. The most remarkable exception is that I have defined the height of some elements by using PHP and style attributes. The author defines the content height of the sub-menus by using the variable $iHeightMI (the name comes from words integer Height Menu Items). That content height defined also height of shadows (height values might be today a little bit different):

function smTop(){

global ...,$iHeightMI;

print "<div id=\"".$id."\" class=\"pageGroup\" style=\"height:".($iHeightMI+10)."px;\">
...
}

function smBottom(){

global $iHeightMI;

print "</div><div class=\"shadowBase\" style=\"height:".($iHeightMI+122)."px;\"><div class=\"shadow\" style=\"height:".($iHeightMI+18)."px;\"> </div></div></div>";
}

The weakness of this solution is that the author must manually set the value of the variable $iHeightMI. If a sub-menu would be an array, it would be possible to count the quantity of array items. That values should be multiplied with the value of the height of one menu (in my case the value is 15px). There are a lot of other presentational features in my solution which could be automated.

The positioning of all menus has been defined in the beginning of the ../Css/dynamicMenus.css[S] CSS-file. It defines also generic presentational features of dynamic menus. It is easy to edit the CSS file so that the main menu is either vertical or horizontal.

Because I have used dynamic menus also as static link tables, setting menus inside table cells and rows can't be done automatic by using PHP-functions. If dynamic menus are not needed as static link tables, all menu blocks related elements can be generated by PHP-scripts.

I made for one menu a template[S], which is related with the definition file of used functions as a text file[S]. If you don't need table cells around menus, delete all source code, which are related with table elements (TABLE, TR and TD).

By using CSS + PHP it is possible to separate from each others the basic structure, presentational details, JavaScript-encoding and the content of sub-menus.

With some further evaluation my solution can be converted using databases, where the values of generated attributes have been get from databases instead of *.inc files. If each menu is an own table, it is possible to fetch the whole content of a table and then for example by using the while-loop and list() at the following way:

<?php
result = mysql($conn, "SELECT classD, idD, mOver,... FROM menutableMain");

while (list($idA, $name,...) = mysql_fetch_row($result)) {
print ...

}
?>

Special headers and empty menu items must however forgot because they would interrupt handling of loops. Or they must be build at the same formula, where ordinary menu items has been build.

It is possible to create a form, which the menu can be updated by using any Web browser. Some form fields should be required and some other implied. I don't have - at least just now - skills to do that. And If I had I would not handle database usage in my pages.

If dynamic menus seems to be downloaded too slow, menus can precompile by making a new *.php file, which PHP compiles. The compiled menu file can be saved with it original name, but into another folder. From that folder the compiled file can be sent into the Internet.

[Top]