2014-07-11 33 views
-1

如何使負載更多按鈕工作,使限制0,5轉換爲限制0,10,並在第三次點擊它將轉換爲限制0,15和最大值達到LIMIT 0,30PHP中的AJAX更多按鈕獲取更多記錄

我使用下面的PHP,MySQL的從數據庫

$select_resdetails = mysql_query("SELECT *, ((ACOS(SIN(-27.486204 * PI() /180) * SIN(`store-latitude` * PI() /180) + COS(-27.486204 * PI() /180) * COS(`store-latitude` * PI() /180) * COS((152.994962 - `store-longitude`) * PI() /180)) *180/PI()) *60 * 1.1515) AS `distance` FROM (SELECT max(if(`field_name`='store-name', `field_value`, null)) AS `store-name`, 
max(if(`field_name`='store-description', `field_value`, null)) AS `store-description`, max(if(`field_name`='store-longitude', `field_value`, null)) AS `store-longitude`, max(if(`field_name`='store-latitude', `field_value`, null)) AS `store-latitude` FROM `wp_cf7dbplugin_submits` WHERE `form_name` = 'Add Store' GROUP BY `submit_time`) A ORDER BY `distance` LIMIT 0,5"); 
?> 
<form class="ui-filterable"> 
     <input id="myFilter" data-type="search"> 
</form> 
<ul data-inset="true" data-role="listview" data-filter="true" data-input="#myFilter" style="padding:0px;"> 
<?php 
while($fetch_resdetails = mysql_fetch_array($select_resdetails)) { 
echo '<li> 
     <h2>' . $fetch_resdetails['store-name'] . '</h2> 
     <p>' . number_format(($fetch_resdetails['distance']), 2, '.', '') . ' KM <em>' . $fetch_resdetails['store-description'] . '</em></p> 
     </a> 
     </li> 
'; 
} 
?> 
<input type="button" value="Load More" /> 
</ul> 

回答

1

提取記錄保存在本地存儲的點擊量

if(limit <= 30){ 
    limit = limit + 5; 
    localStorage.setItem("limit", limit); 
} 

不要忘記最初的變量,並默認值5

而阿賈克斯

使用GET方法 發送此變量,併發送參數

$.get("example.php?limit="+localStorage.getItem("limit"), 
function() { 
    alert("success"); 
}) 

在你的PHP文件中使用$ _GET [ '限制']讀極限變量

0

我正在使用Yii框架來解決這個問題。

中查看文件:

videos_limit
<div id="own_video_list"> 
<a href="javascript: void(0)" class= "load" id="'.$videos_limit.'">View more</a> 
</div> 

這裏$是第一個限制(如5)。

在JS

$('#own_video_list .load').live('click', function() { 
    var lastaction = $(this).attr("id"); 
    var type  = 'own_video_list'; 

    loadMoreVideos(type, lastaction); 
    return false; 

});

function loadMoreVideos(type, lastaction) 
{ 
    if(lastaction) 
    { 
    $("#"+type+" a#"+lastaction).html('<img src = "/images/loading.gif" alt = "loading"> Loading...'); 
    $.ajax({ 
     type: "POST", 
     url: "/videos/home/loadmorevideoshome", 
     data: "type="+ type + "&lastId=" +lastaction, 
     success: function(html){ 
      $("#"+type+" a.load").parent().parent().remove(); 
      $("#"+type).append(html); 
      $('body').animate({scrolltop: $('body')[0].scrollHeight}); 
     } 
    }); 
    } 
    else 
    { 
    $("#"+type+" .load").html("no more video"); 
    } 

}

在控制器

public function actionLoadmorevideoshome() 
{ 
    $type  = $_REQUEST['type']; 
    $lastId = $_REQUEST['lastId']; 
    $userId = Yii::App()->user->getId(); 
    $videoHtml = ''; 

    $videos = VideoList::getMostRecentVideos(self::RECORD_PER_PAGE, $userId , $lastId, 1); 

    if(is_array($videos) && (count($videos) > 0)) 
    { 
    $videoHtml .= $this->renderPartial('_view', array(
             'videoList' => $videos, 'type' => $type,) 
             , true, false); 
    $lastId = $lastId + count($videos); 

    $videoHtml .= '<div class="video_col_01"> 
         <div style="margin-left: 20px;" class="search_btn"> 
         <a href="javascript: void(0)" class= "load" id="'.$lastId.'">View more</a> 

         </div> 
        </div>'; 

    } 
    else 
    { 
     $videoHtml .= '<div class="video_col_01"><font color="red">No More Video</font></div>'; 
    } 

    echo $videoHtml; 
    Yii::app()->end(); 
} 
+0

你能縮小你的代碼,實際上是解決問題的一部分?如果所有這些都是必要的,也許它不是op問題的正確答案。 –