我正在尝试为搜索结果添加分页功能 我在以下行遇到以下错误:
注意:未定义的变量:第262行的C:\ xampp \ htdocs \ t69 \ functions \ functions.php中的限制
以下是第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个以上只有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 .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
} else if ($pn == $lastPage) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> ';
} else if ($pn > 1 && $pn < $lastPage) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
}
// 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. ' ';
// If we are not on page 1 we can place the Back button
if ($pn != 1) {
$previous = $pn - 1;
$paginationDisplay .= ' <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 .= ' <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;
}
}
非常感谢任何帮助。 如果您需要任何澄清,请告诉我。
答案 0 :(得分:1)
$get_crs = mysql_query("select * from books where book_provider='$provider_id' $limit");
...
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
您希望如何使用$limit
?必须在之前定义,然后使用它。
将来,请仔细查看您获得的错误:
注意:第262行的C:\ xampp \ htdocs \ t69 \ functions \ functions.php中的未定义变量:limit
未定义变量:限制
未定义的变量:limit
未定义变量:limit
未定义变量:limit
第262行
答案 1 :(得分:1)
该错误实际上告诉您问题
注意:未定义的变量:第262行的C:\ xampp \ htdocs \ t69 \ functions \ functions.php中的限制
需要在使用之前声明限制。
默认搜索条件或切换这些行。
$get_crs = mysql_query("select * from books where book_provider='$provider_id' $limit");
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
答案 2 :(得分:0)
您需要替换:
$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,以便更清楚你的公式化中的内容是什么(我还逃脱了数据库表和列名。这些只是最佳实践。抱歉,我忘了提到你还需要实际定义限制。