Creating menu from db using .asp

Please note that official support for this menu version has now ceased. There are still plenty of users, though, and the forum is still running. Some of our long-time users may be able to help you out.
Post Reply
Guest

Creating menu from db using .asp

Post by Guest »

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
zork
Beginner
Beginner
Posts: 3
Joined: Wed May 22, 2002 10:32 pm

Re: Creating menu from db using .asp

Post by zork »

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: Select all

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
smadasam
Beginner
Beginner
Posts: 1
Joined: Wed May 29, 2002 9:11 pm
Contact:

what were the first step you took?

Post by smadasam »

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 :?:
zork
Beginner
Beginner
Posts: 3
Joined: Wed May 22, 2002 10:32 pm

Re: what were the first step you took?

Post by zork »

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: Select all

	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: Select all

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: Select all

	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.
indra
Advanced
Advanced
Posts: 15
Joined: Sun May 19, 2002 8:23 pm

Post by indra »

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
zork
Beginner
Beginner
Posts: 3
Joined: Wed May 22, 2002 10:32 pm

Re: Creating menu from db using .asp

Post by zork »

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: Select all

<%@ 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@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
Milo
Advanced
Advanced
Posts: 13
Joined: Tue Jun 10, 2003 1:05 pm
Location: Wales
Contact:

DB example...

Post by Milo »

Post Reply