Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old August 3rd, 2006, 05:18 AM
degsy degsy is offline
Contributing User
ASP Free God 2nd Plane (6000 - 6499 posts)
 
Join Date: Aug 2005
Location: North East, UK
Posts: 6,191 degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 19 h 41 m 52 sec
Reputation Power: 121
In_array function

For reference, here is an in_array function to check if a string/value is in an array.


asp Code:
Original - asp Code
  1.  
  2. Function in_array(element, arr)
  3.     For i=0 To Ubound(arr)
  4.         If Trim(arr(i)) = Trim(element) Then
  5.             in_array = True
  6.             Exit Function
  7.         Else
  8.             in_array = False
  9.         End If 
  10.     Next
  11. End Function



Examples
asp Code:
Original - asp Code
  1.  
  2. <%
  3. Function in_array(element, arr)
  4.     For i=0 To Ubound(arr)
  5.         If Trim(arr(i)) = Trim(element) Then
  6.             in_array = True
  7.             Exit Function
  8.         Else
  9.             in_array = False
  10.         End If 
  11.     Next
  12. End Function
  13.  
  14. str = "Blue"
  15. colors = Array("Red","Green","Blue","Black","White")
  16.  
  17. If in_array(str,colors) Then
  18.     Response.Write str & " is in the array"
  19. Else
  20.     Response.Write str & " is not in the array"
  21. End If
  22. %>







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

Reply With Quote
  #2  
Old October 19th, 2006, 04:49 PM
RTCruiser RTCruiser is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 5 RTCruiser User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 26 m 54 sec
Reputation Power: 0
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!

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > In_array function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT