พบกับบทความเกี่ยวกับคอมพิวเตอร์และ programming ได้ที่ http://articles.modoeye.com
ASP programming / ค้นหาและแบ่งหน้าแสดงผล
korakot_titla
korakot_titla
Wed 18 Oct 2006 22:28:58

ผมรบกวนพี่ๆทีมงานหน่อยนะครับ คือปัญหามีอยู่ว่าผมมี listbox อยู่ 1 อันเอาไว้สำหรับให้เลือกเงื่อนไขในการค้นหา แล้วมี textbox อีก 1 อันให้ป้อนข้อมูลเพื่อทำการค้นหาหลังจากได้ทำการเลือกเงื่อนไขไปแล้ว แล้วก็ทำการค้นหาซึ่งเมื่อผมค้นหาข้อมูลเจอแล้วแต่จำนวนข้อมูลที่แสดงออกมายาวเกินไป(หลายบรรทัด)ผมอยากที่จะทำการแบ่งออกเป็นหน้าๆโดยผมมีตัวอย่างโค้ดด้านล่างที่ผมมานะครับ คือค้นหาได้แต่แบ่งไม่ได้ครับ

<%
    MyType=Request.Form("MyType")
  txt_Search=Request.Form("txt_Search")
  select_manu=Request.form("select")  
  if txt_Search<>"" then
    Set RS=Server.CreateObject("Adodb.Recordset")
    Select Case  MyType
    Case "Stu_Name"
     SQL="Select S.*,T.* ,M.* From TblStudent S INNER JOIN TblTitle T ON S.TitleID=T.TitleID INNER JOIN  TblMajor  M  ON S.Major_ID=M.Major_ID Where FirstName Like '"&txt_Search&"%'"
    Case "Stu_ID"
     SQL="Select S.*,T.* ,M.* From TblStudent S INNER JOIN TblTitle T ON S.TitleID=T.TitleID INNER JOIN  TblMajor  M  ON S.Major_ID=M.Major_ID Where StudentID Like '"&txt_Search&"%'"
    Case "Stu_Major"
     SQL="Select S.*,T.* ,M.* From TblStudent S INNER JOIN TblTitle T ON S.TitleID=T.TitleID INNER JOIN  TblMajor  M  ON S.Major_ID=M.Major_ID Where Major_Thai_Name Like '"&txt_Search&"%'"
    End Select    
    RS.Open SQL,Conn,1,3
     If Request.QueryString("pageno") <> "" Then
        pageno = Request.QueryString("pageno")
        End If

     On Error Resume Next
     rs.pagesize=10'จำนวนข้อความที่ต้องการให้แสดงในแต่ละหน้า
     totalpage = rs.PageCount
     If pageno < 1 or Cint(pageno) > Cint(totalpage) Then pageno = 1 End if
     rs.AbsolutePage = pageno
  
  %>

<TABLE width="91%" height="74"
            border=0 cellPadding=1 cellSpacing=1 bgColor=#CCD4DD>
    <TBODY>
      <TR>
        <TD height="22" vAlign=top bgColor=#eeeeee><font color="#993300" size="-1" face="Tahoma"><strong><img src="pic/arrow-unread.gif" width="10" height="10">รายละเอียดผลการค้นหา</strong></font></TD>
      </TR>
      <TR>
        <TD height="49" vAlign=top bgColor=#ffffff><div align="center">
            <table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
              <tr bgcolor="#E8E8E8">
                <td width="16%"><div align="center"><strong><font size="-1" face="Tahoma">รหัสนักศึกษา</font></strong></div></td>
                <td width="33%"><div align="center"><strong><font size="-1" face="Tahoma">ชื่อ-สกุล</font></strong></div></td>
                <td width="44%"><div align="center"><strong><font size="-1" face="Tahoma">สาขาวิชา</font></strong></div></td>
                <td width="7%"><div align="center"><strong><font size="-1" face="Tahoma">แก้ไข</font></strong></div></td>
              </tr>
     <%
     bg=1   
     For a=1 to rs.PageSize
      if bg = 1 then
     bgc = "#FFFFFF"
    else
     bgc ="#f3f3f3"
    end if
    If rs.EOF Then Exit For   
     %>
              <tr bgcolor="<%=bgc%>">
                <td><div align="center"><%=RS("StudentID")%></div></td>
                <td><%=RS("Title")&RS("FirstName")&" "&RS("LastName")%></td>
                <td><%=RS("Major_Thai_Name")%></td>
                <td><div align="center"><img src="pic/icon_edit.gif" alt="แก้ไข" width="16" height="16"></div></td>
              </tr>
     <%
     bg=bg*(-1)     
     RS.Movenext
     Next
     RS.Close
     Set RS=Nothing
    end if
     %>
            </table>
          </div></TD>
      </TR>
    </TBODY>
  </TABLE>
  <font size="2">
  <%
