|
导读在上一篇中,我们分析了一个简单的通用分页代码控制。但是通常我们在分页的 ASP 页面中要使用到查询功能,那么怎样将查询也“封装”起来呢?同样需要用到 include 方式。我们可以把查询看作两部分,... 在上一篇中,我们分析了一个简单的通用分页代码控制。但是通常我们在分页的 ASP 页面中要使用到查询功能,那么怎样将查询也“封装”起来呢? 同样需要用到 include 方式。 我们可以把查询看作两部分, 1、输入,即接收用户输入的查询字符串,我们把它放在 query.inc 中 2、输出,即根据用户输入的内容构造查询条件,我们把它放在 query_result.inc 中 这样一来,我们的思路就很清晰了, 1、将 query_result.inc 放在 SQL 语句之前,构造查询条件,然后将查询条件嵌入 SQL 语句中 2、将 query.inc 放在 ASP 页面适当的地方(通常和 navigator.inc 在同一行)。 在接下来的文章中,要稍微复杂一点,因此我们先来看看一个示范的数据库:mytest 表1tbluserinfo' 用户信息表 字段flduserinfo_id' 用户 ID flduserinfo_username flduserinfo_nickname flduserinfo_password 表2tbladdressbook' 通讯录表 字段fldaddressbook_id fldaddressbook_classid' 类别 ID fldaddressbook_userid' 用户 ID fldaddressbook_nickname 表3tbladdressbook_class' 通讯录类别表 字段fldaddressbook_class_id' 类别 ID fldaddressbook_class_title' 例如:朋友、客户、亲人、网友 fldaddressbook_class_explain' 类别说明 视图vwaddressbook SELECT tbladdressbook_class.fldaddressbook_class_title, tbladdressbook_class.fldaddressbook_class_explain, tbladdressbook.fldaddressbook_id, tbladdressbook.fldaddressbook_userid, tbladdressbook.fldaddressbook_classid, tbladdressbook.fldaddressbook_nickname, FROM tbladdressbook INNER JOIN tbladdressbook_class ON tbladdressbook.fldaddressbook_classid = tbladdressbook_class.fldaddressbook_class_id [注意:我在每个字段中加上了表的名字,这是为了生成视图方便,以及其他隐含的麻烦。经过我的长期尝试,发现这种方法的确很好。因此向大家推荐这种命名方式。] 当我们在 tbladdressbook 中查询时,要用 flduserid 来区分每个用户的通讯录内容,因此要加上 " flduserid=" & Session("userid") 的限制。现在我们把这个限制加到我们的 SQL 语句中。 请看 sample2.asp <一> 需要分页的 ASP 文件 sample2.asp <% ' 取得当前 ASP 页面的完整路径,重要技巧 theScript= Request.ServerVariables("SCRIPT_NAME") myconnstr= "driver={SQL Server};server=yourserver;uid=sa;pwd=;database=mytest" thePageSize= 20' 每页显示的记录数 '////////////////////////////////////////////////////////// ' ' 定义表名 ' '////////////////////////////////////////////////////////// theTableName= "addressbook" '////////////////////////////////////////////////////////// ' ' 查询条件 ' '////////////////////////////////////////////////////////// theQueryField= "fld" & theTableName & "_nickname"' 查询字段,完整名字 theQueryTitle= "昵称"' 字段显示标题 theQueryTable= "vw" & theTableName' 字段所在的表,完整名字 ' 如果是查询模式,则构造模糊查询语句 if request("mode") = "query" then %><!--#include file="../inc/control/query_result.inc"--><% else ' 否则忽略 theQueryCon = "1>0" end if '////////////////////////////////////////////////////////// ' ' 限制条件 ' '////////////////////////////////////////////////////////// theLimitCon= "fld" & theTableName & "_userid=" & Session("userid") '////////////////////////////////////////////////////////// ' ' 构造 SQL 语句 ' '////////////////////////////////////////////////////////// uSQL = "select * from " & theQueryTable & " where ( " & theQueryCon & " ) and ( " & theLimitCon & " ) " %> <!--#include file="../inc/control/navigator_init.inc"--> <html> <head> <title>test</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <link rel="stylesheet" href="../default.css" type="text/css"> </head> <!-- 你的 HTML 代码//--> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <!--查询控制--> <!--#include file="../inc/control/query.inc"--> </td> <td> <!--导航控制--> <!--#include file="../inc/control/navigator.inc"--> </td> </tr> <!-- 你的 HTML 代码//--> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <!--显示本页记录--> <% For i = 1 To rs.pagesize %> <!-- 你的记录显示代码//--> <% rs.MoveNext If rs.EOF Then Exit For rs.close conn.close End If next %> </td> </tr> <!-- 你的 HTML 代码//--> </body> </html> <二> 查询控制代码: query.inc 注意 form 表单一定要有自己的名字,例如 form_query ,以和其他表单区分 <table width="100%" border="0" cellspacing="0" cellpadding="0" height="25"> <form method="post" action="<%=theScript%>?mode=query" name=form_query onsubmit="javascript:return checkquery(this);"> <tr> <td> <%=theQueryTitle%> <input type="text" name="<%=theQueryField%>" value="<%=request(theQueryField)%>" class="form_text" <%=uTextSize%>> <input type="submit" name="form_query_submit" value="查询" class="form_button"> </td> </tr> </form> <script language="javascript"> function checkquery(form) { if(form.<%=theQueryField%>.value=='') { alert('请输入<%=theQueryTitle%>信息。\n提示:输入 * 可以查询所有内容。'); form.<%=theQueryField%>.focus(); return false; } return true; } </script> <三> 构造模糊查询语句 query_result.inc <% '////////////////////////////////////////////////////////// ' ' 构造模糊查询语句 ' '////////////////////////////////////////////////////////// ' 模糊查询只支持字符串 theQueryValue = trim(request(theQueryField)) if theQueryValue = "*" or theQueryValue = "" then theQueryCon = "1>0" else theQueryCon = theQueryField & " like '%" & theQueryValue & "%' " end if %> |
温馨提示:喜欢本站的话,请收藏一下本站!