Sothink-Flash Movie Maker, SWF Decompiler, DHTML Menu, SWF Quicker, SWF to FLA converter, SWF Editor, HTML Editor
Sothink DHTMLMenu : Using PHP to create dynamic menu from mySQL database


Part 1: Overview

With Sothink DHTMLMenu, you can easily create dynamic DHTML/JavaScript menus from database. The menu structure can be stored in database tables and retrieved by any server side scripts like ASP or PHP. This document shows how to create a dynamic menu from database by using PHP and mySQL database.

In brief, to create a dynamic menu from database, you can configure the Prefix or Suffix of the menu item settings in the Menu Item Settings > Condition of Sothink DHTMLMenu 5.0.

Click here to view a dynamic menu sample whose menu structure is generated from database



As you see, in this sample, the menu contains two levels: the parent level showing the product categories and the sub level showing the specific products. Actually, there are only two menu items, one in the parent level and the other is in the sub level.
The final result that you are seeing is just got from database.

Part 2: Steps

The following are the steps of this example:

  1. Before creating the menu, you should make a database containing the information of the product categories and the specific products.

    To do this, create a database named "dmenu" in your mySQL, create a database user named "dbuser" and the password is "123". Then you can use the following code to generate tables:

    CREATE TABLE Category (
    CID int(11) NOT NULL auto_increment,
    Name char(255) DEFAULT '' NOT NULL,
    Notes char(255),
    PRIMARY KEY (CID)
    );

    CREATE TABLE Product (
    PID int(11) NOT NULL auto_increment,
    CID int(11),
    Name char(255) DEFAULT '' NOT NULL,
    Link char(255),
    Notes char(255),
    PRIMARY KEY (PID)
    );


    Then you can add a few records to the tables.
  2. Launch Sothink DHTMLMenu and create a menu from Blank. Click "General" in "Menu Item Settings", select "HTML". Input <?=$CatName?> in the context box.
     

      
  3. Click"Condition" in "Menu Item Settings".  
     

      
  4. In "Menu Item Settings > Condition" Dialog, input the prefix and suffix for the menu item.
     


    Note: When you add the condition, you may get a warning message in DHTMLMenu. That's because the condition code is server-side code which can not be processed by the browser directly. The warning will not show when you add the menu to a PHP page that is supported by server. You can uncheck "Preview > Enable Auto-Refresh" to avoid seeing the warnings.
    Tips: To prevent this error popping up, you can add the JavaScript comment delimiters /* and */ to the Prefix as well as Suffix. In that way the preview ignores the extra code and the code works well on the server side processing.

    For example:
    Prefix:
    /*
    <?
    for ($i = 0; $i < $numrowsCat; $i++)
    {
    $rowCat = mysql_fetch_array($resultCat);
    $CatName = $rowCat["Name"];
    ?>
    */

    Suffix:
    /*<?
    }
    ?>
    */

      

  5. Click "Add" button to add a sub item of it. Change the item type to "HTML", then input <?=$ProName?> . In the link field, input <?=$Link?>.



        
  6. Click "Condition" in "Menu Item Settings" and input the prefix and suffix for the menu item.

Note: Normally in order to optimize the menu code, if several menu items have the same property, only one item's property code is recorded and other items take reference of its property code. If we add a condition to this item, there is possibility that it will be invisible when its condition is not met. And its property code can not be referenced by other items either. This will cause the whole menu can not work correctly. Please do check "This item may be invisible at run-time" to avoid the problem.

Part 3: Code Explanation

This is the source code at the beginning of this sample page.

Code

Explanation

<?
$dbhost = "localhost"; // DB Host name
$dbusername = "dbuser"; // DB User
$dbpass = "123"; // DB User password
$dbname = "dmenu"; // DB Name
$query = "select * from Category";
$connection = mysql_connect($dbhost, $dbusername, $dbpass);
Connect with the database.
$resultCat = mysql_db_query($dbname, $query);
$numrowsCat = mysql_num_rows($resultCat);
?>  
Retrieve recordset from the CATEGORY table.

After you add item condition and edit the menu item, DHTMLMenu inserts following code into the page. The entire source code below is generated by DHTMLMenu program. Your editing of conditions will be saved in the menu's configuration file (*.pgt), which can be edited again.
To learn further what the code represents,  please read the following code explanation:

Code

Explanation

stm_bm(["uueoehr",400,"".....) Begin the whole menu.
    stm_bp("p0",[0,4,0,0,3,1,0,0,100,"",-2...... ) Begin a popup menu (items of category).
<?
for ($i = 0; $i < $numrowsCat; $i++)
{
     $rowCat = mysql_fetch_array($resultCat);
     $CatName = $rowCat["Name"];
?>
Walk through the CATEGORY recordset and save each category's name as variable "sCatName".
         stm_ai("p0i0",[1,"<?=$CatName?>","",.....) <?=$CatName?> is the category name.
         stm_bpx("p1","p0",[1,4,0,-1,.....) Begin a popup menu (items of product).

<?
$queryPro = "select * from Product where CID=".$rowCat["CID"];
$resultPro = mysql_db_query($dbname, $queryPro);
$numrowsPro = mysql_num_rows($resultPro);

for ($j = 0; $j<$numrowsPro; $j++)
{
$rowPro = mysql_fetch_array($resultPro);
$ProName = $rowPro["Name"];
$Link = $rowPro["Link"];
?>

Retrieve recordset from the PRODUCT table. Walk through the PRODUCT recordset and save product name and link as temporary variables.

         stm_ai("p1i0",[1,"<?=$ProName?> .....) Append an item. <?=$ProName?> is the item text.

<?
}
?>

Move to the next record of PRODUCT table.
  &nbspstm_ep() End of a popup menu.

<?
}
?>

Move to the next record of the CATEGORY recordset.

   stm_ep() End of a popup menu.
stm_em() End of the whole menu.

It is a cool menu, isn't it? This result is reached by the script in the menu. If you want your menu to realize the special function and effect, but you are not familiar with the script, view here to submit your request to us. We will assist you to get it.

      SourceTec Software Co., LTD. All rights reserved. Email:webmaster@sothink.com
      Sothink Software: Tree Menu, DHTMLMenu, SWF Decompiler, SWF Quicker, Glanda, FlashVideo Encoder