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 vscode的问题提问
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM