分页的第二页显示所有结果

时间:2015-08-23 09:08:05

标签: php mysqli

我花了好几天没有取得任何进展,任何帮助都会非常感激。我有一个页面处理表单中的查询并显示结果。结果的第一页按预期工作,显示找到的行数并显示一些结果,但是当我点击下一页时,我只是在数据库中得到所有结果,就像忘记了查询一样。

<?php include_once "db_conx.php";

if (isset($_GET['submit']) && $_GET['submit'] == 'submit_1'){

//Build Search Query from form data

$whereClauses = array(); 
  if (! empty($_GET['location'])) $whereClauses[] ="(location ='".mysqli_real_escape_string($db_conx,$_GET['location'])."' OR location_2 ='".mysqli_real_escape_string($db_conx,$_GET['location'])."' OR location_3 ='".mysqli_real_escape_string($db_conx,$_GET['location'])."' OR location_4 ='".mysqli_real_escape_string($db_conx,$_GET['location'])."')"; 
  if (! empty($_GET['jobtitle'])) $whereClauses[] ="(jobtitle LIKE '%".mysqli_real_escape_string($db_conx,$_GET['jobtitle'])."')";
if (!empty($_GET['start']))
    if ($_GET['start'] == "today") {
        $whereClauses[] = "START = CURDATE()";
    }
else if ($_GET['start'] == "tomorrow") {
        $whereClauses[] = "START = DATE_ADD( CURDATE( ) , INTERVAL( 9 - IF( DAYOFWEEK( CURDATE( ) ) =1, 8, DAYOFWEEK( CURDATE( ) ) ) ) DAY )";   
    }
else if ($_GET['start'] == "next_week") {
        $whereClauses[] = "START = DATE_ADD( CURDATE( ) , INTERVAL( 9 - IF( DAYOFWEEK( CURDATE( ) ) =1, 8, DAYOFWEEK( CURDATE( ) ) ) ) 
DAY )";   
    }
else if ($_GET['start'] == "this_month") {
        $whereClauses[] = "START BETWEEN DATE_SUB( CURDATE( ) , INTERVAL( DAY( CURDATE( ) ) -1 ) DAY ) AND LAST_DAY( NOW( ) )";   
    }
    else if ($_GET['start'] == "next_month") {
        $whereClauses[] = "start BETWEEN DATE_SUB(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)),INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)))-1 DAY) AND LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH))";
    }
else if ($_GET['start'] == "in_future") {
        $whereClauses[] = "START > CURDATE()";   
    }

  $where = ''; 

if (count($whereClauses) > 0) {
$where = ' WHERE '.implode(' AND ',$whereClauses); 
}

// The first query to count the number of rows 
$sql = "SELECT COUNT(id) FROM users " .$where."";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
// Here we have the total row count
$rows = $row[0];

// The number of results to display per page
$page_rows = 5;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
    $last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// Make sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

// The Query again to get just one page worth of rows by applying $limit

$sql="SELECT * FROM users " .$where." ORDER BY notescheck DESC $limit";

$query = mysqli_query($db_conx, $sql);
// This shows the user what page they are on, and the total number of pages
$textline1 = "Results (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';

// If there is more than 1 page worth of results
if($last != 1){
    /* First we check if we are on page one. If we are then we don't need a link to 
       the previous page or the first page so we do nothing. If we aren't then we
       generate links to the first page, and to the previous page. */
    if ($pagenum > 1) {

        $previous = $pagenum - 1;
        $paginationCtrls .= '<ul class="pagination"><li><a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'&where='.$where.'&submit=submit_1">Previous</a></li></ul> &nbsp; &nbsp; ';// Render clickable number links that should appear on the left of the target page number
        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<ul class="pagination"><li><a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'&where='.$where.'&submit=submit_1">'.$i.'</a></li>
</ul> &nbsp; ';
            }
        }
    }
    // Render the target page number, but without it being a link

    // Render clickable number links that should appear on the right of the target page number
    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<ul class="pagination"><li><a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'&where='.$where.'&submit=submit_1">'.$i.'</a></li>
</ul> &nbsp; ';
        if($i >= $pagenum+4){
            break;
        }
    }
    // This does the same as above, only checking if we are on the last page, and then generating the "Next"
    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= '<ul class="pagination"><li><a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'&where='.$where.'&submit=submit_1">Next</a></li>
</ul> &nbsp; &nbsp; ';
    }
}
}
$list = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
    $id = $row["id"];
    $firstname = $row["firstname"];
    $lastname = $row["lastname"];
$list .= '<p><a href="profile.php?id='.$id.'">'.$firstname.' '.$lastname.' Profile</a> - Click the link to view user<br></p>';
}
// Close your database connection
mysqli_close($db_conx);
?>

1 个答案:

答案 0 :(得分:1)

$_SERVER['PHP_SELF']指的是脚本,但不是完整的网址。这意味着您将丢失先前在查询字符串中的所有信息。实际上,唯一的原因是因为你的脚本名称与url相同。 PHP_SELF实际上是供内部使用(包括文件),而不是用于构建URL。

您可以通过将值存储在Cookie中,或者在构建上一页和下一页的链接时使用完整的URL(包括查询字符串)来解决此问题。

构建网址时,您可以使用$_SERVER['REQUEST_URI']。这为您提供了脚本的相对URL,类似于$_SERVER['PHP_SELF'],但实际上是将文档作为请求URL的一部分。

此外,您可以使用$_SERVER['QUERY_STRING']获取请求的整个查询字符串,或者您可以使用$_GET[]获取每个查询字符串值,并仅复制您要使用的查询字符串值网址。

要简单地复制网址中的所有内容,只需更改网页编号,即可执行以下操作:

$pagenum = 5; // example.

$urlParams = $_GET; // Copy the entiry query string.

$urlParams['pn'] = $pagenum - 1; // Replace the page number.
$previous = http_build_query($urlParams); // Build a new query string.

$urlParams['pn'] = $pagenum + 1;
$next = http_build_query($urlParams);

然后你可以建立一个这样的链接:

'<a href="'.$_SERVER['REQUEST_URI'].'?'.$previous.'">Previous page</a>
 <a href="'.$_SERVER['REQUEST_URI'].'?'.$next.'">Next page</a>'