stutco
07-29-2003, 11:37 AM
I want to impliment a search and display function of names and addresses + other fields on a website. I really want this to be easy.
What I would like is to upload a excel or txt file and have that searchable and have the results appear in html or the like.
Any ideas?
MikeM
07-29-2003, 06:46 PM
Why not import the data into ms access and create a search using a dsnless connection??
stutco
07-30-2003, 09:28 AM
that would be great if I knew how. I just need a simple search and return of the results.
MikeM
07-30-2003, 04:06 PM
Open MS access and inport the data from excel.
Upload it to your site and set read write permissions for Iusr
Write your Html form to submit to somefile.asp
copy this code and edit the fields to suit your needs.
upload to the website.
<%
If request.querystring<>"" then
If len(request.querystring("q"))>=no of fields in table then
' Those variable are needed to work with the database
DIM DB
DIM RS
DIM StrOpen
DIM StrOpenInContruction ' This variable is used to create variable "StrOpen"
DIM PathToDatabase ' The location of our database within our server
PathToDatabase="e:\path\to\file\yourfilename.mdb"
DIM NameOfTableInDB ' The name of the table in our Access database. In our case this name is "Table1"
NameOfTableInDB="Table_name"
' The name of each colunm in the table is contained in the array "ColunmNameIntable".
' The sample database provided in this tutorial has up to 20 colunms
' with names "Data1" to "Data20" (and the first colunm, which is named "Id"
' As in this script we will not use all of them, only the ones we will use are included in this array
DIM ColunmNameIntable(5)
ColunmNameIntable(0)="ID"
ColunmNameIntable(1)="First_field"
ColunmNameIntable(2)="second_field"
ColunmNameIntable(3)="Third_field"
ColunmNameIntable(4)="etc"
' When displaying data we will use this values
DIM DataName(4)
DataName(0)="ID"
DataName(1)="First_field"
DataName(2)="second_field"
DataName(3)="Third_field"
DataName(4)="etc"
' Keywords to search will be saved to variable "TheWords"
DIM TheWords
TheWords = request.querystring("q")
' Mode will be saved to this variable. Value will be "OR" or "AND"
DIM TheMode
TheMode = request.querystring("mode")
' When TheWords contains more than one keyword,
' keywords will be save to array "TheWordsArray"
DIM TheWordsArray
DIM MorethanOneKeywords ' If more than one keywords are introduced, then we will set up MorethanOneKeywords value to "yes"
DIM Wmax ' Will be used to calculate maximum number of keywords
DIM i,j ' Just counters
If instr(1,request.querystring("q")," ",1) >0 then
MorethanOneKeywords="yes"
TheWordsArray=Split(TheWords," ")
Wmax=ubound(TheWordsArray)
For i=0 to Wmax
if len(TheWordsArray(i))<3 then
Response.write ("Search Term <b>" & TheWordsArray(i) & "</b> is very sort, so it has not been used<HR>")
TheWordsArray(i)=""
end if
next
end if
' First we will get "StrOpen", wich is latter use to filter the database
If MorethanOneKeywords="yes" then
if TheMode="OR" then
For i=0 to Wmax
if TheWordsArray(i)<>"" then
For j=1 to 2
StrOpen= StrOpen & " OR " & ColunmNameIntable(j) & " LIKE '%" & TheWordsArray(i) & "%'"
next
end if
next
end if
if TheMode="AND" then
For j=1 to 2
StrOpenInContruction=""
For i=0 to Wmax
if TheWordsArray(i)<>"" then
StrOpenInContruction= StrOpenInContruction & " AND " & ColunmNameIntable(j) & " LIKE '%" & TheWordsArray(i) & "%'"
end if
next
StrOpen=StrOpen & " OR (" & Right (StrOpenInContruction,Len(StrOpenInContruction)-5) & ")"
next
end if
else
For j=1 to 2
StrOpen= StrOpen & " OR " & ColunmNameIntable(j) & " LIKE '%" & TheWords & "%'"
next
end if
StrOpen= "SELECT * FROM " & NameOfTableInDB & " WHERE" & Right (StrOpen,Len(StrOpen)-3)
' Now, we will open the data base and perform the search
Set DB = Server.CreateObject("ADODB.Connection")
DB.Open ("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & PathToDatabase)
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open StrOpen, DB
' Finally we will show the maching records
If RS.EOF And RS.BOF Then
'If we get here it means we have selected no rows from the table in our database
Response.write ("There are 0 records.")
else
'If we get here it means we have selected one or more rows from the table in our database
RS.MoveFirst
While Not RS.EOF
'Next 3 lines will display search results. We may change the way we are displaying the results
For j=1 to 3
Response.write (DataName(j) & ": " & RS.Fields (ColunmNameIntable(j)) & "<BR>")
next
Response.write ("<HR>")
RS.MoveNext
Wend
End If
else
Response.write ("Very sort search name. Please try again.")
end if
end if %>
stutco
07-30-2003, 04:32 PM
Thanks I will try that....
MikeM
07-30-2003, 04:40 PM
Make sure that your fields and table name are one word ... or seperated with an _
Mark Elliot
07-30-2003, 09:33 PM
optionally you could save your excel file as a csv file or a tab deliminated file and use php to parse that. the explode() (http://us3.php.net/manual/en/function.explode.php) function would be useful to seperate into columns.
it's a handy idea and doesn't involve a database for such a simple procedure.
some sample code to create a 2d array:
$f = file('filename.txt');
$data = array();
for($i = 0; $i < count($f); $i++)
{
$data[$i] = explode("\t", $f[$i]);
}
Rich2k
07-31-2003, 04:32 AM
The easiest way is use the Excel 2003 XML format then you can do pretty much anything you want with it.
I found a great XSLT file yesterday for converting word documents (in 2003 XML format) to HTML.