我的网站遇到了问题。
我有一张带有id的照片表。当用户将文件上传到服务器时,将填充此表,这很好。我还有一个可以发布图像的论坛,图像被发送到同一张照片桌。在主页上有一个部分,其中一张照片显示在其下方的分页链接中。
现在当有人发帖到论坛并且他们没有包含图片时,它会中断。该线程不会显示,因为它正在寻找0的photo_id,它不存在。所以我上传了一个虚拟图像,将id值更改为零并尝试
$sql = "SELECT * FROM photographs HAVING id != 0 ";
$sql .= "LIMIT {$per_page} ";
$sql .= "OFFSET {$pagination->offset()}";
哪个有效,图像不再显示,但分页仍然认为那里有记录。它显示了该图像的链接,当用户点击它时,它会中断并删除一堆其他链接,这很麻烦。
是否可以从数据库中选择所有内容而不是0记录,或者从分页链接中删除链接?这是代码:
//1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
//2. records per page ($per_page)
$per_page = 1;
//3. total record count ($total_count)
$total_count = Photograph::count_all();
//Find all photos
//use Pagination instead
//$photos= Photograph::find_all();
$pagination = new Pagination($page, $per_page, $total_count);
//Instead of finding all records, just find the records
//for this page
$sql = "SELECT * FROM photographs HAVING id != 0 ";
$sql .= "LIMIT {$per_page} ";
$sql .= "OFFSET {$pagination->offset()}";
$photos = Photograph::find_by_sql($sql);
//Need to add ?page=$page to all links we want to
//maintain the current page(or store $page in $session)
?>
<div id="right">
<?php
foreach($photos as $photo):
?>
<h3 style="margin: 0;"></h3>
<div style="padding: 5px;">
<a href="photo.php?id=<?php echo $photo->id; ?>"><img src="<?php echo $photo->image_path(); ?>" width="200" alt="Photo Share Photo" /></a>
<p align="center"><?php echo $photo->caption; ?></p>
</div>
<?php }
endforeach;
?>
<div id="pagination" style="clear: both;" align="center">
<?php
if($pagination->total_pages() > 1) {
if($pagination->has_previous_page()) {
echo "<a href=\"index.php?page=";
echo $pagination->previous_page();
echo "\">« Previous</a> ";
}
for($i=1; $i <= $pagination->total_pages(); $i++) {
if($i == $page) {
echo " <span class=\"selected\">{$i}</span> ";
} else {
echo " <a href=\"index.php?page={$i}\">{$i}</a> ";
}
}
if($pagination->has_next_page()) {
echo " <a href=\"index.php?page=";
echo $pagination->next_page();
echo "\">Next »</a> ";
}
}
?><br />
<div align="center">
<a href="photo_upload.php">Upload a new photograph</a>
</div>
[编辑]
分页代码。
class Pagination {
public $current_page;
public $per_page;
public $total_count;
public function __construct($page=1, $per_page=20, $total_count=0){
$this->current_page = (int)$page;
$this->per_page = (int)$per_page;
$this->total_count = (int)$total_count;
}
public function offset() {
//Assuming 20 items per page:
//page 1 has an offset of 0 (1-1) * 20
// page 2 has an offset of 20 (2-1) * 20
//in other words, page 2 starts with item 21
return ($this->current_page - 1) * $this->per_page;
}
public function total_pages() {
return ceil($this->total_count/$this->per_page);
}
public function previous_page() {
return $this->current_page - 1;
}
public function next_page() {
return $this->current_page +1;
}
public function has_previous_page() {
return $this->previous_page() >= 1 ? true : false;
}
public function has_next_page() {
return $this->next_page() <= $this->total_pages() ? true : false;
}
}
我会调查total_pages,如果有效,请回复你。我想我可以在count_all方法的末尾添加WHERE id!= 0。除了照片分享之外,我认为没有其他任何东西可以使用它。我将不得不查看代码。
感谢您的帮助。
DC
我简单地补充道:
public static function count_all() {
global $database;
$sql = "SELECT COUNT(*) FROM ".self::$table_name." WHERE `id` **!= 0 AND `show` != 0";**
$result_set = $database->query($sql);
$row = $database->fetch_array($result_set);
return array_shift($row);
}
我把它添加到照片对象中。现在,用户可以选择是否希望他们的图像显示在照片共享部分中,如果他们没有上传带有帖子的图像,则会自动为其分配ID为0.如果这不清楚并且有人需要请帮帮我。
答案 0 :(得分:0)
正如G-Nugget所说。 NULL更适合这种情况。 您的查询看起来也很奇怪。通常的语法是:
SELECT * FROM photographs WHERE id != 0
用于对GROUP BY
个查询应用过滤。
编辑: 看起来你在这里使用$ total_count分页:
$total_count = Photograph::count_all();
<snip>
for($i=1; $i <= $pagination->total_pages(); $i++) {
if($i == $page) {
echo " <span class=\"selected\">{$i}</span> ";
} else {
echo " <a href=\"index.php?page={$i}\">{$i}</a> ";
}
这将显示更多页面的链接,因为$ total_count包含0记录。