File : select2.asp. ID : 4214
Skin : Default | Sons-of-obsidian | Sunburst | Highlighter | Frame
<%
' ###########################################################################################
' select2.asp 
' สำหรับ อ่าน table จากแฟ้ม .mdb มาแสดงทาง browser
' พัฒนาโดย บุรินทร์ รุจจนพันธุ์ webmaster@thaiall.com Mobile.0-1992-7223 Tel.0-5422-3684
' ###########################################################################################
public show_help,version,header_txt,footer_txt,bgcolor,hbgcolor,tbgcolorh,tbgcolor1,tbgcolor2
public get_db,get_rpp,get_pg,l,r,qr,sql,ofld,o,w,rs

' #### section 1 : default value of general###
show_help  = "yes"                     ' ใช้บริการแสดงข้อความอธิบายการใช้โปรแกรมนี้
version    = " :: 1.3_september_47"    ' รุ่นของการพัฒนาโปรแกรม แสดงใน title
header_txt = "โปรแกรมแสดงข้อมูล"       ' ข้อความที่แสดงบนสุดของ body ในส่วนที่เป็นพื้นดำ
footer_txt = "<hr color=blue width=400>ยินดีรับข้อเสนอแนะ e-mail: lampang@moi.go.th tel.0-5426-5027"
bgcolor    = "#ffffdd"                 ' สีพื้นทั้งหมดเป็นเหลืออ่อน
hbgcolor   = "#000000"                 ' สีพื้นที่เป็นแถบดำบนสุดของ body
tbgcolorh  = "#dddddd"                 ' สีพื้นส่วนหัวตาราง
tbgcolor1  = "#ddffff"                 ' สีพื้นบรรทัดคี่ ฟ้าอ่อน
tbgcolor2  = "#ffffff"                 ' สีพื้นบรรทัดคู่ ขาว
fontsize   = "10px"                    ' ถ้าต้องการให้อักษรใหญ่ขึ้น สัก 16px กำลังดีครับ
onlysearchresult = "yes"               ' เมื่อใช้ search= จะไม่แสดงผลอื่นนอกจากผลการสืบค้น

' ### section 2 : default value of Page and Row Per Page ###
public pagedif(5)                      ' เลขให้เลือกจำนวนของ rpp(Row Per Page)
pagedif(0) = 10
pagedif(1) = 25
pagedif(2) = 50
pagedif(3) = 100
pagedif(4) = 200
get_pg  = 1                            ' เลขหน้า ถ้าไม่กำหนดค่าให้ pg
get_rpp = 50                           ' จำนวนระเบียนข้อมูลต่อหน้า ถ้าไม่กำหนดค่าให้ rpp
if (len(request.querystring("pg")) > 0)  then get_pg = request.querystring("pg")
if (len(request.querystring("rpp")) > 0) then get_rpp = request.querystring("rpp")

' ### section 3 : Security of database name ###
' ใช้มือถือของผมเป็นชื่อ database จะไม่มีใครรู้ชื่อ database และนำไปสู่การอาจแอบคัดลอกข้อมูลได้
' ถ้าเก็บ database ไว้ห้องอื่นที่ไม่ใช่ web folder ก็ทำได้ตรง ๆ เลยดังตัวอย่าง เบอร์โทรที่บ้าน
if (len(request.querystring) > 0) then
  get_db = server.mappath(request.querystring("db"))
  if (request.querystring("db") = "019927223") then get_db = server.mappath("dthai97.mdb")
  if (request.querystring("db") = "054223684") then get_db = "D:\thaiall.com\asp\dthai97.mdb"
end if

' ### section 4 : Main procedure ###
main()
footer()
' ###########################################################################################

