一、提出问题:
在大型的asp项目中,都涉及到了条件查询这一功能。用户要求能够针对特定的字段进行查询。对于有些数据,是从多个表取出来的,因此也要能对多个表进行查询。
一般的话,大多数的做法就是每个程序都做一个查询功能,因为每个查询的表都不同,涉及的字段也不同。但是,如果在大型的asp项目中这么做的话,不仅增加了工作量、影响开发进度,而且不能使系统模块化、代码复用,增加了系统维护难度。因此,我们有必要使条件查询模块化。
二、解决思路:
我们设计条件查询的功能为:
1、选择查询字段(字段);
2、选择查询条件(<、>、=、<=、>=、<>、null、not null、like );
3、输入条件值;
4、选择运算关系(or、and);
5、将查询条件添加到列表框、或从列表框删除。
详细界面请看下图:
我们以新闻表(dbnews)为例,假定新闻表包含有标题、时间、内容、发布人。
在查询数据库时,我们一般都是用如:
“select * from news where time=2002-05-22 and user=tom”,我们如果能在条件查询中返回“time=2002-05-22 and user=tom”的条件字符串,问题就解决了。但如果是不同的程序、不同的表怎么做呢?这就是看用什么方法来定义查询的字段了。
在这里,我们把查询字段作为一个过程写在程序中,在查询模块中调用,这样,查询的字段就是动态的生成了。
用户选择好了一个查询字段,以及匹配条件,将它添加到<select></select>框中,用户可以继续选择另外的查询条件,然后又把它添加到<select>中,完成了,就点查询按钮。用查询模块生成如:“time=2002-05-22 and user=tom”的语句,这样在程序中就可以调用了。
三、解决方案:
asp通用条件查询模块的源代码请参看 (四、源代码)。
1、定义过程
在调用条件查询模块前,首先要在程序中定义一个searchfield()过程:
sub searchfield()
response.write "<option value=1title>标题</option>"
response.write "<option value=1time>时间</option>"
response.write "<option value=1content>内容</option>"
response.write "<option value=1user>发布人</option>"
end sub
其中,value中为字段名称,如果字段为字符型,就在字段名前加1,数字型就加2。如:"1title"。
注意:如果数据是从多个表中取出来的,如:select * from news,user ;那么value的值就要加上相应的表,如:1news.title。这样就可以从多个表查询数据。
2、调用条件查询模块:
<!–#include file="../public/condition_search.asp"–>
3、加入按钮事件:
加入“条件查询”按钮,并添加 onclick="search_dis(s1)"事件:
<input type="button" name=consearch onclick="search_dis(s1)">
默认状态下,条件查询模块隐藏在页面中,当点“条件查询”按钮,就显示出来,这样页面即美观,也不会妨碍用户浏览。
这样,我们就完成了“条件查询”模块的调用
当选择好了查询条件,执行查询后,程序getsql文本框将返回查询条件,如:“title=chian and user=tom”,用request.form("getsql")取得!在把它加入到sql语句中,一个新的查询就生成了。
四、asp通用条件查询模块的源代码
<script language=javascript>
<!–
//隐藏或显示条件查询模块
function search_dis(ob){
if (ob.style.display=="none")
{ob.style.display="";window.location.href (#down);}
else
{ob.style.display="none";}
}
//把条件查询语句添加到<select>查询列表框;
function addse(){
var val,val_tmp,sql,oadd,texttype;
var field,condition,textval,relation;
field=frm_search.field.options[frm_search.field.selectedindex].text;
textval=""+frm_search.textval.value+"" ;
condition=frm_search.condition.options[frm_search.condition.selectedindex].text;
relation=frm_search.relation.options[frm_search.relation.selectedindex].text;
val_tmp = frm_search.field.value;
val_tmp = val_tmp.substring(0,1);
//判断字段的数据类型,如果为1,就是字符、日期型;为2,就是数值型;
if (val_tmp==1)
{texttype=""+frm_search.textval.value+" " ;}
else
{texttype=""+frm_search.textval.value+" " ;}
val_field = frm_search.field.value;
val_field = val_field.substr(1);
val= val_field + frm_search.condition.value + texttype + frm_search.relation.value ;
sql = field+condition+textval+relation;
oadd=document.createelement("option")
oadd.value=val;
oadd.text=sql;
frm_search.sesql.add(oadd);
}
//把条件查询语句从<select>查询列表框中删除;
function movese(){
for (i=1;i<frm_search.sesql.options.length;i++){
if(frm_search.sesql.options[i].selected){
frm_search.sesql.remove(frm_search.sesql.selectedindex);
}
}
}
//取得查询条件,并提交;
function getval(){
var gettxt,setable;
gettxt = "";
var path = window.location.pathname ; //取得页面链接和文件名
var line = path.lastindexof("/"); //取得最后一个/的位置
var dot = path.indexof("."); //取得第一个.的位置
var name = path.substring(line+1,dot); //取得文件名
var filename = name + ".asp";
for (i=1;i<frm_search.sesql.options.length;i++){
gettxt = gettxt+ " " + frm_search.sesql.options[i].value ;}
var valleng=gettxt.lastindexof(" ");
gettxt=gettxt.substr(0,valleng); //去掉条件子句的最后一个关系运算符
setable = frm_search.setable.value ;
//window.open (filename+?gettxt=+gettxt,_self); //向当前页面传送查询条件子句
frm_search.getsql.value = gettxt;
frm_search.action = filename;
frm_search.submit ();
}
//–>
</script>
<a name="down"></a>
<form name="frm_search" method="post" action="">
<input type="hidden" name=getsql value="">
<table bgcolor=#6699cc width=100%>
<tr><td>
<table width="617" border="0" cellspacing="1" cellpadding="2" bgcolor="#cccccc"
align="center">
<tbody id=s1 style=display:none>
<tr>
<td width="134" height="25" bgcolor="#006699">
<div align="right"><font color="#ffffff" style="font-size:10pt">查询字段:
</font></div>
</td>
<td width="159" bgcolor="#f2f2f2">
<select name="field" style="font-size:12px"><%call searchfield()%></select>
</td>
<td width="102" bgcolor="#e1e1e1">
<div align="center">
<input type="button" name="addsql" value="添加>>>" onclick="addse()"
style="background: #ffdead;border-bottom: #665b8e 1px solid;border-left: #ffffff 1px
solid;border-right: #665b8e 1px solid;border-top: #ffffff 1px solid;color: #333333;cursor:
hand;font-size: 12px;height: 20px;padding-bottom: 1px;padding-left: 1px;padding-right:
1px;padding-top: 1px">
</div>
</td>
<td rowspan="4" bgcolor="#f2f2f2" width="221">
<select id="sesql" size="6">
<option value="">————查询条件———–</option>
</select>
</td>
</tr>
<tr>
<td width="134" height="25" bgcolor="#006699">
<div align="right"><font color="#ffffff" style="font-size:10pt">查询条件:
</font></div>
</td>
<td width="159" bgcolor="#f2f2f2">
<select name="condition" style="font-size:12px">
<option value="=">等 于</option>
<option value=">">大 于</option>
<option value=">=">大于等于</option>
<option value="<">小 于</option>
<option value="<=">小于等于</option>
<option value="<>">不等于</option>
<option value=" null ">为空</option>
<option value=" not null ">不为空</option>
<option value=" like ">匹配</option>
</select>
</td>
<td width="102" bgcolor="#e1e1e1">
<div align="center">
<input type="button" name="delsql" value="<<<删除" onclick="movese()"
style="background: #ffdead;border-bottom: #665b8e 1px solid;border-left: #ffffff 1px
solid;border-right: #665b8e 1px solid;border-top: #ffffff 1px solid;color: #333333;cursor:
hand;font-size: 12px;height: 20px;padding-bottom: 1px;padding-left: 1px;padding-right:
1px;padding-top: 1px">
</div>
</td>
</tr>
<tr>
<td width="134" height="25" bgcolor="#006699">
<div align="right"><font color="#ffffff" style="font-size:10pt">条 件 值:
</font></div>
</td>
<td width="159" bgcolor="#f2f2f2">
<input type="text" id="textval" size="15" style="background-color: white; border-
bottom: #000000 1px solid;border-left: #000000 1px solid;border-right: #000000 1px solid;border-
top: #000000 1px solid;font-size: 9pt">
</td>
<td width="102" bgcolor="#e1e1e1"> </td>
</tr>
<tr>
<td width="134" height="25" bgcolor="#006699">
<div align="right"><font color="#ffffff" style="font-size:10pt">关系运算符:
</font></div>
</td>
<td width="159" bgcolor="#f2f2f2">
<select name="relation" style="font-size:12px">
<option value="and">并且</option>
<option value="or">或者</option>
</select>
</td>
<td width="102" bgcolor="#e1e1e1">
<div align="center">
<input type="button" name="search_ok" value="查 询" onclick="getval()"
style="background: #deb887;border-bottom: #665b8e 1px solid;border-left: #ffffff 1px
solid;border-right: #665b8e 1px solid;border-top: #ffffff 1px solid;color: #333333;cursor:
hand;font-size: 12px;height: 20px;padding-bottom: 1px;padding-left: 1px;padding-right:
1px;padding-top: 1px">
</div>
</td>
</tr>
</tbody>
</table>
</td></tr>
</table>
</form>
