使用php和mysql进行Ajax分页

时间:2012-08-07 09:19:06

标签: php jquery mysql ajax paging

我正在尝试使用Ajax在我的网站上设置分页,我已经继承了一个脚本,并在以下链接中将其付诸实践 - http://www.testing.arrivaldesign.co.uk/properties

我已经将它工作到一定程度,但它设置为显示前9条记录然后从那里继续,但它只显示第一页上的前9条,但是当你点击下一页时它只重复了4条现有记录。

据我所知,这与限制我的查询有关,但我不知道如何让它工作?

这是ajax方面的代码。

<?php 
include('Connections/connection.php');

include 'functions.php';

// Pagination params
$basePath       = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
$qString        = $_REQUEST['qString'];
$items          = $_REQUEST['items'];
$loadPage       = $_REQUEST['p'];
$current        = $_REQUEST['current'];

$limit = $loadPage*$items;
$min = ($max-$items)+1;

mysql_select_db($database, $conn);
$query_RSproperty = "SELECT properties.*, type.* FROM (properties LEFT JOIN type ON properties.propType=type.typeID) WHERE offline = 'N' ORDER BY propID ASC LIMIT 0, $limit";
$RSproperty = mysql_query($query_RSproperty, $conn) or die(mysql_error());
$row_RSproperty = mysql_fetch_assoc($RSproperty);
$totalRows_RSproperty = mysql_num_rows($RSproperty);

$maxItems       = $totalRows_RSproperty;

// New pagination
$pagination = paginator($basePath . $qString, $loadPage, $maxItems, $items);

// Direction is important for the script to determine which way to animate, left or right. 
$direction = 'left';
if ($current < $loadPage) {
    $direction = 'right';
}
$paginatedStyle = 'style="left:'.($direction == 'left' ? '0' : '-960px').';"';


// The paginated content HTML slide
$page = '<div class="paginated" id="" '.$paginatedStyle.'>';

ob_start();
do {
?>
<div class="grid-1third res-block">
<div class="prop-brief-desc">
            <div class="grid-140"><a href="/properties/<?php echo $row_RSproperty['propSEO'] . '/' . $row_RSproperty['propID']; ?>" id="new<?php echo $no; ?>" class="thumb"><img src="/prop-images/thumbs/<?php echo $row_RSproperty['propImage1']; ?>" width="140" height="105" alt=""></a></div>
            <div class="grid-140 fr">
              <h2><?php echo $row_RSproperty['propBeds']; ?> Bed <?php echo $row_RSproperty['typeName']; ?></h2>
              <?php
              $fulladdress = $row_RSproperty['propAddress1'] . '<br />' . $row_RSproperty['propCity'] . ', ' . $row_RSproperty['propCounty'] . '<br />' . $row_RSproperty['propPostcode'];
              ?>
              <p><?php echo $fulladdress; ?></p>
            </div>
          </div>
          <div class="prop-brief-options<?php echo $no == 2 || $no == 3 ? " hide" : ""; ?>" id="newopt<?php echo $no; ?>">
            <div class="grid-140"> <a href="/properties/<?php echo $row_RSproperty['propSEO'] . '/' . $row_RSproperty['propID']; ?>" class="green_button">Details</a> <a href="#" class="green_button">Arrange Viewing</a> <a href="#" class="green_button">Place Bid</a> <a href="#" class="green_button">Buy it Now</a> </div>
            <div class="grid-140 fr">
              <dl>
                <dt>Auction Ending:</dt>
                <dd><?php
                if(!function_exists('countdown'))  {            
                    function countdown($year, $month, $day, $hour, $minute) {

                        $the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1);
                        $current = time();

                        $difference = $the_countdown_date - $current;
                        if ($difference < 0) $difference = 0;

                        $days = floor($difference/60/60/24);
                        $hours = floor(($difference - $days*60*60*24)/60/60);
                        $minutes = floor(($difference - $days*60*60*24 - $hours*60*60)/60);

                        echo $days."d ".$hours."h ".$minutes."m";
                    }
                }

                $theyear = date("Y",strtotime($row_RSproperty['propEndDate']));
                $themonth = date("n",strtotime($row_RSproperty['propEndDate']));
                $theday = date("d",strtotime($row_RSproperty['propEndDate']));
                $thehour = date("H",strtotime($row_RSproperty['propEndDate']));
                $theminute = date("i",strtotime($row_RSproperty['propEndDate']));

                countdown($theyear,$themonth,$theday,$thehour,$theminute);
                ?></dd>
                <?php if ($row_RSproperty['propCurrBid'] > 0) { ?>
                <dt>Current bid:</dt>
                <dd>£<?php echo number_format($row_RSproperty['propCurrBid']); ?></dd>
                <?php } else { ?>
                <dt>Starting Price:</dt>
                <dd>£<?php echo number_format($row_RSproperty['propStartPrice']); ?></dd>
                <?php } ?>
                <dt>Buy it now:</dt>
                <dd><span class="green">£<?php echo number_format($row_RSproperty['propBinPrice']); ?></span></dd>
              </dl>
            </div>
          </div>
          </div>
<?php       
    } while ($row_RSproperty = mysql_fetch_array($RSproperty));
    /*while ($min <= $max) {
        $page .= '<li>'.$min.'</li>';
        $min++;
    }*/
    $page .= ob_get_contents();
    ob_end_clean();

$page .= '</div>';


// return the JSON
echo json_encode(array( 'pagination' => $pagination, 'page' => $page, 'current' => $loadPage ));
exit;

?>

非常感谢

克里斯

1 个答案:

答案 0 :(得分:1)

那是因为您在查询中将LIMIT硬编码为0

$query_RSproperty = "SELECT properties.*, type.* FROM (properties LEFT JOIN type ON properties.propType=type.typeID) WHERE offline = 'N' ORDER BY propID ASC LIMIT 0, $limit";

因此,当您在第二页上移动时,必须生成查询,如下面的

$query_RSproperty = "SELECT properties.*, type.* FROM (properties LEFT JOIN type ON properties.propType=type.typeID) WHERE offline = 'N' ORDER BY propID ASC LIMIT 0, 4"; 

因此你获得前4条记录。如果要在后续页面上检索下一组记录,则必须在LIMIT 0, $limit动态中设为0,如:

$query_RSproperty = "SELECT properties.*, type.* FROM (properties LEFT JOIN type ON properties.propType=type.typeID) WHERE offline = 'N' ORDER BY propID ASC LIMIT $offset, $limit";

您必须根据每页显示的结果计算$ offset。在第一页上,偏移量始终为0.如果每页显示10条记录,则在第二页上,偏移量为11,第三个偏移量为21,依此类推。