在此练习中,将把一个单选按钮组添加至允许用户选择 AND 或 OR 搜索条件的页面。当运行此页面时,它看起来应如下所示:
function NameAndStateSearch_Or(func int, lname char(30) in, state char(2) in, Customers Customer[]) get Customers with #sql{ select CUSTOMER_ID, FIRST_NAME, LAST_NAME, PASSWORD, PHONE, EMAIL_ADDRESS, STREET, APARTMENT, CITY, STATE, POSTALCODE, DIRECTIONS from EGL.CUSTOMER where LAST_NAME LIKE :lname OR STATE = :state }; if (sqlcode != 0) func = 0; end end
除了此函数在 where 子句中使用 OR 而不是 AND 之外,它与在前一个练习中添加的 NameAndStateSearch_And 函数完全相同。
AND
AND
OR
“属性”视图看起来应如下所示:
package pagehandlers;
import data.*;
PageHandler customersearch { handleHardIOErrors = no, throwNrfEofExceptions = yes }
{view="customersearch.jsp", onPageLoadFunction="onPageLoad"}
searchTerms Customer; //Customer record (search) values
searchResults Customer[]; //Customers results rows
pagemsgRec msgRec; //pagemsgRec variable of type MsgRec
func int; //func - a flag to pass the state of the results
andOr char(3); //A char field that will be bound to the radio button group
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 (%)
if (andOr == "AND")
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
else
CustomerLib.NameAndStateSearch_Or(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
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
除了下列差别之外,此代码与在前一个练习中添加的代码相似:
该页面看起来应如下所示:
当测试该页面时,尝试使用 AND 和 OR 函数。必须选择其中一个单选按钮才能使搜索页面正常工作。
要使用此搜索页面仍然不易,因为样本数据库中的记录并不多,可猜测的州也不多。在下一个练习中,将把 State 输入字段更改为列示在数据库中使用的所有州的组合框。
现在,您可以开始进行练习 2.4:动态填充组合框了。