0

我的网站有问题。

我有一张带有 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 "\">&laquo; 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 &raquo;</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 方法的末尾。我认为除了照片共享之外没有其他任何东西使用它。我将不得不查看代码。

感谢您对此的帮助。

直流

我只是补充说:

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。如果不清楚并且有人需要帮忙,让我知道。

4

1 回答 1

0

As G-Nugget mentioned. NULL is better suited for this. Your query also looks odd. The usual syntax is:

SELECT * FROM photographs WHERE id != 0

Having is used to apply filtering on GROUP BY queries.

Edit: It also looks like you are using $total_count from pagination here:

$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> ";
}

This will show a link for more pages than exist since $total_count includes the 0 record.

于 2012-12-11T04:11:16.200 回答