我對事件頁面使用以下代碼mysql查詢顯示當前日期的分頁結果,但它將在分頁鏈接中顯示9頁,即使結果只有總共3頁等有人可以幫助我確定這是爲什麼發生?MYSQL/PHP分頁顯示9頁,無論結果是否返回
$per_page = 25;
$pages_query = mysql_query("SELECT COUNT('CompName') FROM my_db");
$pages = ceil(mysql_result($pages_query, 0)/$per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = mysql_query("SELECT *, DATE_FORMAT(StartDate, '%d/%m/%Y') StartDate, CompName, HostState, Location, competitiontypedesc FROM my_db WHERE StartDate >= CURDATE() LIMIT $start, $per_page");
while($query_row = mysql_fetch_assoc($query)) {
echo"
<li class='kleo-masonry-item event-item'>
<div class='member-inner-list animated animate-when-almost-visible bottom-to-top start-animation grey-border'>
<div class='event-cell event-date-cell'>
<p class='no-margin'>" . $query_row["StartDate"] . "</p>
</div>
<div class='event-cell name'>
<p class='no-margin'>" . $query_row["CompName"] ." " .$query_row["LastName"]. "</p></div>
<div class='event-cell'>
<p class='no-margin'>" . $query_row["Location"] . "</p>
</div>
<div class='event-cell'>
<p class='no-margin'>" . $query_row["HostState"] . "</p>
</div>
</li>"
;
}
$prev = $page - 1;
$next = $page + 1;
if(!($page <= 1)){
echo "<a href='?page=$prev'><</a> ";}
if($pages >= 1){
for($x=1; $x<=$pages; $x++){
echo ($x == $page) ? '<b><a href="?page='.$x.'">'.$x.'</a></b> ' : '<a href="?page='.$x.'">'.$x.'</a>';}
}
if(!($page >= $pages)){
echo "<a href='?page=$next'>></a> ";}
}
}
請閱讀這個問題:http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php。 – Michas
'echo $ pages'是什麼,'顯示你? –
感謝Michas,實現其折舊並將更新,代碼來自舊教程 – Dexx