2013-01-17 75 views
0

我已經創建了一個頁面,每次使用codeigniter和Ajax向下滾動頁面時都會加載4個產品。Codeigniter獲取帶限制的隨機記錄

我跟着這個tutorial爲codeigniter和jQuery創建分頁。
一切工作正常,而且我使用Ajax從數據庫更改了加載類型。

我遇到了codeigniter的問題。 當我嘗試從表中獲取隨機記錄時,我得到了重複的產品。

這是笨的功能:

更新控制器

function index() 
{ 
    $this->load->helper('url'); 
    $data['description'] = "Description"; 
    $data['keywords'] = "Keywords"; 
    $data['products'] = $this->abitainterni->getAllProductsLimit(); 
    $data['get_products'] = $this->abitainterni->get_products(); 
    $this->load->view('welcome', $data); 
} 

function get_products($offset) 
{ 
    $already_used = $this->input->post('already_used'); 
    $already = explode(',', $already_used); 
    $data['products'] = $this->abitainterni->getAllProductsLimit($offset, $already); 
    $arr['view'] = $this->load->view('get_products', $data, true); 
    $bossy = ''; 

    foreach($data['products'] as $p) { 
     $bossy .= $p->productID.','; 
    } 
    $arr['view'] = $bam; 
    $arr['ids'] = $bossy; 
    echo json_encode($arr); 

    return; 
} 

UPDATE SCRIPT

<script type="text/javascript"> 

$(document).ready(function(){ 

<? 
    $like_a_boss = ""; 

    foreach($products as $gp): 
     $like_a_boss .= $gp->productID.','; 
    endforeach; 
?> 

    var products = '<?= $like_a_boss; ?>'; 
    var loaded_products = 0; 

    $(".loadMoreProducts").click(function(){ 
     loaded_products += 4; 

     var dati = "welcome/get_products/" + loaded_products; 

     $.ajax({ 
      url:'welcome/get_products/' + loaded_products, 
      type: 'post', 
      data: {already_used: products}, 
      cache: false, 
      success: function(data) { 

      var obj = $.parseJSON(data); 

      $("#mainContainerProductWelcome").append(obj.view); 
     already_used += obj.ids; 

      if(loaded_products >= products - 4) { 
       $(".loadMoreProducts").hide(); 
      } else { 
       // load more still visible 
      } 
      }, 
      error: function() { 
      // there's something wrong 
      } 
     }); 

     // show spinner on ajax request starts 
     $(".loading-spinner").ajaxStart(function(){ 
      $(".loading-spinner").show(); 
      $(".text-load").hide(); 
     }); 

     // ajax request complets hide spinner 
     $(".loading-spinner").ajaxStop(function(){ 
      $(".loading-spinner").delay(5000).hide(); 
      $(".text-load").show(); 
     }); 

     return false; 
    }); 

    // submit form contact 
    $(window).scroll(function() { 
    if($(window).scrollTop() + $(window).height() >= $(document).height()) { 
     // click on load more btn 
     $(".loadMoreProducts").click(); 
     return false; 
    } 
}); 

}); 
</script> 

回答

1

你需要跟蹤產品的你已經查詢,拋他們的ID在一個數組中,然後使用類似的東西不在。所以像這樣的事情:

function getAllProductsLimit($offset=0, $already_used = array(0)) 
    { 
     $this->db->order_by('productID', 'RANDOM'); 
     $this->db->where_not_in('productID', $already_used); 
     $query = $this->db->get('product', 4, $offset); 

     if($query->num_rows() > 0){ 
     return $query->result(); 
     } else { 
     return 0; 
     } 
    } 

NEW CONTROLLER 

    function index() 
    { 
     $this->load->helper('url'); 
     $data['title'] = "Scopri i nostri prodotti"; 
     $data['description'] = "Description"; 
     $data['keywords'] = "Keywords"; 
     $data['products'] = $this->abitainterni->getAllProductsLimit(); 
     $data['get_products'] = $this->abitainterni->get_products(); 
     $this->load->view('welcome', $data); 
    } 

    function get_products($offset) 
    { 
     $already_used = $this->input->post('already_used'); 
     $already = explode(',', $already_used); 
     $data['products'] = $this->abitainterni->getAllProductsLimit($offset, $already); 
     $arr['view'] = $this->load->view('get_products', $data, true); 
      $bossy = ''; 
    foreach($data['products'] as $p) 
      { 
     $bossy .= $->productID.','; 
      } 
      $arr['view'] = $bam; 
      $arr['ids'] = $bossy; 
echo json_encode($arr); 
return; 

    } 

NEW SCRIPT 

<script type="text/javascript"> 
$(document).ready(function(){ 

     <? 
    $like_a_boss = ''; 
    foreach($get_products as $gp):?> 
    $like_a_boss .= $gp->productID.','; 
    endforeach;?> 

    var products = '<?= $like_a_boss; ?>'; 
    var loaded_products = 0; 

    $(".loadMoreProducts").click(function(){ 
     loaded_products += 4; 

     var dati = "welcome/get_products/" + loaded_products; 

     $.ajax({ 
      url:'welcome/get_products/' + loaded_products, 
      type: 'post', 
      data: {already_used: products}, 
      cache: false, 
      success: function(data) { 
      var obj = $.parseJSON(data); 

      $("#mainContainerProductWelcome").append(obj.view); 
      already_used += obj.ids;   

      if(loaded_products >= products - 4) { 
       $(".loadMoreProducts").hide(); 
      } else { 
       // load more still visible 
      } 
      }, 
      error: function() { 
      // there's something wrong 
      } 
     }); 

     // show spinner on ajax request starts 
     $(".loading-spinner").ajaxStart(function(){ 
      $(".loading-spinner").show(); 
      $(".text-load").hide(); 
     }); 

     // ajax request complets hide spinner 
     $(".loading-spinner").ajaxStop(function(){ 
      $(".loading-spinner").delay(5000).hide(); 
      $(".text-load").show(); 
     }); 

     return false; 
    }); 

    // submit form contact 
    $(window).scroll(function() { 
    if($(window).scrollTop() + $(window).height() >= $(document).height()) { 
     // click on load more btn 
     $(".loadMoreProducts").click(); 
     return false; 
    } 
}); 

}); 
</script> 

然後,無論您使用該函數,在將結果回顯給您的ajax函數之前,運行一個快速的foreach以將剛剛獲得的產品的id添加到已使用的數組中。你可以存儲這個會話,或者在你的ajax內容之間來回傳遞,或者你的ajax內容寫得很好,你不需要擔心,只需將產品ID附加到你展示的每個產品上使用數據屬性或其他東西,並以這種方式生成數組。

+0

感謝您的幫助,當我做了一個foreach的產品加載到頁面,如果我不明白,在此之前: <? if($ products!= 0){ foreach($ products as $ p){ ?> 我要檢查$ already_used var? – simo

+0

好吧,如果你想保持產品ID在會話中,你可以做到這一點,但它會在你打電話給數據庫之前。如果你發佈初始控制器,加載第4個產品的頁面以及ajax響應控制器,我可以向你展示一些代碼。 – Rooster

+0

是的,當然,我一直在更新我的問題。 – simo