我之前发布了这个脚本来修复分页错误,现在我需要更新它以摆脱折旧的mysql但是我遇到了ceil(mysql_result)和mysql_fetch_assoc($ query)部分的问题。
$per_page = 25;
$pages_query = mysql_query("SELECT count(*) FROM my_db WHERE StartDate >= CURDATE()");
$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> ";}
}
}
答案 0 :(得分:0)
习惯于清楚地命名变量:$ pages_query应该是$ pagesResultset。 这就是本质:
$dbCon = new mysqli(host,user,pas,db);
$resultset = $dbCon->query("select ...");
if($resultset->num_rows == 0) die('issues');
$pageSize = 25;
$pageCount = ceil($resultset->num_rows / $pageSize);
while($row = $resultset->fetch_assoc()) {
/* do something */
}
$dbCon->close();