' ### function 1 ###
function main()
%><html><head><meta http-equiv=content-type content="text/html; charset=windows-874"><title><%
response.write(request.querystring("title") & " " & version) 
%></title><style type="text/css">
body{scrollbar-base-color:red;scrollbar-arrow-color:white;font-family:ms sans serif,microsoft sans serif;font-size:<%=fontsize %>}
td{font-family:ms sans serif,microsoft sans serif;font-size:<%=fontsize %>}
</style></head><body bgcolor=<%=bgcolor %> topmargin=0 leftmargin=0>
<table width=100% cellpadding=0 cellspacing=0><td bgcolor=<%=hbgcolor %>><font color=white><%=header_txt %></td></table>
<%
if (len(request.querystring) > 0) then
  response.write("<center>")
  if (len(request.querystring("search")) > 0) then 
    response.write "<form action=select2.asp?" & request.querystring & " method=post>"
    response.write "เลือกข้อมูล <input name=key_" & request.querystring("search") & " value='"
    if (len(request.form) > 0) then response.write(request.form(1)) 
    response.write "'>"
    if (len(request.querystring("searchbar")) > 0) then 
      response.write ("<input type=submit value='" & request.querystring("searchbar") & "'>")
    else
      response.write ("<input type=submit value='ค้นหาตาม " & request.querystring("search") & "'>")
    end if
    response.write "</form>"
    if (len(request.form) = 0 and onlysearchresult = "yes") then exit function
  end if  
set conn = server.createobject("adodb.connection")
conn.open "provider=microsoft.jet.oledb.4.0; data source=" & get_db
sql = "select * from " + request.querystring("tb") + " "
if (len(request.form) > 0 or len(request.querystring("where")) > 0) then 
  found_where = 0
  sql = sql + "where "
  if (len(request.querystring("search")) > 0) then 
    sql = sql + request.querystring("search") +" like '%" + request.form(1) + "%' "
    found_where = 1
  end if
  if (len(request.querystring("where")) > 0) then
    if (found_where = 1) then sql = sql + " and "
     w = split(request.querystring("where"),",")
     sql = sql + w(0) +" like '%" + w(1) + "%' "    
  end if
end if
if (len(request.querystring("order1a")) > 0 or len(request.querystring("order1d")) > 0) then
  if (len(request.querystring("order1a")) > 0) then 
    sql = sql + "order by " + request.querystring("order1a") + " "
  else
    sql = sql + "order by " + request.querystring("order1d") + " desc "
  end if
  if (len(request.querystring("order2a")) > 0 or len(request.querystring("order2d")) > 0) then
    if (len(request.querystring("order2a")) > 0) then 
      sql = sql + ", " + request.querystring("order2a") + " "
    else
      sql = sql + ", " + request.querystring("order2d") + " desc "
    end if
  end if
end if
set rs = server.createobject("ADODB.recordset")
' -----------------------
rs.open sql,conn, 1, 3
' บางครั้ง error บรรทัดข้างบนนี้ เพราะชื่อเขตข้อมูลเป็นคำสงวน 
' เช่น position หรือ desc วิธีแก้ไขคือท่านต้องเปลี่ยนชื่อเขตข้อมูลใน table เป็นชื่ออื่น
' -----------------------
if (rs.recordcount = 0) then 
  response.write("<hr color=blue>NOT FOUND<br><br><b>SQL : " + sql + "<hr color=blue>")
else
response.write ("<b>จำนวนระเบียนข้อมูล</b> : " & cstr(rs.recordcount) & " ระเบียน ")
if (len(request.form) = 0) then
  if (cint(get_rpp) > 0) then rs.pagesize = cint(get_rpp) else rs.pagesize = 5
  response.write ("<b>จำนวนระเบียนข้อมูลต่อหน้า</b> : " & rs.pagesize & " ระเบียน ")
  response.write ("<b>เลือกจำนวนระเบียนข้อมูล/หน้า</b> : ")
  for i = 0 to 4
    response.write " <"
    if (cint(get_rpp) = pagedif(i)) then response.write("! ")
    response.write "a href=select2.asp?"
    if (instr(request.querystring,"pg=") = 0) then 
      response.write("pg=1&")
      qr = request.querystring
	else
      qr = replace(request.querystring,"pg="& get_pg & "&","pg=1&")
	end if
    if (instr(qr,"rpp=") = 0) then response.write("rpp=" & pagedif(i) & "&")
    response.write(replace(qr,"rpp="& get_rpp,"rpp=" & pagedif(i)))
    response.write(">" & pagedif(i) & "</a> |")
  next
  response.write ("<br><b>เลือกหน้าที่</b> : ")
  for i = 1 to rs.pagecount
    response.write " <"
    if (cint(get_pg) = i) then response.write("! ")
    response.write "a href=select2.asp?"
    response.write(replace(request.querystring,"pg="& get_pg,"pg=" & i))
    if (instr(request.querystring,"pg=") = 0) then response.write("&pg=" & i)
    if (instr(request.querystring,"rpp=") = 0) then response.write("&rpp=" & get_rpp)
    response.write(">" & i & "</a> |")
  next
  rs.absolutepage = get_pg
end if
if (instr(request.querystring,"seq=yes") = 0) then 
  response.write(" <a href=select2.asp?" & request.querystring &"&seq=yes>ลำดับ</a>")
end if
response.write("<table cellspacing=2 cellpadding=2><tr bgcolor=" & tbgcolorh & ">")
if (request.querystring("seq") = "yes") then response.write("<td>ลำดับ</td>")
for i = 1 to 40
  oh(i)
next
response.write("</tr>")
l = (get_pg - 1) * get_rpp
r = 0
rs.movefirst
do while not rs.eof
  r = r + 1
  l = l + 1
  if (r mod 2 = 0) then response.write("<tr bgcolor=" & tbgcolor2 & ">") else response.write("<tr bgcolor=" & tbgcolor1 & ">")
  if (request.querystring("seq") = "yes") then response.write("<td>"& l &"</td>")
  for i = 1 to 40
    od(i)
  next 
  response.write("</tr>")
  if (r >= cint(get_rpp) and len(request.form) = 0) then rs.movelast  
  rs.MoveNext
loop
response.write("</table>")
conn.close
set rs = nothing
set conn = nothing
end if
end if 
end function
' ###########################################################################################

' ### function 2 ###
function od(id)
  ofld = request.querystring("f" & id)
  if (len(ofld) > 0) then 
    if(asc(left(ofld,1)) < 65) then o = rs.fields(cint(ofld)) else o = rs.fields(cstr(ofld))
      if (len(request.form) > 0 and len(ofld) > 1) then 
        if (ofld = request.querystring("search")) then o = replace(o&"",request.form(1),"<b>"& request.form(1) &"</b>")
      end if
    response.write("<td>"& o &"</td>")
  end if	
end function
' ###########################################################################################

' ### function 3 ###
function oh(id)
  ofld = request.querystring("f" & id)
  if (len(ofld) > 0) then 
    if (len(request.querystring("hf" & id)) > 0) then
      o = request.querystring("hf" & id)
    else 
      if(asc(left(ofld,1)) < 65) then o = "field " & (ofld + 1) else o = ofld
    end if
    response.write("<td>"& o &"</td>")
  end if	
end function
' ###########################################################################################

' ### function 4 ###
function footer()
  if (show_help = "yes") then
    if (len(request.querystring) = 0) then show_help_detail()  
    response.write footer_txt
    response.write " &nbsp; <a href=select2.asp>?</a>"
    response.write chr(13) & "<! " & sql & ">"
  else
    response.write footer_txt
  end if
  %>
  <br>
  <!--BEGIN WEB STAT CODE---->
  <SCRIPT LANGUAGE="javascript1.1">page="govapply";</SCRIPT>
  <! SCRIPT LANGUAGE="javascript1.1" src="http://truehits.gits.net.th/data/k0020653.js"><! /SCRIPT>
  <!-- END WEBSTAT CODE -->  
  </body></html><%
end function
' ###########################################################################################