If pageno <> 1 and pageno <> 2 Then
  Response.Write "<font size=2 color=#666666>"&"<a href='admin_edit_color_major.asp' >หน้าแรก</a>  "&"</font>"

End If
if pageno <> 1 Then
  Response.Write "<font size=2 color=#666666>"&"<a href='admin_edit_color_major.asp?pageno="&pageno-1&"' > หน้าก่อน</a>  "&"</font>"
End if
For b =1 To totalpage
  If b = Cint(pageno) Then
    Response.Write "<b> ("&b&") </b>"
  Else
    Response.Write "<a href='admin_edit_color_major.asp?pageno="&b&"' >"&b&"</a>"
  End If
  If b <> totalpage Then
     Response.Write " "
  End If
Next
If Cint(pageno) <> totalpage Then
  Response.Write "<font size=2 color=#666666>"&"  <a href='admin_edit_color_major.asp?pageno="&pageno+1&"' >ถัดไป </a>"&"</font>"
End If
If Cint(pageno) <> totalpage Then
  Response.Write"<font size=2 color=#666666>"&" <a href='admin_edit_color_major.asp?pageno="&totalpage&"' >สุดท้าย</a>"&"</font>"
End If

%>
ซึ่งการ

Administrator
Wed 18 Oct 2006 23:29:22
ถ้าต้องการแบ่งหน้าควรจะส่งค่ามาแบบ GET ครับ ไม่เช่นนั้นการส่งค่าการค้นหาไปยังหน้าถัดไปทำได้ลำบากครับ (เป็นเหตุผลที่ทำไม search engine ต่างๆมีการส่งค่าแบบ GET ครับ)

ส่วน query ให้ลองอ่านที่กระทู้นี้ครับ http://forum.modoeye.com/module12-446.html
korakot_titla
korakot_titla
Thu 19 Oct 2006 14:52:46

รบกวนช่วยดูโค้ดนี้ให้ผมหน่อยนะครับ คือปัญหามีอยู่ว่า เวลาผมคลิกเลือกเงื่อนไขสมมุติเป็นรหัสนักศึกษา แล้วป้อนข้อมูล "17" เพื่อค้นหาค้นที่รหัสขึ้นต้นด้วย "17" พอคลิกปุ่มค้นหาแล้วก็สามารถค้นหาได้แล้วทำการแบ่งหน้าให้ได้นะครับ แต่พอผมคลิกดูหน้าที่ 2 ของข้อมูลที่ได้ทำการค้นหามาแล้ว มันจะส่งไม่ส่งค่าหรือเงื่อนไขที่เราเลือกไว้มาทำให้ไม่มีข้อมูลนะครับ
  <%
  myselect=Request.Form("select")
  txt_search=Request.Form("txt_search")
  if txt_search = "" then
     txt_search=Request.QueryString("txt_search")
  end if  
  if myselect="" then   
     myselect="name"
     Response.write "ว่าง"
  end if

    response.write myselect
  if txt_search = "" And myselect <> "total" then
     SQL="Select * From tblQuota"
  elseif myselect = ""  then
     SQL="Select * From tblQuota"
  else
  Select Case myselect
  Case "total"
   SQL="Select * From tblQuota "  
  Case "name"
   SQL="Select * From tblQuota Where firstname Like '"&txt_search&"%'"  
  Case "id"
   SQL="Select * From tblQuota Where idcard Like '"&txt_search&"%'"
  'Case "major"  
  End Select
  end if
  Set RS=Server.CreateObject("Adodb.Recordset")
  RS.Open SQL,Conn,1,3
  RS.PageSize=15        ' default=10
  If Request.QueryString("Page")="" Then
    CurrentPage=1
  Else
    CurrentPage=CInt(Request.QueryString("Page"))   ' ไม่แปลงจะเปรียบเทียบค่าไม่ได้
  End If
  with response
          .Write "<script language='JavaScript'>"
           .Write "function newtopic() { window.open('searchScholarshipDetail.asp','','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=400,height=300'); }"
          .Write "function toPage(select) {"
          .Write "parent.location='test_Search.asp?myselect="& Request.Form("select")& "&txt_search="&txt_search&"&Page=' + select.options[select.selectedIndex].value }"
         .Write "</script>"       
  end with
