我的網站有問題。在id!= 0的MySQL數據庫中選擇記錄
我有一個帶有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,並返回給你,如果它的工作。我想我可以將WHERE ID!= 0添加到count_all方法的末尾。除了照片分享以外,我認爲別的什麼都不用。我將不得不查看代碼。
感謝您的幫助。
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.如果這不明確,並且有人需要幫助它,讓我知道。
看起來你應該使用'NULL'而不是'0'。 –