' ### function 5 ###
function show_help_detail()
%><ul><font size=4><u><b>SELECT2.ASP</b></u><br>
:: ถูกเขียนเพื่อให้ สำนักงานจังหวัด นำไปเผยแพร่ให้ส่วนราชการต่าง ๆ นำไปใช้ประโยชน์<hr color=black width=80% align=left>
<b>1. โปรแกรมนี้ เน้นการอ่าน table ใน .mdb ไปแสดงผล หรือสืบค้นตามเขตข้อมูล</b> 
<dd>สามารถอ่านตารางข้อมูล(Table) จาก MS ACCESS(.MDB) 
เพื่อนำข้อมูลไปแสดงผลเป็นตารางใน Internet explorer(IE) สามารถนำใช้ได้กับส่วนราชการต่าง ๆ กว่า 120 หน่วยงาน โดยไม่จำเป็นต้องปรับปรุง Source code 
การใช้โปรแกรมนี้ในรูปแบบต่าง ๆ สามารถทำได้โดยการเปลี่ยนคำสั่งเรียกใช้ใน URL หรือการสร้าง web link ให้ตรงกับความต้องการ
<br><b>2. สิ่งที่จำเป็นต่อการใช้โปรแกรมนี้ ของหน่วยงาน</b>
<dd>2.1 มี web server ที่เป็น IIS หรือ PWS เพราะถ้ามีเฉพาะ select2.asp จะนำใช้งานไม่ได้ ถ้าสำนักงานจังหวัดเตรียมให้ ก็ใช้ของเขา
<dd>2.2 มีคนในหน่วยงานที่สามารถใช้ MS ACCESS และรู้ว่าหน่วยงานของตน มีข้อมูลอะไรบ้าง ที่ควรเผยแพร่
<dd>2.3 มีตารางข้อมูล(Table) ซึ่งจัดเก็บใน MS ACCESS(.mdb) และรู้ชื่อเขตข้อมูลต่าง ๆ 
<br><b>3. ตัวอย่างภาษา html ที่นำไป ใส่ในเว็บเพจ ของท่าน</b>
<table bordercolor=black border=1 align=center><td bgcolor=white><font face=fixedsys>&lt;body&gt;
<br>&lt;a href=<font color=red>select2.asp?db=dthai97.mdb&tb=tbthai&f1=fname</font>&gt;<font color=blue>Name of CEO</font>&lt;/a&gt;
<br>&lt;a href=<font color=red>select2.asp?db=dthai97.mdb&tb=tbthai&f1=fname</font>&gt;<font color=blue face="ms sans serif" size=0>รายชื่อหัวหน้าส่วนราชการ</font>&lt;/a&gt;
<br>&lt;/body&gt;
</font>
</td></table>
<br><b>4. ตัวอย่างการเรียกใช้ select2.asp แบบต่าง ๆ</b><ol><font size=0> 
<li><a href=select2.asp?db=dthai97.mdb&tb=tbthai&f1=fname>select2.asp?db=dthai97.mdb&tb=tbthai&f1=fname</a>
<li><a href=select2.asp?seq=yes&db=dthai97.mdb&tb=tbthai&f1=fname>select2.asp?seq=yes&db=dthai97.mdb&tb=tbthai&f1=fname</a>
<li><a href=select2.asp?title=แสดงข้อมูล&rpp=15&pg=1&db=dthai97.mdb&tb=tbthai&f1=0&f2=1&f3=2>select2.asp?title=แสดงข้อมูล&rpp=15&db=dthai97.mdb&tb=tbthai&f1=0&f2=1&f3=2</a>
<li><a href=select2.asp?rpp=5&pg=1&db=dthai97.mdb&tb=tbthai&f1=fid&f2=fname&f3=fsalary>select2.asp?rpp=5&db=dthai97.mdb&tb=tbthai&f1=fid&f2=fname&f3=fsalary</a>
<li><a href=select2.asp?rpp=5&db=dthai97.mdb&tb=tbthai&f1=fid&f2=fname&hf1=รหัสสมาชิก&hf2=ชื่อสมาชิก>select2.asp?rpp=5&db=dthai97.mdb&tb=tbthai&f1=fid&f2=fname&hf1=รหัสสมาชิก&hf2=ชื่อสมาชิก</a>
<li><a href=select2.asp?rpp=5&pg=1&db=dthai97.mdb&tb=tbthai&f1=fid&f2=fname&f3=fsalary&search=fname>select2.asp?rpp=5&db=dthai97.mdb&tb=tbthai&f1=fid&f2=fname&f3=fsalary&search=fname</a>
<li><a href=select2.asp?rpp=5&pg=1&db=dthai97.mdb&tb=tbthai&f1=fname&search=fname&searchbar=ค้นหาตามชื่อ>select2.asp?rpp=5&pg=1&db=dthai97.mdb&tb=tbthai&f1=fname&search=fname&searchbar=ค้นหาตามชื่อ</a>
<li><a href=select2.asp?db=019927223&tb=tbthai&f1=fname&search=fname>select2.asp?db=019927223&tb=tbthai&f1=fname&search=fname</a>
<li><a href=select2.asp?db=054223684&tb=tbthai&f1=fname&hf1=ชื่อบุคลากร>select2.asp?db=054223684&tb=tbthai&f1=fname&hf1=ชื่อบุคลากร</a>
<li><a href=select2.asp?db=054223684&tb=tbthai&f1=fname&order1a=fname>select2.asp?db=054223684&tb=tbthai&f1=fname&order1a=fname</a>
<li><a href=select2.asp?db=dthai97.mdb&tb=tbthai&f1=fname&order1d=fname&seq=yes>select2.asp?db=dthai97.mdb&tb=tbthai&f1=fname&order1d=fname</a>
<li><a href=select2.asp?db=dthai97.mdb&tb=tbthai&f1=fname&f2=fsalary&order1a=fname&order2d=fsalary>select2.asp?db=dthai97.mdb&tb=tbthai&f1=fname&f2=fsalary&order1a=fname&order2d=fsalary</a>
<li><a href=select2.asp?db=govapply.mdb&tb=listname&f1=pos&f2=quizid&f3=name&f4=surname&f5=qaddr&rpp=100&seq=yes&search=name>ค้นหารายชื่อผู้มีสิทธิ์สอบ อบต จากข้อมูลกว่า 20000 ระเบียน</a>
<li><a href=select2.asp?db=govapply.mdb&tb=listname&f1=pos&f2=quizid&f3=name&f4=surname&f5=qaddr&rpp=100&seq=yes&where=qaddr,ร.ร.บุญวาทย์วิทยาลัย>แสดงรายชื่อผู้มีสิทธิ์สอบ เฉพาะสถาบัน</a>
<li><a href=select2.asp?db=govapply.mdb&tb=listname&f1=pos&f2=quizid&f3=name&f4=surname&f5=qaddr&rpp=100&seq=yes&where=qaddr,ร.ร.บุญวาทย์วิทยาลัย%20อาคารจิตรภัสร์%20ห้อง%20723&search=name>ค้นหารายชื่อผู้มีสิทธิ์สอบ อบต จากห้องสอบด้วย where</a>
</font></ol>
<br><b>5. ตัวอย่าง Keyword ของ web link ที่ผู้ใช้สามารถกำหนดเงื่อนไข ในการแสดงรายงาน</b>
<table width=95% bgcolor=#ddffdd><td valign=top>
<ol>
<li><b>db=dthai97.mdb</b> กำหนด database ตรง ๆ เก็บในห้องเดียวกับ asp
<li><b>db=019927223</b> กำหนด database ทางอ้อม เพื่อความปลอดภัย
<li><b>tb=tax</b> กำหนดชื่อตารางที่เก็บใน .mdb เช่นตารางชื่อ tax
<li><b>f1=fname</b> กำหนดชื่อเขตข้อมูลที่จะนำมาแสดง เป็นหลักแรก
<li><b>f2=5</b> กำหนดเขตข้อมูลที่ 5 ของตาราง มาแสดง เป็นหลักที่สอง
<li><b>f40=salary</b> กำหนดเขตข้อมูลได้สูงสุด 40 หลัก
<li><b>hf1=ชื่อพนักงาน</b> กำหนดคำอธิบายหลักแรก ในส่วน header
<li><b>hf2=สถานะ</b> กำหนดคำอธิบายหลักที่สอง ในส่วน header
<li><b>title=สำนักงานจังหวัด</b> กำหนดชื่อหน่วยงาน ที่อยู่บนสุดของ IE
</ol>
</td><td valign=top>
<ol>
<li><b>search=fname</b> กำหนดเขตข้อมูลที่ต้องการค้นหา เช่น fname
<li><b>searchbar=ค้นหาตามชื่อ</b> กำหนดคำอธิบาย ในปุ่มสำหรับสืบค้น
<li><b>rpp=10</b> กำหนดจำนวนระเบียนต่อหน้า หรือจำนวนบรรทัดต่อหน้า
<li><b>pg=1</b> กำหนดเลขหน้าที่ต้องการแสดงผล
<li><b>order1a=fname</b> จัดเรียงตาม fname แบบน้อยไปมาก เป็น field แรก
<li><b>order1d=fname</b> จัดเรียงตาม fname แบบมากไปน้อย เป็น field แรก
<li><b>order2a=fname</b> จัดเรียงตาม fname แบบน้อยไปมาก เป็น field ที่สอง
<li><b>order2d=fname</b> จัดเรียงตาม fname แบบมากไปน้อย เป็น field ที่สอง
<li><b>seq=yes</b> กำหนดให้แสดงลำดับ เป็นหลักแรก
<li><b>where=qaddr,ร.ร.บุญวาทย์</b> เลือกสถานที่สอบ
</ol>
</td></table>
<br><b>6. ตัวอย่าง Keyword ที่สามารถแก้ไขในโปรแกรม select2.asp</b> (ต้องแก้ไขใน source code)
<table width=95% bgcolor=#ddffdd><td valign=top>
<ol>
<li><b>show_help  = "yes"</b> ใช้บริการแสดงข้อความอธิบายการใช้โปรแกรมนี้
<li><b>version    = " :: 1.2_june_47"</b> รุ่นของการพัฒนาโปรแกรม แสดงใน title
<li><b>header_txt = "โปรแกรมแสดงข้อมูล"</b> ข้อความที่แสดงบนสุดของ body ในส่วนที่เป็นพื้นดำ
<li><b>footer_txt = "ยินดีรับข้อเสนอแนะ"</b> ข้อความที่แสดงล่างสุดของ body
<li><b>bgcolor    = "#ffffdd"</b> สีพื้นทั้งหมดเป็นเหลืออ่อน
<li><b>hbgcolor   = "#000000"</b> สีพื้นที่เป็นแถบดำบนสุดของ body
<li><b>tbgcolorh  = "#dddddd"</b> สีพื้นส่วนหัวตาราง
<li><b>tbgcolor1  = "#ddffff"</b> สีพื้นบรรทัดคี่ ฟ้าอ่อน
<li><b>tbgcolor2  = "#ffffff"</b> สีพื้นบรรทัดคู่ ขาว
<li><b>fontsize   = "10px"</b> ถ้าต้องการให้อักษรใหญ่ขึ้น สัก 16px กำลังดีครับ
<li><b>onlysearchresult = "yes"</b> เมื่อใช้ search= จะไม่แสดงผลอื่นนอกจากผลการสืบค้น
<li><b>pagedif(4) = 200</b> เลขให้เลือกจำนวนของ rpp(Row Per Page)
<li><b>get_pg  = 1</b> เลขหน้า ถ้าไม่กำหนดค่าให้ pg
<li><b>get_rpp = 10</b> จำนวนระเบียนข้อมูลต่อหน้า ถ้าไม่กำหนดค่าให้ rpp
</ol>
</td></table>
<br>7. http://www.thaiall.com/source เป็นที่เก็บ source code ของ select2.asp
<br>8. http://www.thaiall.com/asp เป็นที่เก็บตัวอย่างแฟ้มข้อมูลชื่อ dbthai97.mdb
<!--
' การเชื่อมต่อของโปรแกรมนี้ สามารถใช้คำสั่ง <b>conn.execute("delete * from tbthai")</b> ถ้าคิดจะใช้
' ถ้าใช้คำสั่ง set rs = conn.execute(sql) จะใช้ rs.recordcount ไม่ได้
-->
</font></ul><center><%
end function
' ###########################################################################################
%>