Tìm kiếm với hàm instr trong ASP
Trong lập trình ASP với cơ sở dữ liệu để tìm kiếm, ta đã quen với việc dùng câu lệnh Like để tìm kiếm. Hôm nay tôi trình bày một kinh nghiệm sử dụng hàm instr có sẵn trong ASP để hỗ trợ việc tìm kiếm.
Xem xét hàm tim_kiem sau
function tim_kiem(noi_dung, chuoi_tim,dieu_kien)
dim keywords,keycnt,kq_1,kq_2,kq,kq_tam
rem đầu tiên đưa các từ cần tìm vào 1 mảng
keywords = split(chuoi_tim," ")
rem đếm số từ cần tìm
keycnt = ubound(keywords)
rem gán các giá trị mặc định cho các biến
kq_1=0
kq_2=0
kq=0
kq_1=instr(1,noi_dung,chuoi_tim,vbTextCompare)
kq_tam=0
For Each word in keywords
kq_2 = instr(1,noi_dung,word,vbTextCompare)
if kq_2>0 then kq_tam=kq_tam+1 end if
next
select case dieu_kien
rem nếu điều kiện là “tìm chính xác cụm từ” thì kết quả chính là kq_1
case "phrase":kq=kq_1
rem nếu điều kiện là “tìm tất cả từ” thì nếu kết quả tạm lớn hơn số từ +1 là đúng
case "and":if kq_tam>=keycnt + 1 then kq=1 else kq=0 end if
rem nếu điều kiện là “tìm ít nhất một từ” thì nếu kết quả tạm lớn hơn 1 là đúng
case "or":if kq_tam>=1 then kq=1 else kq=0 end if
end select
tim_kiem=kq
end function
Giải thích
Hàm Instr(số_bắt_đầu_tìm, nội_dung,chuỗi_tìm,tùy_chọn) sẽ trả về số thứ tự tìm thấy chuỗi_tìm trong nội_dung.
Do đó, trong hàm tìm kiếm, đầu tiên ta tách các từ cần tìm vào một mảng tên là keywords. Sau đó kq_1 sẽ là tìm nguyên chuỗi trong nội dung, nếu xuất hiện chuỗi này trong nội dung, kq_1 sẽ lớn hơn 1. Do đó, với điều kiện “tìm chính xác cụm từ”, ta chỉ đơn giản dùng ngay kq_1.
Với điều kiện “Tìm ít nhất một từ”, ta thấy kq_2 được tăng lên sau từng vòng lặp bằng với số từ tìm kiếm. Do đó chỉ cần kq_2 lớn hơn 1 thì kết quả tìm kiếm sẽ là đúng.
Với điều kiện “tìm tất cả các từ” thì kq_2 ít nhất phải bằng số từ cần tìm, do đó với điều kiện kq_2>=keycnt+1 thì cho kết quả đúng.
Phạm vi ứng dụng
Với một số đặc thù công việc tìm kiếm, đôi khi ta cần tìm một lúc trên nhiều trường trong cơ sở dữ liệu, việc sử dụng mệnh đề hoặc (OR) và mệnh đề và (AND) kết hợp với câu lệnh Like trong cơ sở dữ liệu sẽ làm cho hệ thống chạy chậm và câu lệnh phức tạp. Việc đưa một phần công việc tìm kiếm cho ASP xử lý sẽ giúp cơ sở dữ liệu chạy nhanh hơn và kết quả tìm kiếm sẽ tốt hơn.
Bình luận
free poker money
free poker games
free poker download
blackjack free
download casino
strip black jack
bonus poker
free monopoly slots
free online slot games
online bingo games
world series poker
casino free online
cleopatra slot machine
blackjack download
free texas hold em
sim slots
roulette free games
on line casinos
free texas holdem online
free download poker
slots for free
free casino money
free casino online
casinos on line
slots on line
free black jack game
free poker on line
play poker online
no deposit casino bonuses
online black jack
casino free
poker game online
bonus casino online
blackjack for free
free slot games online
free slot machine game
video poker free
free poker websites
online slot machines
video slot machines
cricket free ringtones
ringtone uploader
free ringtones
ringtones for cingular
24 ctu ringtone
free slot machines
3 card poker
online black jack
absolute poker
slot machine game
free texas hold em
free online poker games
free money casino
slot machine game
blackjack game
ringtones nokia
free cellular south ringtones
free centennial wireless ringtones
free composer ringtones
free gospel ringtones
download poker game
new online casinos
play free roulette
sim slots
free blackjack online
voice ringtones
free cellular south ringtones
cell phone ringtones
crazy frog ringtones
free ringtones for nokia
no deposit online casinos