分页后,使用PDO仅显示一行

时间:2015-03-07 21:06:50

标签: php mysql pdo

我正在从mysql转换到PDO。当我尝试添加页码以显示查询结果时,我遇到了一些问题。 这是代码:

$query_RS_Search = "SELECT * FROM products WHERE Category LIKE :category AND hidden = 'no'";
if (($_GET['Category']) =='Keyboards'){
switch( $_GET['price'] ){
    case '':
        $query_RS_Search .= ' AND Category != "Recent Keyboards" ORDER BY price ASC';
        break;
    case '0-500':
        $query_RS_Search .= ' AND Category != "Recent Keyboards" AND products.price BETWEEN 0 AND 500 ORDER BY Keysound_price ASC';
        break;
    case '500-1000':
        $query_RS_Search .= ' AND Category != "Recent Keyboards" AND products.price BETWEEN 500 AND 1000 ORDER BY price ASC';
        break;


}
}else{switch( $_GET['price'] ){
    case '':
        $query_RS_Search .= ' ORDER BY price ASC';
        break;
    case '0-500':
        $query_RS_Search .= ' AND products.price BETWEEN 0 AND 500 ORDER BY price ASC';
        break;
    case '500-1000':
        $query_RS_Search .= ' AND products.price BETWEEN 500 AND 1000 ORDER BY Keysound_price ASC';
        break;

}
}


$RS_Search = $conn->prepare($query_RS_Search) or die(mysql_error());
$RS_Search->bindValue(':category', '%' .  str_replace('-', ' ',$_GET['Category']) . '%' );
$RS_Search->execute();
$row_RS_Search = $RS_Search->fetch();
$row = $RS_Search->rowCount();

// Here we have the total row count
$rows = $row[0];
// This is the number of results we want displayed per page
$page_rows = 7;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
    $last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// This is your query again, it is for grabbing just one page worth of rows by applying $limit


$RS_Search = $conn->prepare($query_RS_Search.$limit) or die(mysql_error());
$RS_Search->bindValue(':category', '%' .  str_replace('-', ' ',$_GET['Category']) . '%' );
$RS_Search->execute();


// This shows the user what page they are on, and the total number of pages
$textline1 = "Testimonials (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
    /* First we check if we are on page one. If we are then we don't need a link to 
       the previous page or the first page so we do nothing. If we aren't then we
       generate links to the first page, and to the previous page. */
    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';
        // Render clickable number links that should appear on the left of the target page number
        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            }
        }
    }
    // Render the target page number, but without it being a link
    $paginationCtrls .= ''.$pagenum.' &nbsp; ';
    // Render clickable number links that should appear on the right of the target page number
    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
        if($i >= $pagenum+4){
            break;
        }
    }
    // This does the same as above, only checking if we are on the last page, and then generating the "Next"
    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
    }
}

这只会在尝试显示时产生一行:

<?php do { ?>
<?php echo $row_RS_Search['Manufacturer']; ?> <?php echo $row_RS_Search['Model']; ?> in <?php echo $row_RS_Search['Color']; ?>

<?php } while ($row_RS_Search = $RS_Search->fetch()); ?>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>

我想每页显示7行。 欢迎任何帮助

1 个答案:

答案 0 :(得分:0)

$limit字符串中缺少空格,您稍后会将其连接到prepare()语句。

// This sets the range of rows to query for the chosen $pagenum
$limit = ' LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;