【发布时间】:2017-09-22 16:11:00
【问题描述】:
我有一个用于跟踪客户信息和设备的数据库。我有一个有效的搜索页面,将所有数据合并为可读格式。它设置为每页仅显示一个结果。
我正在尝试将过滤添加到搜索中。这样,如果您只搜索姓氏,就不会浪费时间搜索设备数据库。它只会搜索客户表中的姓氏字段。我有这个工作问题是当按下下一页时它得到零结果。
我知道为什么会这样。这是因为我使用 isset 和复选框来进行过滤。
$SLastName = $_GET['LastName'];
$SFirstName = $_GET['FirstName'];
$SAddress = $_GET['Address'];
$SAP = $_GET['AP'];
$SEquipment = $_GET['Equipment'];
$SEverything = $_GET['Everything'];
if (isset($SLastName)){
$construct .="(customers.LastName LIKE '%$search_each%')";
if (isset($SFirstName)){
$construct .=" OR (customers.FirstName LIKE '%$search_each%')";
}
if (isset($SAddress)){
$construct .=" OR (customers.Address LIKE '%$search_each%')";
}
if (isset($SAP)){
$construct .=" OR (aps.AP LIKE '%$search_each%')";
}
if (isset($SEquipment)){
$construct .=" OR (equipment.ESN LIKE '%$search_each%')
OR (equipment.WMAC LIKE '%$search_each%')
OR (equipment.SN LIKE '%$search_each%')
OR (equipment.Manufacturer LIKE '%$search_each%')";
}
if (isset($SEverything)){
$construct .=" OR (customers.Account LIKE '%$search_each%')
OR (customers.AlternatePhone LIKE '%$search_each%')
OR (customers.Phone LIKE '%$search_each%')
OR (routableip.routable_IP LIKE '%$search_each%')
OR (unitip.UNITIP_new LIKE '%$search_each%')";
}
}
else if(isset($SFirstName)){
$construct .="(customers.FirstName LIKE '%$search_each%')";
if (isset($SAddress)){
$construct .=" OR (customers.Address LIKE '%$search_each%')";
}
if (isset($SAP)){
$construct .=" OR (aps.AP LIKE '%$search_each%')";
}
if (isset($SEquipment)){
$construct .=" OR (equipment.ESN LIKE '%$search_each%')
OR (equipment.WMAC LIKE '%$search_each%')
OR (equipment.SN LIKE '%$search_each%')
OR (equipment.Manufacturer LIKE '%$search_each%')";
}
}
else if(isset($SAddress)){
$construct .="(customers.Address LIKE '%$search_each%')";
if (isset($SAP)){
$construct .=" OR (aps.AP LIKE '%$search_each%')";
}
if (isset($SEquipment)){
$construct .=" OR (equipment.ESN LIKE '%$search_each%')
OR (equipment.WMAC LIKE '%$search_each%')
OR (equipment.SN LIKE '%$search_each%')
OR (equipment.Manufacturer LIKE '%$search_each%')";
}
}
else if(isset($SAP)){
$construct .="(aps.AP LIKE '%$search_each%')";
if (isset($SEquipment)){
$construct .=" OR (equipment.ESN LIKE '%$search_each%')
OR (equipment.WMAC LIKE '%$search_each%')
OR (equipment.SN LIKE '%$search_each%')
OR (equipment.Manufacturer LIKE '%$search_each%')";
}
}
else if(isset($SEquipment)){
$construct .="(equipment.ESN LIKE '%$search_each%')
OR (equipment.WMAC LIKE '%$search_each%')
OR (equipment.SN LIKE '%$search_each%')
OR (equipment.Manufacturer LIKE '%$search_each%')";
}
搜索输入页面使用复选框。在提交页面上选中这些框以查看设置了哪些字段。设置字段确定 sql 将是什么。
就像我说的那样,这对于找到第一个结果很有效。
这里是我的代码中与分页有关的部分。
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
//Pagination Starts
$prev = $start - $per_page;
$next = $start + $per_page;
$adjacents = 3;
$last = $max_pages - 1;
if($max_pages > 1){
//previous button
if (!($start<=0)) {
$PrevButton .= "<a href='search.php?
search=$search&submit=Search&start=$prev' class='button Prev1'
style='vertical-
align:middle'><span>Prev</span></a> ";
}
else{
$PrevButton .= "<a class='incative' style='vertical-
align:middle'><span>Prev</span></a> ";
}
$current =$next;
//next button
if (!($start >=$foundnum-$per_page)){
$NextButton = "<a href='search.php?
search=$search&submit=Search&start=$next' class='button Next2'
style='vertical-align:middle'><span>Next</span></a>";
}
else {
$NextButton = "<a class='incative' style='vertical-
align:middle'><span>Next</span></a> ";
}
}
例如,如果我搜索姓氏 Smith。它将显示结果 1 of 10。然后,当我按下下一个按钮时,它会出现我内置的错误,因为它没有找到任何带有搜索关键字的结果。
我认为正在发生的事情是,当按下下一个按钮时,它会更改 GET url 以显示第 2 页,但是当发生这种情况时,它会丢失搜索 sql 的值,因为它不再看到 $SLastName isset。
我不知道如何解决这个问题。
【问题讨论】:
标签: php mysql search pagination