2013-12-23 38 views
0

我一起搜索了一個mysql數據庫,但我只知道如何創建指向下一頁的鏈接。當我滾動到底部時,是否有任何方法來加載更多數據。如果是這樣,如何使用下面的代碼來實現它?在向下滾動時加載更多搜索數據

$currentPage = $_SERVER["PHP_SELF"]; 

$maxRows_searchHH = 20; 
$pageNum_searchHH = 0; 
if (isset($_GET['pageNum_searchHH'])) { 
    $pageNum_searchHH = $_GET['pageNum_searchHH']; 
} 
$startRow_searchHH = $pageNum_searchHH * $maxRows_searchHH; 

$colname_searchHH = "-1"; 
if (isset($_GET['searchString'])) { 
    $colname_searchHH = $_GET['searchString']; 
} 
mysql_select_db($database_conn_happyhours, $conn_happyhours); 
$query_searchHH = sprintf("SELECT name, address, hhName, phone, dayOfTheWeek, hours, `description`, imageURL, website, cost, googleMap FROM happyhours WHERE address LIKE %s ORDER BY name ASC", GetSQLValueString("%" . $colname_searchHH . "%", "text")); 
$query_limit_searchHH = sprintf("%s LIMIT %d, %d", $query_searchHH, $startRow_searchHH, $maxRows_searchHH); 
$searchHH = mysql_query($query_limit_searchHH, $conn_happyhours) or die(mysql_error()); 
$row_searchHH = mysql_fetch_assoc($searchHH); 

if (isset($_GET['totalRows_searchHH'])) { 
    $totalRows_searchHH = $_GET['totalRows_searchHH']; 
} else { 
    $all_searchHH = mysql_query($query_searchHH); 
    $totalRows_searchHH = mysql_num_rows($all_searchHH); 
} 
$totalPages_searchHH = ceil($totalRows_searchHH/$maxRows_searchHH)-1; 

$queryString_searchHH = ""; 
if (!empty($_SERVER['QUERY_STRING'])) { 
    $params = explode("&", $_SERVER['QUERY_STRING']); 
    $newParams = array(); 
    foreach ($params as $param) { 
    if (stristr($param, "pageNum_searchHH") == false && 
     stristr($param, "totalRows_searchHH") == false) { 
     array_push($newParams, $param); 
    } 
    } 
    if (count($newParams) != 0) { 
    $queryString_searchHH = "&" . htmlentities(implode("&", $newParams)); 
    } 
} 
$queryString_searchHH = sprintf("&totalRows_searchHH=%d%s", $totalRows_searchHH, $queryString_searchHH); 

回答

0

我會建議你使用JavaScript,jQuery和HTML:

1)頁的第一個結構HTML:網頁上有您想要一個div追加更多的數據

<div id="divMyData"></div> 

2)用於獲取基於提供的數據更多的數據來創建一個函數,然後使用它的滾動事件,關鍵是,你將需要得到JSON格式你的下一個頁面的數據。

<script type="text/javascript"> 
    var currentPage = 0; 
    var isShowMore = false; 

    function loadData(c){ 
     $.ajax({ 
      cache: !1, 
      type: "POST", 
      url: e, // url of the method to call 
      data: $.toJSON(c), // you are passing the parameter needed by method called 
      contentType: "application/json; charset=utf-8", 
      success: function (result) { 
       // Key to append 
       $("#divMyData").html($("#divMyData").html() + result.htmlData); 
       // and you may implement more logics according to your requirements 
       isShowMore = false; 
      }, 
      error: function (xhr, ajaxOptions, thrownError) { 
       //alert(xhr.status); 
      } 
     }); 
    } 

    function scrollEvent(e) { 
     var body = document.body, 
      html = document.documentElement; 

     var docHeight = Math.max(body.scrollHeight, body.offsetHeight, 
           html.clientHeight, html.scrollHeight, html.offsetHeight); 
     var currentScroll = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop; 
     // condition to check if scroll is at bottom (25% height of document you may change it according to your requirement) 
     if (((docHeight * 75)/100) < currentScroll) { 
      if (!isShowMore) { 
       isShowMore = true; 
       currentPage = currentPage + 1; 
       //i am providing an example of data that you can pass to ajax as variable c, change it according to your need 
       var c = { 
         categoryId: 1, 
         Range: "100 - 40000", 
         pageNumber: currentPage, 
         orderby: "name" 
        }; 
       loadData(c); 
      } 
     } 
    } 

    // Attaching scroll event when document/window is loaded 
    function OnFirstLoad() { 
     if (document.attachEvent) { 
      document.attachEvent('onscroll', scrollEvent); 
     } else if (document.addEventListener) { 
      document.addEventListener('scroll', scrollEvent, false); 
     } 

    } 
    window.onload = OnFirstLoad; 
</script> 

htmlData是對象屬性的名稱,您正在返回所調用的服務器方法。我在服務器端執行php

相關問題