%>
โค้ดตรงที่ผมเน้นสีนี้ถ้าเป็นถ้าเลือกแล้วถ้าเลือกหน้า 2,3,4 ไปเรื่อยๆ มันจะเป็นค่าว่างนะครับแล้วมันจะไปอ่านตรงโค้ดที่ผมเน้นสีไว้ทำให้ตัวแปร myselect (คือตัวแปรที่ใช้ในการเก็บเงื่อนไข) จึงทำให้เป็นการเลือกเงื่อนไขคือ ชื่อนักศึกษา ตลอด ช่วยรบกวนไล่ดูโค้ดให้ผมหน่อยนะครับถ้าผมเข้าใจไม่ผิดตามโค้ดมันจะไม่ส่งค่า myslect มาให้กับหน้าเดิมเวลาเราเลือกหน้าถัดไป จากข้อมูลที่เราค้นหาได้

รบกวนหน่อยนะครับ ขอบคุณมากครับ

Administrator
Thu 19 Oct 2006 16:32:03
myselect=Request.Form("select")
  txt_search=Request.Form("txt_search")
  if txt_search = "" then
     txt_search=Request.QueryString("txt_search")
  end if 

ผมได้แจ้งแล้วว่าจะเกิดปัญหานี้ถ้าใช้การรับค่าจาก form ในแบบ POST ให้เปลี่ยนเป็นแบบ GET แทนครับ

myselect=Request.QueryString("select")
txt_search=Request.QueryString("txt_search")
if txt_search = "" then
   txt_search=Request.QueryString("txt_search")
end if
ประมาณนี้ครับ
korakot_titla
korakot_titla
Fri 20 Oct 2006 10:01:23
ผมลองเปลี่ยนแล้วนะครับ ก็ยังเหมือนเดิมนะครับ
Administrator
Fri 20 Oct 2006 10:14:52
If pageno <> 1 and pageno <> 2 Then
    Response.Write "<font size=2 color=#666666>"&"<a href='admin_edit_color_major.asp' >หน้าแรก</a>  "&"</font>" End If
if pageno <> 1 Then
    Response.Write "<font size=2 color=#666666>"&"<a href='admin_edit_color_major.asp?pageno="& pageno-1 & "&select=" & Request.QueryString("select") & "&txt_search=" & Request.QueryString("txt_search") & "' > หน้าก่อน</a>  "&"</font>"
End if

For b =1 To totalpage
    If b = Cint(pageno) Then
        Response.Write "<b> ("&b&") </b>"
    Else
        Response.Write "<a href='admin_edit_color_major.asp?pageno=" & b & "&select=" & Request.QueryString("select") & "&txt_search=" & Request.QueryString("txt_search") &"' >"& b &"</a>"
    End If

    If b <> totalpage Then
        Response.Write " "
    End If
Next

If Cint(pageno) <> totalpage Then
    Response.Write "<font size=2 color=#666666>"&"  <a href='admin_edit_color_major.asp?pageno="&pageno+1&"&select=" & Request.QueryString("select") & "&txt_search=" & Request.QueryString("txt_search") &"' >ถัดไป </a>"&"</font>"
End If

If Cint(pageno) <> totalpage Then
  Response.Write"<font size=2 color=#666666>"&" <a href='admin_edit_color_major.asp?pageno="&totalpage&"&select=" & Request.QueryString("select") & "&txt_search=" & Request.QueryString("txt_search") &"' >สุดท้าย</a>"&"</font>"
