将分页集成到mysql查询中

时间:2012-06-03 17:23:29

标签: php mysql pagination

我有以下mysql查询,我从这里添加了分页: http://www.tonymarston.net/php-mysql/pagination.html

$DBQuery3 = mysqli_query($dblink, "SELECT * FROM images WHERE project_id = '$FormProjectID'");

if (mysqli_num_rows($DBQuery3) < 1) {
    $ProjectContent = '
        <p>This project is empty. <a href="index.php?page=upload&id='.$FormProjectID.'">Upload</a> some files to get started.</p>
    ';
} else {

    //if no page number is set, start at page 1
    if (isset($_GET['pageno'])) {
        $pageno = $_GET['pageno'];
    } else {
        $pageno = 1;
    }

    //This code will count how many rows will satisfy the current query.
    $DBQuery3b = mysqli_query($dblink, "SELECT count(*) FROM images WHERE project_id = '$FormProjectID'");
    $query_data = mysqli_fetch_row($dblink, $DBQuery3b);
    $numrows = $query_data[0];

    //This code uses the values in $rows_per_page and $numrows in order to identify the number of the last page.
    $rows_per_page = 2;
    $lastpage = ceil($numrows/$rows_per_page);

    //This code checks that the value of $pageno is an integer between 1 and $lastpage.
    $pageno = (int)$pageno;
    if ($pageno > $lastpage) {
       $pageno = $lastpage;
    }
    if ($pageno < 1) {
       $pageno = 1;
    }

    //This code will construct the LIMIT clause for the sql SELECT statement.
    $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

    $DBQuery3c = "SELECT * FROM images WHERE project_id = $FormProjectID $limit";
    $DBQuery3d = mysqli_query($dblink, $DBQuery3c);

        //set this variable to empty and so we can latwe loop and keep adding images to it
        $ProjectContent ='';
        while($row = mysqli_fetch_array($DBQuery3d)) {
            $DBImageID = $row['image_id'];
            $DBProjectID = $row['project_id'];
            $DBImageName = $row['image_name'];
            $DBImageDescription = $row['image_description'];
            $DBDateCreated = $row['date_created'];
            $DBLinkToFile = $row['link_to_file'];
            $DBLinkToThumb = $row['link_to_thumbnail'];
            $DBGivenName = $row['given_name'];

            //if the image was given a name by the user, display it
            //otherwise display the generated name

            if (strlen($DBGivenName) > 1) {
                $FileName = $DBGivenName;
            } else {
                $FileName = $DBImageName;
            }

            $ProjectContent .= '
                <a href="index.php?page=image&id='.$DBImageID.'"><img src="uploads/'.$DBLinkToThumb.'" width="150px" height="150px" alt="'.$FileName.'" title="'.$FileName.'"/></a>
            ';

            //Finally we must construct the hyperlinks which will allow the user to select other pages. We will start with the links for any previous pages.
        if ($pageno == 1) {
           $FirstPrev = " FIRST PREV ";
        } else {
           $First = " <a href='{$_SERVER['PHP_SELF']}?page=project&id=$FormProjectID&pageno=1'>FIRST</a> ";
           $prevpage = $pageno-1;
           $Prev = " <a href='{$_SERVER['PHP_SELF']}?page=project&id=$FormProjectID&pageno=$prevpage'>PREV</a> ";
        }

        //Next we inform the user of his current position in the sequence of available pages.
        $PageNumb = " ( Page $pageno of $lastpage ) ";

        //This code will provide the links for any following pages.
        if ($pageno == $lastpage) {
           $NextLast = " NEXT LAST ";
        } else {
           $nextpage = $pageno+1;
           $Next = " <a href='{$_SERVER['PHP_SELF']}?page=project&id=$FormProjectID&pageno=$nextpage'>NEXT</a> ";
           $Last = " <a href='{$_SERVER['PHP_SELF']}?page=project&id=$FormProjectID&pageno=$lastpage'>LAST</a> ";
        }
    }
}

然后在我的HTML中我有:

<div id="projectview">
        <?php echo $ProjectContent; ?>

        <?php echo $FirstPrev; ?>
        <?php echo $First; ?>

        <?php echo $Prev; ?>

        <?php echo $PageNumb; ?>

        <?php echo $NextLast; ?> 

        <?php echo $Next; ?> 
        <?php echo $Last; ?> 
        <div class="clear-div"></div>
    </div>

例如在这种情况下输出2个图像,但分页链接如下所示:

FIRST PREV ( Page 1 of 0 ) NEXT LAST

单击链接不会循环显示任何其他图像,它仍会显示相同的2张图像。

我无法弄清楚我在这里做错了什么。我不明白为什么它说“1 of 0”,显然应该有更多的结果。

0 个答案:

没有答案