weixin_33675507 2012-08-07 09:19 采纳率: 0%
浏览 30

使用php和mysql进行Ajax分页

I am trying to setup paging on my site using Ajax, I've inherited a script and put it into practice at the following link - http://www.testing.arrivaldesign.co.uk/properties.

I've got it working to an extent, but it's set to show the first 9 records and then carry on from there, but it's only showing the first 9 on the first page, but then when you click to the next page it just repeats 4 of the existing records.

As far as I can see it's to do with limit on my query, but I don't know how to get it working?

This is the code for the ajax side of things.

<?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;

?>

Many thanks

Chris

  • 写回答

1条回答 默认 最新

  • weixin_33724570 2012-08-07 09:24
    关注

    That's because you have hard coded LIMIT as 0 in your query

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

    So when you move on second page, the query must be getting generated like follow

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

    hence you re getting first 4 records. If you want to retrieve the next set of records on subsequent pages, then you have to make 0 in LIMIT 0, $limit dynamic like:

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

    You have to calculate $offset depending on how much results you are displaying per page. On first page, offset will always be 0. If you are displaying 10 records per page, then on second page, offset will be 11, on third offset will be 21 and so on.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题