我正在尝试为图片库添加一个简单的分页,但我达到了一个我没有收到任何错误消息的地方,但它仍然无效。我不知道在哪里寻找错误。 我的代码是;
<?php
include 'core/init.php';
ini_set('display_errors', 'On'); // aan of uit
error_reporting(E_ALL);
?>
<body>
<div id="wrap">
<div id="header">
<h1>Rob Cnossen</h1>
</div>
<div id="titel">
<?php
if (isset($_GET['album_id'])) {
$album_id = $_GET['album_id'];
$album_data = $albums->album_data($album_id, 'name', 'description');
echo '<h2>', $album_data['name'], '</h2>';
$albums = $albums->get_albums();
$images = $images->get_images($album_id);
}
?>
</div>
<div id="sidebarleft">
<?php
if (empty($images)) {
echo 'Er zijn geen foto\'s in dit album';
} else {
foreach ($albums as $album) {
foreach ($images as $image) {
?><div id="fotoos"><?php
if ($image["album"] === $album["id"])
echo'<a href="view_album.php?album_id=', $album["id"],'&image_id=uploads/', $image["album"], '/', $image["img_name"],'"><img src="uploads/thumbs/', $image["album"], '/', $image["img_name"],'" title="" /></a><div id="kruisje"><a href="delete_image.php?image_id=', $image["id"],'">_|</div></a>';
?></div><?php
}
}
}
?>
<div id="pagination">
<?php
$count_query = $db->prepare('SELECT * FROM images where album_id= ?');
$count_query->bindValue(1, $album_id);
try{
$count_query->execute();
}catch (PDOException $e){
die($e->getMessage());
}
$count = $count_query->fetchColumn();
if(isset($_GET['page'])){
$page = preg_replace("#[^0-9]#,",$_GET['page']);
}else{
$page = 1;
}
$limit = 2;
$lastPage = ceil($count/$limit);
if($page<1){
$page = 1;
}elseif($page>$lastPage){
$page = $lastPage;
}
$offset = ($page-1)*$limit;
$query = $db->prepare('SELECT image_id FROM images WHERE album_id= ? ORDER BY image_id DESC
LIMIT ?,?');
$query->bindValue(1, $album_id);
$query->bindParam(2, $offset, PDO::PARAM_INT);
$query->bindParam(3, $limit, PDO::PARAM_INT);
try{
$query->execute();
}catch (PDOException $e){
die($e->getMessage());
}
if($lastPage !=1){
if($page != $lastPage){
$next = $page + 1;
$pagination='<a href="'. $_SERVER['REQUEST_URI'] .'?page='.$next.'">Volgende</a>';
}
if($page != $lastPage){
$prev = $page - 1;
$pagination.='<a href="'. $_SERVER['REQUEST_URI'] .'?page='.$prev.'">Vorige</a>';
}
}
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo $row['image_id'];//This echoes out two image id's, at the moment that is number 75 and 73
}
echo $pagination;
?>
</div>
</div>
</div>
</body>
get_image函数是;
public function get_images($album_id) {
$images = array();
$count_query = $this->db->prepare("SELECT `image_id`, `image_name`, `album_id`, `timestamp`, `ext` FROM `images` WHERE `album_id`=? ORDER BY `timestamp` DESC");
$count_query->bindValue(1, $album_id);
try{
$count_query->execute();
while ($images_row = $count_query->fetch(PDO::FETCH_ASSOC)) {
$images[] = array(
'id' => $images_row['image_id'],
'img_name' => $images_row['image_name'],
'album' => $images_row['album_id'],
'timestamp' => $images_row['timestamp'],
'ext' => $images_row['ext']
);
}
return $images;
}catch(PDOException $e){
die($e->getMessage());
}
}
我认为$ pagination变量有问题,但我不知道是什么。我在这张专辑中有7张图片,$ limit是2,仍有7张图片都显示出来。如果我点击一个分页链接,网址会显示我点击了一个分页链接,但这就是全部。如果我点击下一个或上一个链接上的vife次数,则网址可以显示我的内容; http://www.robcnossen.nl/view_album.php?album_id=8?page=2?page=2?page=0?page=2?page=0
该网站为http://www.robcnossen.nl/view_album.php?album_id=8
我希望你能看出导致问题的原因。 谢谢......
答案 0 :(得分:0)
如果我是你,我会从网上下载一个有效的解决方案并根据你的具体情况进行调整,或者考虑到一些SoC(关注点分离)来重构整个事情。
pageNum
中只有url
会更改pageNum
数组)first
/ last
后面的逻辑,以及prev
/ next
通过清理代码并封装某些功能来帮助自己。例如:
function html_a($href, $content) {
return '<a href="'.$href.'">'.$content.'</a>';
}
function pag_href($url, $id) {
return sprintf($url,$id);// url: host/target.php?pageNum=%s&someOtherstuff...
}
// usage inside loop
echo html_a(pag_href($url,$id),$pageNum);
// if you do this right, your pagination code can become reusable
使用一些mysql功能对您有利。您可以使用LIMIT
限制结果数量,使用OFFSET
设置页面的第一个元素,并使用SQL_CALC_FOUND_ROWS
获得总行数
// This way is clearer for me
$q = 'SELECT SQL_CALC_FOUND_ROWS image_id FROM images WHERE album_id=? ORDER BY image_id DESC LIMIT ? OFFSET ?'
// after executing your query you may now use pdo's foundRows
$totalNumEntries = $pdo->foundRows();
// get number of pages with pageSize
$totalNumPages = (int)ceil($totalNumEntries / $yourPageSize);
总而言之,尝试练习一些关注/分离问题,提前做一些规划,你不必处理甚至不想调试的代码
对不起,这对现有代码没有帮助