Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to achieve this search...

Status
Not open for further replies.

aamaker

Programmer
Jan 20, 2002
222
US
Im working on a lottery website and have a 'winning numbers' database for a 'game' that is setup like this:

field 1: "digit1"
field 2: "digit2"
field 3: "digit3"


I need (DESPARATELY) to develop a search feature that works like this one:
... so that the user enters three digits and all the varying combinations of this 'number' are returned in a simple HTML table.

I know theres a way but cant figure out how to come up with a solution that finds ONLY the combinations for those 3 digits.

How can I achieve this given the way the numbers are stored in my database?


** In the future, I'll need to port this same thing over to another game that has 4 digits then another that has 5 digits if that helps put it all in perspective.

aamaker@sc.rr.com


-- Thanks for any help you can provide!
 
Ok there is really two ways I can think of to do this (off the top of my head)

Assuming you need it out of the db:
1) Create all the possible combinations and have a lengthy set of OR's in your SQL statement, or:
2) Since the digits are split into multiple fields simply do something like this:
Code:
Dim usrDigits
usrDigits = "1,2,3"

Dim sqlStmt
sqlStmt = "SELECT * FROM NumberTable WHERE digit1 in ("&usrDigits&") AND digit2 in ("&usrDigits&") AND digit3 in ("&usrDigits&")
The problem with the above code is that if the user has the numbers 1 1 3 and and the winning number is 1 3 3 then you will get a false match.

Assuming you just want to list all combinations of numbers:
Well, we could do this the easy way or the slightly harder way. The easy way is to hardcode it, with 3 numbers this is 6 lines, with 4 numbers this is 24 lines, and with 5 numbers this is 120 lines:
Code:
<table>
   <tr>
      <td>
         <%=usrDigit1 & usrDigit2 & usrDigit3%>
      </td>
   </tr>
   <tr>
      <td>
         <%=usrDigit1 & usrDigit3 & usrDigit2%>
      </td>
   </tr>
   <tr>
      <td>
         <%=usrDigit2 & usrDigit3 & usrDigit1%>
      </td>
   </tr>
etc...

This could be a bit of a pain to do though, so perhaps we should try to loop through the values. First put the values in a correctly sized array:
Code:
'we'll do 5 numbers to make a point
Dim myArray(4)
'we are assuming the previous page had a form with 5 inputs for the users number, txtNumber0, txtNumber1, txtNumber2, etc
Dim i
For i = 0 to 4
   myArray(i) = Request.Form(&quot;txtNumber&quot;&i)
Next

Ok so we have our 5 numbers in an array, now we need to output all the possibilities...
this is taking a bit more work than I though it would, I'll get back to you :)

-tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
No more vacation for me :(

FAQ FAQ20-2863
= new Forums.Posting.General.GettingAnswers()
 
Heh, this was fun, ok I commented the code, basically what this is doing is recursively finding all the combinations of digits number of digits using the numbers between min and max as possible values. What this allows us to do is create the array like I have above, then use the function to find all the possible combinations of array indexes (without repeating, that was the hard part):
Code:
<%
'Will return # delimited strings of comma delimited numbers
Function combos(min, max, digits)
	Dim answerString, workingString
	Dim workingArray

	Dim outer, inner

	'If there are more than 1 digit left to resolve
	If digits > 1 Then
		'recursively get all the right digit combinations
		workingString = combos(min, max, digits-1)

		'split the strings into an array so each array index will be a possible digit combination for digits-1
		workingArray = Split(workingString,&quot;#&quot;)
		
		'outer is left most number
		For outer = min to max
		
			'loop through the strings returned for right side
			For inner = 0 to UBound(workingArray)
			
				'if this number hasn't been used in the right side yet
				If InStr(workingArray(inner),outer) = 0 Then
				
					'concatenate all the remaining possible outer values onto the right sides in strings that are # delimited
					answerString = answerString & outer & &quot;,&quot; & workingArray(inner) & &quot;#&quot;
				End If
			Next
		Next
	Else

		'there is only 1 digit reminaing to resolve, simply concatenate 0 to 4 with #'s
		For outer = min to max
			answerString = answerString & outer & &quot;#&quot;
		Next
	End If

	'return the result of this function
	combos = left(answerString,len(answerString)-1)
End Function

%>

There's our function, now we need to call it. To test this out here is a small piece of code:
Code:
<table>
<%
Dim answers, answersArray
answers = combos(0,4,5)
answersArray = Split(answers,&quot;#&quot;)

Response.Write &quot;There are &quot; & UBound(answersArray)+1 & &quot; combinations.<br>&quot;

Dim i
For i = 0 to UBound(answersArray)
	Response.Write &quot;<tr><td>&quot;&i&&quot;</td><td>&quot;&answersArray(i)&&quot;</td></tr>&quot;
Next

%>
</table>
This sample code runs the function for array indexes 0 to 4 and wants 5 digit combinations back. The combinations are passed back like this:
0,1,2,3,4#0,1,2,4,3#0,1,3,2,4# etc...

So we can split on the # sign to get each individual set of indexes, then when we want to apply these to our array in the previous post we can split on the commas to retrieve the indexes. The only problem with this code is that it will not handle numbers higher than 9 for min and max and there is no error checking. If you enter less digits than appear between min and max you will have an error, for example:
combos(0,9,4) says make all the 4 digit combinations with the numbers between 0 and 9, this is legal.
combos(0,4,9) says make all the 9 digit combinations with the numbers between 0 and 4, this is not legal because there are only 5 numbers to choose from.

Now, in order to convert from the indexes to the numbers in our array (myArray from previous post with 5 user numbers in it):
Code:
<table>
<%
Dim myArray(4)
'we are assuming the previous page had a form with 5 inputs for the users number, txtNumber0, txtNumber1, txtNumber2, etc
Dim i
For i = 0 to 4
   myArray(i) = i+5
Next


Dim answers, answersArray, digitCtr, digitArray
Dim tempIndex

answers = combos(0,4,5)
answersArray = Split(answers,&quot;#&quot;)

Response.Write &quot;There are &quot; & UBound(answersArray)+1 & &quot; combinations.<br>&quot;

For i = 0 to UBound(answersArray)
	'splits the comma delimited string for this combination
	digitArray = Split(answersArray(i),&quot;,&quot;)
	
	If i mod 5 = 0 Then Response.Write &quot;<tr>&quot;

	Response.Write &quot;<td>| &quot;
	For digitCtr = 0 to UBound(digitArray)
		tempIndex = cInt(digitArray(digitCtr))
		Response.Write myArray(tempIndex) & &quot; &quot;
	Next
	Response.Write &quot;</td>&quot;

	If i mod 5 = 4 Then Response.Write &quot;</tr>&quot;
Next

%>
</table>
I substituted values in the array instead of the user values from Request.Form because this was complicated enough to require me to test it before posting. I think this may be a bit more than what you were after, but I enjoyed writing it.

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
No more vacation for me :(

FAQ FAQ20-2863
= new Forums.Posting.General.GettingAnswers()
 
Awesome.

Email me your contact info - I have money for you to develop

1) the search form
2) the search return page for this

What is your hourly rate? How much roughly to do a version of this that handles a three digit number?

AWESOME (and I like the enthusiasm you have for what you do!)

aamaker@sc.rr.com
 
I'm sorry, I would be interested but recruiting is forbidden on the forum, it's strictly for technical discussion and such.

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
No more vacation for me :(

FAQ FAQ20-2863
= new Forums.Posting.General.GettingAnswers()
 
hey! I'm enthusiastic too [sad] Tarwn's just better at this. [sad][sad][sad][sad][sad][sad][sad] ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

 
good form [smile] ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top