我正在尝试采用Sebastian Sulinski设计的高级搜索表单,可在https://github.com/ssd-tutorials/advanced-search-form/tree/master/new_improved_version_completed上找到
我试图整合一个示例分页类,但它不起作用。
有关对此结果应用分页的任何想法吗?
答案 0 :(得分:0)
function getRecords($search = null) {
$items = array();
$params = array();
$connect = mysqli_connect("localhost", "root", "", "acsdb");
$record_per_page = 50;
$page = '';
$output = '';
if(isset($_POST["page"]))
{
$page = $_POST["page"];
}
else
{
$page = 1;
}
$start_from = ($page - 1)*$record_per_page;
$sql = "SELECT DISTINCT(`staff`.`staffname`), `staff`.`indexno` AS `index`,
locations.name, staff.email FROM `staff` JOIN `locations` ON `locations`.`id` = `staff`.`slocationid` ";
if (!empty($search) && is_array($search)) {
foreach($search as $key => $value) {
switch($key) {
case 'keyword':
$items[] = "`staff`.`staffname` LIKE ?";
$params[] = "%{$value}%";
break;
case 'locations':
$items[] = "`locations`.`id` = ?";
$params[] = $value;
break;
case 'locationm':
$items[] = "`staff`.`slocationid` IN (
SELECT `id`
FROM `locations`
WHERE `id` = ?
)";
$params[] = $value;
break;
}
}
if (!empty($items)) {
$sql .= " WHERE ";
$sql .= implode(" AND ", $items);
}
}
$sql .= " ORDER BY `staff`.`staffname` ASC LIMIT $start_from, $record_per_page";
return fetchRecords($sql, $params);
}
并添加了
<script>
$(document).ready(function(){
load_data();
function load_data(page)
{
$.ajax({
url:"functions.php",
method:"POST",
data:{page:page},
success:function(data){
$('#pagination_data').html(data);
}
})
}
$(document).on('click', '.pagination_link', function(){
var page = $(this).attr("id");
load_data(page);
});
});
</script>
到index.php