|
<% ' โปรแกรม select4.asp ' ปรับปรุง : 2552-12-29 (เพิ่มเรื่องกำหนดชื่อใน xls) ' โปรแกรมอ่านข้อมูลใน table จากแฟ้ม .mdb หรือ .xls มาแสดงทาง Browser (Internet Explorer) ' พัฒนาโดย บุรินทร์ รุจจนพันธุ์ ให้หน่วยงานหนึ่งในลำปาง ' ' ตอนที่ 1 : ประกาศตัวแปร public show_help,version,header_txt,footer_txt,bgcolor,hbgcolor,tbgcolorh,tbgcolor1,tbgcolor2 public get_db,get_rpp,get_pg,get_hdr,l,r,qr,sql,ofld,o,w,rs ' ตอนที่ 2 : กำหนดข้อความ และสีพื้น proname = "select4.asp" version = "select4.asp :: version 4.1_nov_52a" show_help = "yes" ' ใช้บริการแสดงข้อความอธิบายการใช้โปรแกรมนี้ header_txt = "โปรแกรมแสดงข้อมูล" ' ข้อความที่แสดงบนสุดของ body ในส่วนที่เป็นพื้นดำ footer_txt1= "<hr color=blue width=400>" footer_txt2= "ยินดีรับข้อเสนอแนะ" footer_txt3= "<br>:: <a href=?>help</a> | <a href=http://www.thaiall.com/perlphpasp/source.pl?key=4215>source code</a> ::" bgcolor = "#ffffdd" ' สีพื้นทั้งหมดเป็นเหลืออ่อน hbgcolor = "#000000" ' สีพื้นที่เป็นแถบดำบนสุดของ body tbgcolorh = "#dddddd" ' สีพื้นส่วนหัวตาราง tbgcolor1 = "#ddffff" ' สีพื้นบรรทัดคี่ ฟ้าอ่อน tbgcolor2 = "#ffffff" ' สีพื้นบรรทัดคู่ ขาว fontsize = "10pt" ' ถ้าต้องการให้อักษรใหญ่ขึ้น สัก 16px กำลังดีครับ onlysearchresult = "yes" ' เมื่อใช้ search= จะไม่แสดงผลอื่นนอกจากผลการสืบค้น ' ตอนที่ 3 : กำหนดสีขนาดคอลัมภ์ 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 get_hdr = "yes" ' กำหนด ชื่อ field มาพร้อมกับข้อมูลในแฟ้ม .xls if (len(request.querystring("pg")) > 0) then get_pg = request.querystring("pg") if (len(request.querystring("rpp")) > 0) then get_rpp = request.querystring("rpp") if (len(request.querystring("hdr")) > 0) then get_hdr = request.querystring("hdr") if (len(request.querystring("header")) > 0) then header_txt = request.querystring("header") ' ตอนที่ 4 : ความปลอดภัยของข้อมูล ' ใช้มือถือของผมเป็นชื่อ database จะไม่มีใครรู้ชื่อ database และนำไปสู่การอาจแอบคัดลอกข้อมูลได้ ' ถ้าเก็บ database ไว้ห้องอื่นที่ไม่ใช่ web folder ก็ทำได้ตรง ๆ เลยดังตัวอย่าง เบอร์โทรที่บ้าน if (len(request.querystring) > 0) then get_db = server.mappath(request.querystring("db")) if (request.querystring("db") = "0819927223") then get_db = server.mappath("dthai97.mdb") if (request.querystring("db") = "054223684") then get_db = "D:\thaiall.com\asp\dthai97.mdb" end if ' ตอนที่ 5 : เรียกใช้ฟังก์ชัน main() footer() ' ฟังก์ชันที่ 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="& proname &"?"& 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") if (right(get_db,3) = "mdb") then conn.open "provider=microsoft.jet.oledb.4.0; data source=" & get_db else if (get_hdr = "yes") then conn.open "provider=microsoft.jet.oledb.4.0; data source=" & get_db & "; extended properties='excel 8.0; hdr=yes';" else conn.open "provider=microsoft.jet.oledb.4.0; data source=" & get_db & "; extended properties='excel 8.0; hdr=no';" end if end if 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) 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> : ") for i = 0 to 4 response.write " <" if (cint(get_rpp) = pagedif(i)) then response.write("! ") response.write "a href=?" 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=?" 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 end if if (instr(request.querystring,"seq=yes") = 0) then response.write(" <a href=?" & 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 255 oh(i) next response.write("</tr>") l = (get_pg - 1) * get_rpp last = l r = 0 cnt = 0 rs.movefirst do while not rs.eof cnt = cnt + 1 if (cnt > last) then 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 255 od(i) next response.write("</tr>") end if 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 ' ฟังก์ชันที่ 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 ' ฟังก์ชันที่ 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 ' ฟังก์ชันที่ 4 function footer() if (show_help = "yes") then if (len(request.querystring) = 0) then show_help_detail() end if response.write footer_txt1 response.write footer_txt2 response.write footer_txt3 %> <br> </body></html><% end function ' ฟังก์ชันที่ 5 function show_help_detail() %><ul><font size=4><u><b>โปรแกรมอ่านข้อมูลจากตารางมาแสดงผล (Select4.asp)</b></u><br><font color=red> <b>1. โปรแกรมสำหรับอ่านข้อมูลจากตารางใน .mdb หรือ .xls มาแสดงผล และสืบค้นตามเขตข้อมูล</b> <dd>สามารถอ่านตารางข้อมูล(Table) จาก Ms Access(.mdb) หรือ Ms Excel(.xls) เพื่อนำข้อมูลมาแสดงผลด้วยโปรแกรมประเภทบราวเซอร์ (Ex. IE, FireFox) <br><b>2. สิ่งที่จำเป็นต่อการใช้โปรแกรมนี้ ของหน่วยงาน</b> <dd>2.1 มีเครื่องบริการเว็บที่ใช้ IIS หรือ PWS จะใช้ Apache หรือ Appserv หรือ Thaiabc ไม่ได้ <dd>2.2 มีโปรแกรม Ms Access หรือ Ms Excel สำหรับสร้างตารางข้อมูล <dd>2.3 มีข้อมูลจัดเก็บในตารางข้อมูล โดยรู้ชื่อตาราง หรือชื่อพื้นที่เก็บข้อมูล <br><b>3. ตัวอย่างนำไปใช้</b> <br>3.1 นำไปใส่ในเว็บเพจของท่าน หรือโฮมเพจของหน่วยงาน <table bordercolor=black border=0><td bgcolor=white><dd><body> <dd><a href=<font color=red>select4.asp?db=dthai97.mdb&tb=tbthai&f1=fname</font>><font color=blue>Name of CEO</font></a> <dd><a href=<font color=red>select4.asp?db=dthai97.mdb&tb=tbthai&f1=fname</font>><font color=blue face="microsoft sans serif">รายชื่อหัวหน้าส่วนราชการ</font></a> <dd></body></font> </td></table> 3.2 เรียกใช้ Address โดยตรง ถ้าข้อมูลอยู่ในแฟ้ม .mdb <table bordercolor=black border=0><td bgcolor=white><pre> 1. <a href=?db=dthai97.mdb&tb=tbthai&f1=fname>select4.asp?db=dthai97.mdb&tb=tbthai&f1=fname</a> 2. <a href=?seq=yes&db=dthai97.mdb&tb=tbthai&f1=fname>select4.asp?seq=yes&db=dthai97.mdb&tb=tbthai&f1=fname</a> 3. <a href=?title=แสดงข้อมูล&rpp=15&pg=1&db=dthai97.mdb&tb=tbthai&f1=0&f2=1&f3=2>select4.asp?title=แสดงข้อมูล&rpp=15&db=dthai97.mdb&tb=tbthai&f1=0&f2=1&f3=2</a> 4. <a href=?rpp=5&pg=1&db=dthai97.mdb&tb=tbthai&f1=fid&f2=fname&f3=fsalary>select4.asp?rpp=5&db=dthai97.mdb&tb=tbthai&f1=fid&f2=fname&f3=fsalary</a> 5. <a href=?rpp=5&db=dthai97.mdb&tb=tbthai&f1=fid&f2=fname&hf1=รหัสสมาชิก&hf2=ชื่อสมาชิก>select4.asp?rpp=5&db=dthai97.mdb&tb=tbthai&f1=fid&f2=fname&hf1=รหัสสมาชิก&hf2=ชื่อสมาชิก</a> 6. <a href=?rpp=5&pg=1&db=dthai97.mdb&tb=tbthai&f1=fid&f2=fname&f3=fsalary&search=fname>select4.asp?rpp=5&db=dthai97.mdb&tb=tbthai&f1=fid&f2=fname&f3=fsalary&search=fname</a> 7. <a href=?rpp=5&pg=1&db=dthai97.mdb&tb=tbthai&f1=fname&search=fname&searchbar=ค้นหาตามชื่อ>select4.asp?rpp=5&pg=1&db=dthai97.mdb&tb=tbthai&f1=fname&search=fname&searchbar=ค้นหาตามชื่อ</a> 8. <a href=?db=0819927223&tb=tbthai&f1=fname&search=fname>select4.asp?db=0819927223&tb=tbthai&f1=fname&search=fname</a> 9. <a href=?db=054223684&tb=tbthai&f1=fname&hf1=ชื่อบุคลากร>select4.asp?db=054223684&tb=tbthai&f1=fname&hf1=ชื่อบุคลากร</a> 10. <a href=?db=054223684&tb=tbthai&f1=fname&order1a=fname>select4.asp?db=054223684&tb=tbthai&f1=fname&order1a=fname</a> 11. <a href=?db=dthai97.mdb&tb=tbthai&f1=fname&order1d=fname&seq=yes>select4.asp?db=dthai97.mdb&tb=tbthai&f1=fname&order1d=fname</a> 12. <a href=?db=dthai97.mdb&tb=tbthai&f1=fname&f2=fsalary&order1a=fname&order2d=fsalary>select4.asp?db=dthai97.mdb&tb=tbthai&f1=fname&f2=fsalary&order1a=fname&order2d=fsalary</a> </pre></td></table> 3.3 เรียกใช้ Address โดยตรง ถ้าข้อมูลอยู่ในแฟ้ม .xls <table bordercolor=black border=0><td bgcolor=white><pre> 1. <a href=?db=dthai97.xls&tb=dthai1&f1=1&hdr=no&rpp=25&hf1=ชื่อ>select4.asp?db=dthai97.xls&tb=dthai1&f1=1&hdr=no&rpp=25&hf1=ชื่อ</a> </pre></td></table> <b>4. แหล่งทดสอบโปรแกรมนี้แบบออนไลน์</b> <dd>+ <a href=http://www.thaiabc.com/asp/select4.asp>http://www.thaiabc.com/asp/select4.asp</a> <br> <b>5. Keyword สำหรับกำหนดประกอบการเรียกใช้ ให้ได้รายงานตามต้องการ</b> <table width=95% bgcolor=#ddffdd><td valign=top><pre> 1. <b>db=dthai97.mdb</b> กำหนดชื่อฐานข้อมูลในห้องเดียวกับ script 2. <b>db=0819927223</b> กำหนดฐานข้อมูลทางอ้อม ปิดชื่อฐานข้อมูล 3. <b>tb=tax</b> กำหนดชื่อตารางที่เก็บข้อมูล 4. <b>f1=fname</b> กำหนดชื่อเขตข้อมูลที่จะนำมาแสดง เป็นหลักแรก 5. <b>f2=5</b> กำหนดลำดับเขตข้อมูลมาแสดงเป็นหลักที่สอง(เริ่มจาก0) 6. <b>f255=salary</b> กำหนดเขตข้อมูลได้สูงสุด 255 หลัก 7. <b>hf1=ชื่อพนักงาน</b> กำหนดคำอธิบายหลักแรก ในส่วน header 8. <b>hf2=สถานะ</b> กำหนดคำอธิบายหลักที่สอง ในส่วน header 9. <b>title=สำนักงานจังหวัด</b> กำหนดชื่อหน่วยงาน ใน title ของ IE 10. <b>header=รายงานผลสอบอบต</b> กำหนดคำอธิบาย แทน โปรแกรมแสดงข้อมูล 11. <b>hdr=no</b> กำหนดชื่อใน .xls ว่าไม่มี header (only of xls) 12. <b>hdr=yes</b> กำหนดชื่อใน .xls ทำให้บรรทัดแรกไม่มาแสดงผล </td><td valign=top><pre> 13. <b>search=fname</b> กำหนดเขตข้อมูลที่ต้องการค้นหา 14. <b>searchbar=ค้นหาตามชื่อ</b> กำหนดคำอธิบายประกอบปุ่มสืบค้น 15. <b>rpp=10</b> กำหนดจำนวนบรรทัดข้อมูลต่อหน้า 16. <b>pg=1</b> กำหนดเลขหน้าที่ต้องการแสดง 17. <b>order1a=fname</b> เรียงตาม fname แบบน้อยไปมาก 18. <b>order1d=fname</b> เรียงตาม fname แบบมากไปน้อย 19. <b>order2a=fname</b> เรียงตาม fname แบบน้อยไปมาก 20. <b>order2d=fname</b> เรียงตาม fname แบบมากไปน้อย 21. <b>seq=yes</b> กำหนดให้แสดงลำดับ เป็นหลักแรก 22. <b>where=qaddr,ร.ร.บุญวาทย์</b> เลือกข้อมูลตามเขตข้อมูล </td></table> <br><b>6. ตัวแปรที่สามารถแก้ไขใน script</b> <table width=95% bgcolor=#ddffdd><td valign=top> <ol> <li><b>show_help = "yes"</b> ใช้บริการแสดงข้อความอธิบายการใช้โปรแกรมนี้ <li><b>version = " :: 1.0"</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 = "10pt"</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. <b>ขั้นตอนการกำหนดชื่อพื้นที่ หรือชื่อตารางใน MS Excel</b> <dd>การอ่านแฟ้ม .xls ต้องกำหนดพื้นที่ เหมือนกำหนดตารางขึ้นมาสำหรับถูกอ้างอิง ซึ่งนำไปใช้ใน ODBC ได้เช่นกัน <dd>มีวิธีการดังนี้ 1)แทรก 2)ชื่อ 3)กำหนด 4)เลือกพื้นที่พร้อมตั้งชื่อตาราง </font></ul><center><% end function %> |