在多个页面上显示标记的图像失败


Displaying tagged images across multiple pages fails

我觉得这是一个比什么都更合乎逻辑的问题。数据库包含通过源引用保存的图片和标签的布尔值,例如 isLandscape=1。我制作了一个系统,可以根据询问的类型遍历结果页面。以下是我面临的一个例子。我只看到第 12 页 - 第 0 页>第 22 页相同的图片。然后我开始看到新的。我想我一直忽略了这个错误,因为直到现在我才注意到它。我注意到的一件事是 page22*12pictures = 264,这与看到的第一个新图片 ID 相同。您可以在此处看到错误(只需将 p 更改为不同的页面(。

<?php
$pictureid   = -1;
$startpage   = 0;
$viewsection = -1;
$uid         = -1;  //user id
$amntperrow  = 4; //how many pictures per row, must correlate with doThumb()'s switch case amounts
$maxrows     = 3;    //how many rows of pictures to drop
if(isset($_GET['pid']) && is_int(intval($_GET['pid']))) $pictureid   = clean($_GET['pid']);
if(isset($_GET['sec']) && is_int(intval($_GET['sec']))) $viewsection = clean($_GET['sec']);
if(isset($_GET['p'])   && is_int(intval($_GET['p'])))   $startpage   = clean($_GET['p']);
$result = generateResult(array("isFlowers"), $startpage);
//**snip** -- drawing thumbnails would happen here
function generateResult($types, $page) {
    global $amntperrow;
    global $maxrows;
    $sqlWheres = "";
    $idAmnt = ($amntperrow*$maxrows)*$page;
    if(isset($types) && !empty($types)) {
        if(count($types) >= 1) {
            for($i = 0; $i<count($types); $i++) {
                $sqlWheres .= $types[$i] . "='1'";
                if($i < count($types)-1) $sqlWheres .= " AND ";
            }
        }
    }        
    $result = "SELECT * FROM pictures WHERE ";
    if(!empty($sqlWheres)) $result .= $sqlWheres . " AND " ;
    $result .= " private='0' AND id >='" . $idAmnt . "' LIMIT " . ($amntperrow*$maxrows);
    return $result;
}
?>

这似乎是我忽略的一个明显的错误。感谢您的帮助。

这两个查询之间有什么区别?

SELECT *
FROM pictures
WHERE private = '0' AND id >= '24'
LIMIT 12;

SELECT *
FROM pictures
WHERE private = '0' AND id >= '36'
LIMIT 12;

答:可能根本没有区别。 在任一情况下,数据库引擎都可以决定它要返回 id s 100 到 111 的图片 - 该结果集满足任一查询的所有条件。

请尝试如下查询:

"SELECT *
FROM pictures
WHERE private = '0'
ORDER BY id
LIMIT " . $idAmnt . ", " . ($amntperrow * $maxrows)

ORDER BY id确实是关键。 对数据库结果进行分页通常结合使用 ORDER BYLIMIT 来完成。