对筛选结果的分页

时间:2018-04-13 16:56:15

标签: php mysql forms search pagination

我正在尝试采用Sebastian Sulinski设计的高级搜索表单,可在https://github.com/ssd-tutorials/advanced-search-form/tree/master/new_improved_version_completed上找到

我试图整合一个示例分页类,但它不起作用。

有关对此结果应用分页的任何想法吗?

1 个答案:

答案 0 :(得分:0)

HardcoreHenry,我将getrecords函数更改为:

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