| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||||||
|
||||||||
|
In_array function
For reference, here is an in_array function to check if a string/value is in an array.
asp Code:
Examples asp Code:
Database Table: color_list Records: Code:
ID color 1 Black 2 Brown 3 Red 4 Blue 5 Green 6 White Table: color_selection Code:
ID color 1 Red,Green,Blue Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="db.asp" -->
<%
Set colour_list = Server.CreateObject("ADODB.Recordset")
colour_list.ActiveConnection = conn
colour_list.Source = "SELECT * FROM color_list"
colour_list.CursorType = 0
colour_list.CursorLocation = 2
colour_list.LockType = 1
colour_list.Open()
Set colour_selection = Server.CreateObject("ADODB.Recordset")
colour_selection.ActiveConnection = conn
colour_selection.Source = "SELECT * FROM color_selection"
colour_selection.CursorType = 0
colour_selection.CursorLocation = 2
colour_selection.LockType = 1
colour_selection.Open()
Function in_array(element, arr)
For i=0 To Ubound(arr)
If Trim(arr(i)) = Trim(element) Then
in_array = True
Exit Function
Else
in_array = False
End If
Next
End Function
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form name="form1" id="form1" method="post" action="">
<%
While NOT colour_list.EOF
%>
<input type="checkbox" name="colour_list" value="<%=colour_list("color")%>"<%If in_array(colour_list("color"), Split(colour_selection("color"),",")) Then Response.Write " checked=""checked"""%> /> <%=colour_list("color")%> <br />
<%
colour_list.MoveNext()
Wend
%>
</form>
</body>
</html>
<%
colour_list.Close()
Set colour_list = Nothing
colour_selection.Close()
Set colour_selection = Nothing
%>
__________________
CyberTechHelp |
|
#2
|
|||
|
|||
|
Sir, you are a genius.
Thank you. I initially got an 'Invalid use of Null' error, but I got it to work by dumping the smaller array into a variable, i.e: Code:
If in_array(StoreRS("StNumber"), Split(CompletedStores_variable,",")) Then
Thanks again for the elegant solution to my problem! |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > In_array function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|