Milonic provide full featured pull down web menus for some of the worlds largest companies
click here to see what it can do for you

Download Milonic DHTML Menu
Buy Milonic DHTML Menu

Back To Start Of Archive
Taken From The Forum: Archived Topics for the old Version 3.0 JavaScript Menu
Forum Topic: Click to view post
Last Updated: Wednesday July 18 2012 - 06:07:38

Creating menu from db using .asp


Poster: Anonymous
Dated: Thursday May 23 2002 - 11:58:21 BST

Hi All

I've been trying to set up a menu using Active Server Pages to draw the menu items from a db, but have come up with a glitch I can't solve.

Here's what I've managed so far - I renamed menu_array.js to menu_array.asp. This works fine.

I then added the following code (on one line) to create a menu item using VBScript, and replacing the text and link with info drawn from the db.

<%
Response.Write(" ,""<img src=newsimage.gif border=0>&nbsp;" & NS("org_name") & """,""" & NS("org_url") & """,,,1")
%>

Again, this works fine, and if I repeat the line then that's fine too.

The problem comes when I incorporate a loop to use all the elements from a recordset.

For instance

<%
for i = 1 to 5
Response.Write(" ,""<img src=newsimage.gif border=0>&nbsp;" & NS("org_name") & """,""" & NS("org_url") & """,,,1")
NS.MoveNext
next i
%>

doesn't work (I get a scripting error with no menu appearing at all). I think it may be to do with the different ways in which VBScript and JavaScript handle carriage returns, but don't know and am a bit baffled.

If you spot any obvious error on my part I'd be delighted to hear about it.

cheers

Garve

Re: Creating menu from db using .asp


Poster: zork
Dated: Thursday May 23 2002 - 21:31:14 BST

Anonymous wrote:
The problem comes when I incorporate a loop to use all the elements from a recordset.

For instance

<%
for i = 1 to 5
Response.Write(" ,""<img src=newsimage.gif border=0>&nbsp;" & NS("org_name") & """,""" & NS("org_url") & """,,,1")
NS.MoveNext
next i
%>

doesn't work (I get a scripting error with no menu appearing at all).


What is the scripting error that you are getting?

Also, I'm assuming that you have at least 5 records in your recordset (based on your example of "for i = 1 to 5..."). If you are actually using a for...next loop for your recordset, you might want to use this instead:

Code:
While Not rsAny.EOF
   Response.Write ...
   rsAny.MoveNext
Loop


One thing that I've found to be helpful is to create a test.asp page that outputs the code out to the page. Then I can view the code to make sure it is being created properly.

If everything looks okay, you can then copy the output into the menu_array.asp page and see if it that works.

At least it should get you pointing in the right direction to figure out where the problem is.

Hope that helps :)

-Zork

what were the first step you took?


Poster: smadasam
Dated: Wednesday May 29 2002 - 21:21:13 BST

what were the first step you took? I am basicly trying to do the same thing with a SQL database, but I am not quite sure what I need to do to get it started... The steps you took would be helpful :?:

Re: what were the first step you took?


Poster: zork
Dated: Monday June 10 2002 - 19:29:12 BST

smadasam wrote:
what were the first step you took? I am basicly trying to do the same thing with a SQL database, but I am not quite sure what I need to do to get it started... The steps you took would be helpful :?:


I'm not sure if you were asking me or not (but I'm responding anyway ;) )

Here are the steps that I took to create a dynamic menu using .asp:

1. Rename menu_array.js to menu_array.asp
2. Change all references of menu_array.js to menu_array.asp (e.g.: <SCRIPT language="JavaScript" src="menu_array.asp" type="text/javascript"></SCRIPT>)

The next few steps took me a while. I've found it's best to add a little bit of code at a time and check your results (it makes it easier to locate your bugs).

3. Add database connection code
4. Create new main menu link (the sub menu items will be populated with data from your database). For this example we're adding "My Menu Test":
Code:
   addmenu(menu=["main_menu",
   ,,150,1,"",style1,,"left",effect,,,,,,,,,,,,
   ,"Sub Menu #1","",,,0
   ,"Sub Menu #2","",,,0
   ,"My Menu Test","",,,0
   ])

5. Create a new submenu section and populate the titles of each submenu item with data from your database. Here is a sample of some of the code I used (it might be difficult to read with all of the embedded asp code):

Code:
Do While Not rsMenu.EOF
   Response.Write "addmenu(menu=[""" & rsMenu("menu_desc") & """," & vbCrLf
   Response.Write ",,160,1,"""",style1,,""left"",effect,,,,,,,,,,,," & vbCrLf
   Response.Write ",""" & rsMenu("menu_name") & """,""/test/" & rsMenu("menu_page_name") & ".asp"",,,0" & vbCrLf
   rsMenu.MoveNext
Loop
Response.Write "])" & vbCrLf


That produced the following menu section in menu_array.asp:
Code:
   addmenu(menu=["My Menu Test",
   ,,160,1,"",style1,,"left",effect,,,,,,,,,,,,
   ,"Menu Item #1","/test/menu_1.asp",,,1
   ,"Menu Item #2","/test/menu_2.asp",,,1
   ,"Menu Item #3","/test/menu_3.asp",,,1
   ])

"My Menu Test", "Menu Item #1", "menu_1", "Menu Item #2", "menu_2", "Menu Item #3", "menu_3" were all retrieved from the database.

Any variable can be retrieved from the database, just make sure you write everything in the proper order and in the proper way (i.e. all string values need quotes, etc.)

Hope that helps :D

-Zork

p.s. I grabbed bits and pieces of my code so some of the arguments for addmenu may be incorrect in places.


Poster: indra
Dated: Monday June 10 2002 - 22:17:16 BST

What do you do with this line of code?

menunum=0;menus=new Array();_d=document;function addmenu(){menunum++;menus[menunum]=menu;}function dumpmenus(){mt="<script language=javascript>";for(a=1;a<menus.length;a++){mt+=" menu"+a+"=menus["+a+"];"}mt+="<\/script>";_d.write(mt)}


Thanks

Re: Creating menu from db using .asp


Poster: zork
Dated: Tuesday June 11 2002 - 19:59:35 BST

indra wrote:
What do you do with this line of code?

menunum=0;menus=new Array();_d=document;function addmenu(){menunum++;menus[menunum]=menu;}function dumpmenus(){mt="<script language=javascript>";for(a=1;a<menus.length;a++){mt+=" menu"+a+"=menus["+a+"];"}mt+="<\/script>";_d.write(mt)}

Thanks


I left it exactly as it is in the original file. When I converted menu_array.js to menu_array.asp I kept it in the same place except I added some variable declarations and opened my database connection at the top of the page:

Code:
<% __at__ LANGUAGE="VBSCRIPT" %>
<%
'***********************************************************
' Name:         menu_array.asp   
' Purpose:      Creates a dynamic menu using the Milonic menu.
' Parameters:   None
' Updates:      
'***********************************************************

Option Explicit
Response.Buffer = True
Response.Expires = 0

   ' Declare and instantiate ADO objects
   Dim adoConn, cmdAny, rsMenu
   Set adoConn = Server.CreateObject("ADODB.Connection")
   Set cmdAny = Server.CreateObject("ADODB.Command")
   Set rsMenu = Server.CreateObject("ADODB.Recordset")

   ' Open database connection and set command object
   adoConn.Open Application("ConnectionString")
   cmdAny.ActiveConnection = adoConn

   Set rsMenu = cmdAny.Execute( -- insert sql here -- )

%>

/*
Milonic DHTML Website Navigation Menu - Version 3.4
Written by Andy Woolley - Copyright 2002 (c) Milonic Solutions Limited. All Rights Reserved.
Please visit http://www.milonic.co.uk/menu or e-mail menu3 __at__ milonic.com for more information.

The Free use of this menu is only available to Non-Profit, Educational & Personal web sites.
Commercial and Corporate licenses  are available for use on all other web sites & Intranets.
All Copyright notices MUST remain in place at ALL times and, please keep us informed of your
intentions to use the menu and send us your URL.
*/


//The following line is critical for menu operation, and MUST APPEAR ONLY ONCE. If you have more than one menu_array.js file rem out this line in subsequent files
menunum=0;menus=new Array();_d=document;function addmenu(){menunum++;menus[menunum]=menu;}function dumpmenus(){mt="<script language=javascript>";for(a=1;a<menus.length;a++){mt+=" menu"+a+"=menus["+a+"];"}mt+="<\/script>";_d.write(mt)}
//Please leave the above line intact. The above also needs to be enabled if it not already enabled unless this file is part of a multi pack.

////////////////////////////////////
// Editable properties START here //
////////////////////////////////////

.
.
.

etc.



Hope that helps :)

-Zork

DB example...


Poster: Milo
Dated: Wednesday June 11 2003 - 12:25:02 BST

http://www.milonic.co.uk/menu/forum/viewtopic.php?t=1920

Just my $0.02 worth...