分页无法正常工作 - 未应用限制


pagination not working properly - limit not being applied

我正在尝试将分页添加到我的搜索结果中,这样做我在以下行遇到以下错误:

注意:未定义的变量:限制在 C:''xampp''htdocs''t69''functions''functions.php 在第 262 行

下面是第 262 行:

$get_crs = mysql_query("select * from books where book_provider='$provider_id' $limit");

本质上,我的搜索结果应该只显示 10 个项目,如果超过 10 个项目,它应该显示要导航到的其他页面(如第 1 - 5 页)。

我的问题如下 限制似乎不适用于正在显示的项目,显示的项目超过 10 个,我不想强制只显示 10 个,例如 LIMIT 10,因为如果超过 10 个,那么当我希望如果超过 10 个时,只会显示 10 个,分页显示在底。

下面是代码的一部分:

$outputList = '';
    if(isset($_GET['provider'])){
        $provider_id = $_GET['provider'];
    global $con;
$get_crs = mysql_query("select * from books where book_provider='$provider_id' $limit");
$nr = mysql_num_rows($get_crs); // Get total of Num rows from the database query
if (isset($_GET['pg'])) { // Get pn from URL vars if it is present
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pg']); 
    //$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // filter everything but numbers for security(deprecated)
} else { // If the pn URL variable is not present force it to be value of page number 1
    $pn = 1;
} 
//This is where we set how many database items to show on each page 
$itemsPerPage = 10; 
// Get the value of the last page in the pagination result set
$lastPage = ceil($nr / $itemsPerPage);
// Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
if ($pn < 1) { // If it is less than 1
    $pn = 1; // force if to be 1
} else if ($pn > $lastPage) { // if it is greater than $lastpage
    $pn = $lastPage; // force it to be $lastpage's value
} 
// This creates the numbers to click in between the next and back buttons
// This section is explained well in the video that accompanies this script
$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($pn == $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 
// Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax
// $sql2 is what we will use to fuel our while loop statement below
//////////////////////////////// END Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////
$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
if ($lastPage != "1"){
    // This shows the user what page they are on, and the total number of pages
    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';
    // If we are not on page 1 we can place the Back button
    if ($pn != 1) {
        $previous = $pn - 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
    } 
    // Lay in the clickable numbers display here between the Back and Next links
    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
    // If we are not on the very last page we can place the Next button
    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
    } 
}

    while($row_crs = mysql_fetch_array($get_crs)){ 
    $crs_id = $row_crs['course_id'];
        $crs_cat = $row_crs['course_cat'];
    $crs_provider = $row_crs['course_provider'];
    $crs_title = $row_crs['course_title'];
        $crs_price = $row_crs['course_price'];
          $crs_city= $row_crs['course_city'];
          $crs_date= $row_crs['course_date1'];

$crs_sdesc= $row_crs['course_sdesc'];
        $crs_image = $row_crs['course_image'];

    $outputList .=  "
           <article class='search-result row'><center>
      <div class='col-xs-12 col-sm-12 col-md-3' id='thumbnailContainer'>
        <a href='#' title='Lorem ipsum' class='thumbnail' id='resultThumbnail'><img src='content/logo_ontrack.png' /></a>
      <button id='resultprice'><i class='fa fa-usd fa-2x'><span id='resultpriceText'> $crs_price</span></i></button>
      </div>
      <div class='col-xs-12 col-sm-12 col-md-2'>
        <ul class='meta-search' id='listDesign'>
          <li><button id='resultInfo'><i class='fa fa-calendar fa-1x'><span id='iconText'>  $crs_date</span></i></button></li>
           <li><button id='resultInfo'><i class='fa fa fa-tags fa-1x'><span id='iconText'> <b> Web Development</b></span></i></button></li>
<li><button id='resultInfo'><i class='fa fa-graduation-cap fa-1x'><span id='iconText'> <b> HOTT</b></span></i></button></li>
<li><button id='resultInfo'><i class='fa fa-map-marker fa-1x'><span id='iconText'> <b> $crs_city</b></span></i></button></li>
        </ul>
      </div></center>
      <div class='col-xs-12 col-sm-12 col-md-7 excerpet'>
        <h3 id='resultHeading'><a href='#' id='headingLinking'><b>$crs_title</b></a></h3>
        <p id='courseshortDescription'>
$crs_sdesc
        </p>  
        <center><button class='btn btn-danger' id='findoutBtn'><a href='details.php?crs_id=$crs_id' style='color:white;'>Find Out More</a></button></center>     

      <span class='clearfix borda'></span>
    </article>


        ";

print "$outputList"; 
echo $paginationDisplay;

    }
}

任何帮助将不胜感激。如果您需要任何澄清,请告诉我。

$get_crs = mysql_query("select * from books where book_provider='$provider_id' $limit");
...
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 

您希望如何使用$limit?在使用它之前,必须对其进行定义。

将来,请更仔细地查看您遇到的错误:

注意:未定义的变量:C:''xampp''htdocs''t69''functions''functions.php 第 262 行中的限制

未定义的变量:限制

未定义的变量:限制

未定义的变量:限制

未定义的变量:限制

在 262 行

错误实际上告诉你问题

注意:未定义的变量:限制在 C:''xampp''htdocs''t69''functions''functions.php 第 262 行

在使用限制之前需要声明限制。

默认搜索条件或切换这些行。

$get_crs = mysql_query("select * from books where book_provider='$provider_id' $limit");
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 

您需要替换:

$get_crs = mysql_query("select * from books where book_provider='$provider_id' $limit");

跟:

$get_crs = mysql_query("SELECT * FROM `books` WHERE `book_provider`='$provider_id' LIMIT $limit");

我大写了您的 SQL,以更清楚地了解您制定的查询中的内容(我还转义了数据库表和列名。这些只是最佳实践。抱歉,我忘了提到您还需要实际定义限制。