既然您已经创建了 Web 页面,就必须添加执行以下操作的 EGL 代码:从 Web 页面检索搜索项、根据这些项搜索数据库并在页面上显示搜索结果。
function NameAndStateSearch_And(func int,
lname char(30) in, state char(2) in,
Customers Customer[])
get Customers;
if (sqlcode != 0) func = 0; end
end
此函数类似于库中获取数据库中的每个记录的函数。唯一的差别是此函数接收下列四个参数:
此函数将立即检索数据库中的每条记录。在接下来的几个步骤中,将编辑此函数生成的 SQL 语句以便只返回符合搜索项 lname 和 state 的记录。
必须将光标放在 get Customers 这一行上的词 Customers 上,否则将不能编辑 SQL 语句。
where LAST_NAME like :lname and STATE = :state
该代码看起来应类似如下所示:
EGL 提供了创建和生成 SQL 语句的不同方法。在 EGL 教程的前面部分,您通过使用 usingKeys 子句指定特定的客户标识号来检索特定的数据库记录。此子句创建一个 SQL where 语句,该语句类似于刚才添加至 CustomerLib.egl 的 where 语句。还可以使用 defaultSelectCondition 来执行相同的任务。
package pagehandlers; import data.*; PageHandler customersearch { handleHardIOErrors = no, throwNrfEofExceptions = yes } {view="customersearch.jsp", onPageLoadFunction="onPageLoad"} searchTerms Customer; //Search input searchResults Customer[]; //Search results pagemsgRec msgRec; //pagemsgRec variable of type MsgRec func int; //func - a flag to pass the state of the results Function onPageLoad() if (func == 0) //Either no rows, or 1st time into page pagemsgRec.msg="No customer(s) found or no search criteria entered."; End end function searchFunction() func = 1; //Initialize func before calling database searchTerms.Last_Name = searchTerms.Last_Name+"%"; //Add wildcard (%) //to search field CustomerLib.NameAndStateSearch_And(func, searchTerms.Last_Name, searchTerms.State, searchResults); //Call search function pagemsgRec.msg = "Customer(s) found. Search again?"; //Assign msg text pagemsgRec.nbr = sysLib.size(searchResults); //Get row count end End Record msgRec //Custom EGL record type definition //used to combine numeric and string data 10 nbr int; //Integer - numeric field 10 msg char(222); //Character data end
以下是一些有关刚才添加的代码的技术说明:
此记录包含两个字段:nbr - int,一个表示搜索返回的结果数的数字;msg - char(222),一个表示反馈消息的字符串。必须按顺序将这两个字段绑定至“提交”按钮上方的输出组件,其中,nbr - int 放在左边的输出组件上,msg - char(222)放在右边的输出组件上。
该页面看起来应如下所示:
当测试该页面时,在 LAST_NAME 字段中输入一个字母,在 STATE 字段中输入一个州名。注意此搜索页面是区分大小写的。例如,如果在 LAST_NAME 字段中输入 F 并在 STATE 字段中输入 NV,则页面会显示多个结果,如下图所示:
要使用此搜索结果页面并不容易,因为用户既必须知道客户所在的州,又必须知道客户姓的首字母。如果用户能够在 AND 搜索与 OR 搜索之间进行选择就会更好一些。在下一个练习中,将把此选项添加至页面。在后面的练习中,将把 State 输入字段更改为列示在数据库中使用的所有有效州的组合框。
现在,您可以开始进行练习 2.3:使用 OR 搜索条件了。