Click to See Complete Forum and Search --> : Array issues


minority
09-09-2005, 03:17 AM
Hi

This is the first time i have used an array.

basically what i am trying to test is how it done.

i have simply done a database call and placed them into an array however i am getting a type mismatch.


<%
Set rsgeolink2 = Server.CreateObject("ADODB.Recordset")
Set rsgeolink = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT Dept_ID, [Dept_Name] FROM Department"

Dim MyDept ' An array of the departments
Dim MyID ' An array of the dept id
i = 0 ' Used to create a unique id for each array value

rsgeolink.open strSQL, strCon
Do While not rsgeolink.EOF

Dept_Name = rsgeolink("Dept_Name")
Dept_ID = rsgeolink("Dept_ID")

MyDept(i)= Dept_Name
MyID(i) = Dept_ID
i = i+1

rsgeolink.movenext
loop
rsgeolink.Close

response.write MyDept(i) & "<br>"


%>


the error is happening at line 25 which is the blank line just below MyDept(i)
and i basically removed my array values MyDept and MyID and it doesnt error so i gather it is something to do with the way i am trying to put my data into them from the database

can anyone explain how this is done or show me what the issue is with my code as i couldnt find an answer on google.

russell_g_1
09-11-2005, 11:34 AM
i think the problem is that you haven't said that your arrays are arrays. you need to stick emtpy brackets on the end of the names when you declare them. and then you need to redim them to the right size once you know what it is. here's an example...

<%

loadData()

%>
<html>
<body>

<%

writeData()

%>

</body>
</html>




<%

function loadData()

dim sql,rs,totalrecords,i
dim arrid(),arrname()

sql = "select id,name from item"

if runsql(sql,rs) then

totalrecords = rs.recordcount - 1

redim arrid(totalrecords)
redim arrname(totalrecords)

for i = 0 to totalrecords

arrid(i) = rs("id")
arrname(i) = rs("name")

rs.movenext

next

rs.close

end if

session("arrid") = arrid
session("arrname") = arrname

end function




function writeData()

dim arrid,arrname,i

arrid = session("arrid")
arrname = session("arrname")

for i = 0 to ubound(arrid)

response.write arrid(i) & " = " & arrname(i) & "<br>"

next

end function





function runSQL(SQL,rs)

dim myrs

on error resume next

set myRs = createobject("ADODB.recordset")
myRs.Open SQL,"DATABASE=filesys1;UID=sa;PWD=qwertyuiop;DSN=filesys1", 1, 3
set rs = myRs
if err then
runSQL = false

response.write err.description

else
runSQL = true
end if

end function



%>

buntine
09-11-2005, 08:06 PM
It would be best to set an explicit size for the array or redimension it after the size has been established.

Set rsgeolink2 = Server.CreateObject("ADODB.Recordset")
Set rsgeolink = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT Dept_ID, [Dept_Name] FROM Department"

Dim MyDept() ' An array of the departments
Dim MyID() ' An array of the dept id
i = 0 ' Used to create a unique id for each array value

rsgeolink.open strSQL, strCon, 3

ReDim MyDept(rsgeolink.RecordCount)
ReDim MyID(rsgeolink.RecordCount)

It may also be more efficient (or just cleaner) to use a for loop if you want a incrementing variable.

For i = 0 To rsgeolink.RecordCount - 1
...
Next

Regards.

minority
09-12-2005, 04:13 AM
double post

minority
09-12-2005, 04:17 AM
its okay wasnt thinking i meant to do it within a javascript array after all ...

although i can not get it to work correctly i am not sure if it is my javascript or my asp that is creating the issues.


<!--#include file="../../include/connected.asp"--><html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<%
Set rsgeolink2 = Server.CreateObject("ADODB.Recordset")
Set rsgeolink = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM Document_Type"
strSQL2 = "SELECT Sub_ID, Sub_Name, Dept_ID, Doc_Type_ID FROM Doc_Sub_Category"
rsgeolink.open strSQL, strCon
%>
<script type="text/javascript">
<!--
<!--First array are your "dept_x" variables, shown below: -->

var docs=[<%do until rsgeolink.EOF %>
[<%for each x in rsgeolink.Fields%><%=needcomma%>"<%=x.value%>"<%needcomma = ","%><%next%>]<%rsgeolink.MoveNext%><%needcomma = ""%><%loop%><%rsgeolink.close%>
];

var ids=[<%rsgeolink.open strSQL2, strCon%> <%Do While not rsgeolink.EOF%>
["<%=rsgeolink("Sub_ID")%>","<%=rsgeolink("Sub_Name")%>","<%=rsgeolink("Dept_ID")%>","<%=rsgeolink("Doc_Type_ID")%>"]<%rsgeolink.movenext%><%loop%><%rsgeolink.Close%>
];

function Box2(idx) {
var f=document.myform;
f.box2.options.length=null;
f.box2.options[0]=new Option("Select Document Type", 0);
for(i=1; i<=docs.length; i++) {
if (docs[i-1][idx+2] == 'yes')
f.box2.options[i]=new Option(docs[i][1], docs[i][0]); // if it's yes creat the option docs[i][0] = type_id docs[i][1] = type_name
}
Box3("0"); //calls the function to create third box options with value of 0 (leaves box disabled since no option is selected)
}

