<%@LANGUAGE = "JAVASCRIPT"%> ASP example


This file is a simple example of how to get items from a access database. I code ASP with javascript so the example is in javascript. I will make a more advances example later. The table consist of 4 simple columns:

menuID - Autonumber - the id of the menuitem.
mName - String - The menu name
mLink - String - The link
parent - Number - a recursive relation to menuID.


This can rather easily be converted to control the entire menu and by adding a server-side admin *anyone* could easily change the menu. The new menumaker that I will hopefully soon have time to make will probably use something like this.

On this site I use a similar approuch, the only difference is that I make a js file everytime I update, that way I don't have to get the items from the database on every visit. I will try and make an example like that as well later.

ASP source-code:
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ASP CODE START - READING ITEMS FROM THE DATABASE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
function readItemsFromDatabase(){
  //The path to your database:
  var db ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("menu.mdb")
  
  var q = "SELECT menuID,mName,mLink,parent from tblMenu ORDER BY parent,menuID ASC"
  
  var rs=Server.CreateObject("ADODB.Recordset")
  rs.CacheSize = 25; 		// Cache data fetching
  rs.CursorType = 3
  rs.LockType = 3
  
  //Opening database --- --
  rs.Open(q,db)
  
  //Now using getRows because that's so sexy :}
  if(!rs.EOF){
    var rsarr = rs.GetRows();
    rsarr = rsarr.toArray();
    var cols= rs.Fields.Count //Setting how much to add to row each for
  }else var rsarr=new Array()
  
  //Closing database, we don't need it anymore - we have the info in the array
  rs.close()
  rs = null
  
  var menuID,mName,mLink,parent
  
  for(row=0;row<rsarr.length;row+=cols){ //Looping rows
    //Setting variables 
    menuID = "m" + rsarr[row]
    mName = rsarr[row+1]
    mLink = String(rsarr[row+2])
    if(mLink=="null") mLink="" 
    parent = rsarr[row+3]
    if(parent!=0) parent = "m" + parent
    else parent=""
    //Making menu item
    Response.write("oCMenu.makeMenu('"+menuID+"','"+parent+"','"+mName+"','"+mLink+"')\n")
  }
}
//Calling function
readItemsFromDatabase()

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ASP CODE END - READING ITEMS FROM THE DATABASE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/