End If
kangbit
kangbit
Wed 4 Jun 2008 00:05:57

รบกวนสอบถามหน่อยนะคับคือผมมีตัวอย่างโค้ดที่ติดปัญหามาสอบถามนะครับคือเป็นการแสดงผลแบบแบ่งหน้าแต่ติดปัญหาตรงที่ว่าตรงลำดับผมอยากให้มันรันสมมุติเราแบ่ง 10 แถวต่อหน้าและหน้าแรกก็แสดงลำดับตั้งแต่ 1-10 และหน้าที่สองก็แสดง 2-20 แต่ตัวนี้มันไม่รันตามที่ได้บอกนะครับคือมันจะรัน 1-10 ตลอดทุกหน้าเลยครับ รบกวนช่วยดูให้หน่อยนะครับ ขอบคุณมากครับ

---------------------

  <% 
 
   Disp_Page =10
  Set rs=Server.CreateObject("Adodb.Recordset")
     'CD=Request.QueryString("CD")
   
     SQL="Select * From TblProvince"
   rs.Open Sql,Conn,1,1
     rs.pagesize = Disp_Page
     page_count = 1
     if rs.pagecount >= 1 then page_count = rs.pagecount
     current_page = 1
     if not Request.QueryString("page") = "" then
      rs.absolutepage = int(Request.QueryString("page"))
      current_page = int(Request.QueryString("page"))
      count_num = (Disp_Page * current_page) - (Disp_Page-1)
     else
      count_num = 1
     end if
     num = 1

     for rscount = 1 to Disp_Page
      if not rs.eof then
%>
  <tr>
    <td align="center" bgcolor="#D7FFD7"><%= num %></td>
    <td bgcolor="#D7FFD7">&nbsp;<a href="#"><%'=RS("Position_Ad")%></a></td>
  </tr>
  <%
       rs.movenext
       num = num + 1
      end if
     next
     rs_recordcount = rs.recordcount  
   ' end if
   rs.Close     
%>
  <tr>
    <td colspan="5" align="center" class="td_footer">รวมทั้งหมด <%= num-1 & " / " & rs_recordcount %>
      รายการ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ปัจจุบันหน้า&nbsp;
      <%= abs(current_page) %>&nbsp;/&nbsp;<%=page_count%>
      <% if page_count > 1 then %>
      &nbsp; ไปหน้า
      <select name="s_page"  class="menulist" onChange="window.open(this.options[this.selectedIndex].value,'_self');">
        <% for i = 1 to page_count %>
        <option value="?page=<%=i%>"<% if int(Request.QueryString("page")) = i then Response.Write(" selected") %>><%=i%></option>
        <% next %>
      </select>
      <% end if %>
    </td>
  </tr>
</table>

-----------------------

webmaster
webmaster
Wed 4 Jun 2008 10:31:58
เข้าใจว่าน่าจะเป็น 1-10 และ 11-20 มากกว่านะครับ ส่วนการทำ paging ใน MS products นั้นอ้างอิงตามคำสั่งของ MSSQL ได้เลยครับ หาอ่านดูได้ใน http://articles.modoeye.com ครับ
kagnbit
kagnbit
Thu 5 Jun 2008 00:12:56

ผมลองดูแล้วสงสัยคงเกิดจากตรงนี้ครับ

num = 1

ผมลงเปลี่ยนเปน

num =((rs.absolutepage-1) * Disp_Page) + 1

แต่ค่าที่ได้ติดลบนะครับ

webmaster
webmaster
Thu 5 Jun 2008 08:31:33
ลองดูที่นี่เพิ่มนะครับ http://www.asp101.com/samples/db_paging.asp
Reply
Name:
E-mail:
Home | Services | Forum | Classified | Directories | Support | Contact
ATOM feed RSS 0.9 feed RSS 1.0 feed RSS 2.0 feed
Copyright © 2005 - 2007 Modoeye.com, All Rights Reserved.
Disclaimer | Privacy policy | Term of Use | Term of Services
Valid XHTML Valid CSS! PHP: Hypertext Preprocessor MySQL database Apache Powered! FreeBSD Power to serve
Modoeye Sitemap Client login