function Box3(idx) {
var j=1;
var f=document.myform;
f.box3.options.length=null; //erases current values
f.box3.options[j] = new Option("Select Document ID",0)
f.box3.disabled = true; //makes sure box starts disabled
for(i=0; i<ids.length; i++) {//loops through the ids array
if (ids[i][2] = f.box1.selectedIndex && ids[i][3] = f.box2.selectedIndex){ //returns true if ids[i][2] = dept_id and ids[i][3] = type_id (both of the values for selected items in boxes 1 + 2
if (f.box3.disabled){ //enables box if disabled
f.box3.disabled = false;
}
f.box3.options[j]=new Option(ids[i][1], ids[i][0]); //creates box option
j += 1;
}
}
//-->
</script>


</head>

<body>
<form name="myform" method="post" action="#">
<select name="box1" onchange="Box2(this.selectedIndex)">
<%
strSQL2 = "SELECT Dept_ID, Dept_Name FROM Department"
rsgeolink.open strSQL2, strCon
Do until rsgeolink.EOF
Response.Write("<option value=""" & rsgeolink("Dept_ID") & """ > " & rsgeolink("Dept_Name") & "</option>" & vbCrLf)
rsgeolink.MoveNext()
loop
rsgeolink.close
%>
</select>

<select name="box2" onchange="Box3(this.selectedIndex)"></select>

<select name="box3"></select>
</form>

</body>
</html>


what do you think?

this is what it looks like when it outputs at the html level.


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
<!--
<!--First array are your "dept_x" variables, shown below: -->

var docs=[
["1","QA Manual","False","True","False","False","False","False","False","False","False","False","False"]
["2","Processes","False","False","True","True","True","True","True","True","True","False","False"]
["3","Forms","True","True","True","True","True","True","True","True","True","True","True"]
["4","Organisation Charts","False","True","True","True","True","True","True","True","True","False","False"]
["5","Quality Procedures","False","True","False","False","False","False","False","False","False","False","False"]
["6","General information","True","False","False","False","False","False","False","False","False","True","False"]
["10","Risk Assessments","False","False","False","False","False","False","False","False","False","True","False"]
["11","Accidents","True","False","False","False","False","False","False","False","False","True","False"]
["12","HSE memos","False","False","False","False","False","False","False","False","False","True","False"]
["13","HSE information","True","False","False","False","False","False","False","False","False","True","False"]
["14","Safety meetings","True","False","False","False","False","False","False","False","False","True","False"]
["15","Maintenance and Inspection","True","False","False","False","False","False","False","False","False","True","False"]
["16","Records","False","False","False","False","False","False","False","False","False","False","False"]
["17","Manufacturing Forms","True","False","False","False","False","True","False","False","False","False","False"]
];

var ids=[
["13","Electronic Down Hole BTR","0","17"]
["1","Health and Safety Policy","10","6"]
["2","Audit Reports","10","6"]
["3","responsible Personnel","10","6"]
["4","Accident Form","10","11"]
["5","Accident Log","10","11"]
["6","manual handling","10","13"]
["7","electrical safety","10","13"]
["8","COSHH","10","13"]
["9","Minutes","10","14"]
["10","Reports","10","14"]
["11","Inspection Reports","10","15"]
["12","Certification","10","15"]
];

function Box2(idx) {
var f=document.myform;
f.box2.options.length=null;
f.box2.options[0]=new Option("Select Document Type", 0);
for(i=1; i<=docs.length; i++) {
if (docs[i-1][idx+2] == 'yes')
f.box2.options[i]=new Option(docs[i][1], docs[i][0]); // if it's yes creat the option docs[i][0] = type_id docs[i][1] = type_name
}
Box3("0"); //calls the function to create third box options with value of 0 (leaves box disabled since no option is selected)
}

function Box3(idx) {
var j=1;
var f=document.myform;
f.box3.options.length=null; //erases current values
f.box3.options[j] = new Option("Select Document ID",0)
f.box3.disabled = true; //makes sure box starts disabled
for(i=0; i<ids.length; i++) {//loops through the ids array
if (ids[i][2] = f.box1.selectedIndex && ids[i][3] = f.box2.selectedIndex){ //returns true if ids[i][2] = dept_id and ids[i][3] = type_id (both of the values for selected items in boxes 1 + 2
if (f.box3.disabled){ //enables box if disabled
f.box3.disabled = false;
}
f.box3.options[j]=new Option(ids[i][1], ids[i][0]); //creates box option
j += 1;
}
}
//-->
</script>


</head>

<body>
<form name="myform" method="post" action="#">
<select name="box1" onchange="Box2(this.selectedIndex)">
<option value="1" > Quality Management</option>
<option value="2" > Sales and Maketing</option>
<option value="3" > Design and Development</option>
<option value="4" > Purchasing</option>
<option value="5" > Manufacturing</option>
<option value="6" > Customer Support</option>
<option value="7" > Administration and Finance</option>
<option value="8" > Operations and Rentals</option>
<option value="9" > Personnel</option>
<option value="10" > Health and Safety</option>

</select>

<select name="box2" onchange="Box3(this.selectedIndex)"></select>

<select name="box3"></select>
</form>

</body>
</html>


Any help in this part would be greaat as it doing my nut in.

seems to be at this point it errors out at
<select name="box1" onchange="Box2(this.selectedIndex)">

for some reason.

minority
09-13-2005, 05:24 AM
has no one got any ideas?

russell_g_1
09-14-2005, 01:14 PM
what's it all supposed to do?

HaganeNoKokoro
09-14-2005, 01:17 PM
You need commas between array items var ids=[
["13","Electronic Down Hole BTR","0","17"],
["1","Health and Safety Policy","10","6"],
["2","Audit Reports","10","6"],
["3","responsible Personnel","10","6"],
["4","Accident Form","10","11"],
["5","Accident Log","10","11"],
["6","manual handling","10","13"],
["7","electrical safety","10","13"],
["8","COSHH","10","13"],
["9","Minutes","10","14"],
["10","Reports","10","14"],
["11","Inspection Reports","10","15"],
["12","Certification","10","15"]
];(also happening in the docs array)

minority
09-15-2005, 10:23 AM
cheers found it lol

about a hour ago...taking me this long to come back to the thread.