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> " & 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> " & 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
Creating menu from db using .asp
Re: Creating menu from db using .asp
What is the scripting error that you are getting?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> " & 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).
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
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?
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?
I'm not sure if you were asking me or not (but I'm responding anyway )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
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
])
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
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
])
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
-Zork
p.s. I grabbed bits and pieces of my code so some of the arguments for addmenu may be incorrect in places.
Re: Creating menu from db using .asp
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: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
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.
-Zork