如何分页结果?

时间:2014-12-17 16:29:44

标签: php mysql pagination html-table

我有一个自定义搜索,我将结果发送到主页的某个位置,但我希望每页限制并获得多个页面结果。

<?php
include 'baglan.php';

$request_owner_sc = $_REQUEST['request_owner'];
if($request_owner_sc!='Select Request Owner'){
$request_owner_kosul=" AND request_owner like '%$request_owner_sc%' ";
}
else {
$request_owner_kosul=' ';    
}

$developer_sc = $_REQUEST['developer'];
if($developer_sc!='Select Developer'){
$developer_kosul=" AND developer like '%$developer_sc%' ";
}
else {
$developer_kosul=' ';    
}

$phase_sc = $_REQUEST['phase'];
if($phase_sc!='Select Phase'){
$phase_kosul=" AND phase like '%$phase_sc%' ";
}
else {
$phase_kosul=' ';    
}

$module_sc = $_REQUEST['module'];
if($module_sc!='Select Module'){
$module_kosul=" AND module like '%$module_sc%' ";
}
else {
$module_kosul=' ';    
}

$request_number_sc = $_REQUEST['request_number'];
if($request_number_sc!='Select Request Number'){
$request_number_kosul=" AND dom_id like '%$request_number_sc%' ";
}
else {
$request_number_kosul=' ';    
}

$development_type_sc = $_REQUEST['development_type'];
if($development_type_sc!='Development Type'){
$development_type_kosul=" AND development_type like '%$development_type_sc%' ";
}
else {
$development_type_kosul=' ';    
}

$status_sc = $_REQUEST['status'];
if($status_sc!='Select Status'){
$status_kosul="status like '%$status_sc%' ";
}
else {
$status_kosul='status IN ("Waiting","In Progress","Completed","Rejected") ';    
}
$project_sc = $_REQUEST['project'];
if($project_sc!='Select Project'){
$project_kosul=" AND project like '%$project_sc%' ";
} 
else {
$project_kosul=' ';    
}
$country_sc = $_REQUEST['country'];
if($country_sc!='Select Country'){
$country_kosul=" AND country like '%$country_sc%' ";
}
else {
$country_kosul=' ';    
}

$search_sc = $_REQUEST['ara'];
if($search_sc!='Search'){
$search_kosul=" AND short_description like '%$search_sc%' ";
}
else {
$search_kosul=' ';    
}




$sorgu="select * from new_dom WHERE $status_kosul $search_kosul $request_owner_kosul     $developer_kosul $phase_kosul $module_kosul $request_number_kosul $development_type_kosul $project_kosul $country_kosul ORDER BY dom_id DESC";



$result=  mysqli_query($conn, $sorgu) or die($sorgu."<br/><br/>".mysql_error());

$num = mysqli_num_rows( $result );





if($num >= 1 ){


    echo "<b><font color='red'>$num record(s) found!</font></b>";
    echo "<br></br>";
    echo "<br></br>";
    echo "<div class='result_table' style='width:800px;height:30px'>";
    echo "<table   width = '850' align = 'center' >";
    echo ("<tr><td>Request Number</td><td>Short Description</td><td>Country</td><td> Phase</td><td>Status</td><td>Module</td><td>Development Type</td><td>Priority</td><td>Developer</td><td>Request Owner</td></tr>");

    echo "</b></tr>"; 
    echo "</div>";



while($row = mysqli_fetch_array( $result )){


    echo "<tr>";
    echo "<td>" . $row['dom_id'] . "</td>";
    echo "<td>" . $row['short_description'] . "</td>";
    echo "<td>" . $row['country'] . "</td>";
    echo "<td>" . $row['phase'] . "</td>";
    echo "<td>" . $row['status'] . "</td>";
    echo "<td>" . $row['module'] . "</td>";
    echo "<td>" . $row['development_type'] . "</td>";
    echo "<td>" . $row['priority'] . "</td>";
    echo "<td>" . $row['developer'] . "</td>";
    echo "<td>" . $row['request_owner'] . "</td>";
    echo "</tr>";
       // echo   $row['dom_id'] . $row['status'] . $row['country'] . $row['developer'] ;
    }
}else{

    echo "<b>No Record found!</b>";
}        


?>

1 个答案:

答案 0 :(得分:1)

计算您有多少结果。设置变量或常量,要在页面上显示的数量。 ceil($has / $pages)获取,您有多少页。检查当前页码是什么,将其存储到变量$pageToGet中。如果没有这样的变量,请将其设置为1。

然后根据所选页面nymber在查询中使用LIMIT x OFFSET yx将为($pageToGet - 1) * $pageToShow