How to parse an undetermined amount of URL Variables (cgi.Query_String

Form passes url string containing "?variable=data&...",

Page 1 of 1

1 Replies - 20667 Views - Last Post: 24 July 2006 - 07:02 PM Rate Topic: -----

#1 Samuraiken   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 19-July 06

How to parse an undetermined amount of URL Variables (cgi.Query_String

Posted 19 July 2006 - 07:57 PM

Here's my "problem"...And if anyone can help, that'd be great.

Basically, i have a form, that retrieves a list of servers from an access database. The number of servers isn't static though: there can be new ones added at any time. I'm trying to make this as dynamic as possible so when i'm not here, i don't get email traffic asking me to update it :crazy:

part of the code looks like this:
						<tr><cfoutput query="GetServers">
							<td bgcolor="white">#GetServers.Name#</td>
							<td bgcolor="white"><cfif #GetServers.Type# eq 1>File Server</cfif><cfif #GetServers.Type# eq 2>Mail Server</cfif><cfif #GetServers.Type# eq 3>Misc Server</cfif></td>
							<td bgcolor="white">#Net#</td>
							<td bgcolor="white"> </td>
							<td bgcolor="white" align="center">Pass<INPUT TYPE="RADIO" NAME="#GetServers.Name#" Value="1"></td>
							<td bgcolor="white" align="center">Fail<INPUT TYPE="RADIO" NAME="#GetServers.Name#" Value="2"></td>
							<td bgcolor="white" align="center">Running<INPUT TYPE="RADIO" NAME="#GetServers.Name#" Value="3"></td>
						</tr></cfoutput>



it gets passed to another page, so the query string will look something like this:
https://webserver/cm/new/daily2.cfm?Serverone=1&Servertwo=1&Serverthree=2&Serverfour=2&Serverfive-S=3

,etc etc..

What i'm trying to do is, without knowing how many total servers were passed from the form page, parse through and count the number of "&" (because thats the symbol that seperates
the variables in the url string. So Server=value&server=value&. The "value" is going to be 1 (server backup is finished), 2 (server backup is running), 3 (server backup failed).
Here's the code i have so far.

		
															 <!--- find the "&" sign, as the parse point between variables---> 
					<CFSET #Parse#=#Find("&", UrlString)#>
					<!--- store url string, minus the currentvar, which is being removed --->	
					<CFSET #URL2#=#Right(UrlString, (StrLen - Parse))#><strong>URL2</strong> = <em>#URL2#</em><br>
					<!--- get the length of the new string --->
					<CFSET #StrLen1#=#Len(URL2)#>	StrLen1 = #StrLen1#<br>	
					<!--- grab the current variable selection (server=status) --->
					<CFSET #CurrentVar#=#Left(UrlString, Parse-1)#>CurrentVar = #CurrentVar#<br>
					<!--- finds the "=" in currentvar selection --->
					<cfset #FindEQ# = #find("=", URL2)#>FindEQ = #FindEQ#<br>
					<!--- get the server name from currentvar --->
					<cfset #Server#=#left(UrlString, (FindEQ-1))#>Server = #Server#<br>
					<!--- get the status from currentvar --->	
					<cfset #Status#=#Mid(UrlString, (FindEQ+1), 1)#>Status = #Status#<br>
				<!--- set #UrlString# to #Url2# --->
				<cfset #UrlString# = #right(cgi.Query_String, (StrLen - Parse))#>URLString = #UrlString#<br><br>


So basically it will take a string like
https://webserver/cm...;Serverfive-S=3
find the first "&" and store everything before it into a variable (thus, from the above string, it will store "serverone=1" into #CurrentVar# (the current variable=value being looked at).
Then it will find the "=", and split the "server" and "value" from each other.
For what i have right now, it works just fine...but it only does it for the first part of the string.

The problem i'm having is that i want to loop this for the number of variables in the URL (so do it 5 times if there are only 5 servers, or 20 times if there are 20 servers).
I tried using <CFSCRIPT> but i think i horribly mutilated it (because i was going to use cfscript to do a "do/while" loop (while the length of #urlString# was GT 0), but you cant have normal
CF tags in <CFSCRIPT>. Maybe i'm doing something wrong.

Here's the CFSCRIPT code if anyone wanted to look at that.
<cfoutput>
		<!--- get the length of the current string --->
	<CFSET #StrLen#=#Len(UrlString)#>
	<CFIF StrLen is 0>
		No URL string listed
	<cfelse>
		<cfscript>
			  do {
					ParseP = Find("&", UrlString);
					URL2 = Right(UrlString, (StrLen - ParseP));
					StrLen1 = Len(URL2);
					CurrentVar = Left(UrlString, ParseP-1);
					FindEQ = find("=", URL2);
					Server = left(UrlString, (FindEQ-1));
					Status = Mid(UrlString, (FindEQ+1), 1);
				UrlString = right(cgi.Query_String, (StrLen - ParseP));
			  } while (StrLen GT 0);
		</cfscript> 
	</cfif>
</cfoutput>



I'd greatly appreciate it if someone was able to help out. Or at least pass off some ideas. It's just a project i was working on so that the folks i'm working with no longer have to keep paper records of the backup reports for our servers. This way we can store it in a database, and back that thing up with our offsite location storage.

Is This A Good Question/Topic? 0
  • +

Replies To: How to parse an undetermined amount of URL Variables (cgi.Query_String

#2 wiredwizard   User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 07-May 05

Re: How to parse an undetermined amount of URL Variables (cgi.Query_String

Posted 24 July 2006 - 07:02 PM

How about something simple like

[
<cfset newVar = url.myString>
<cfset thsCount = listlen(newVar, "&")>
]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1