Codeigniter获得带限制的随机记录

时间:2013-01-17 15:04:04

标签: sql codeigniter jquery codeigniter-2

每次使用codeigniter和Ajax向下滚动页面时,我都创建了一个加载4个产品的页面。

我遵循这个tutorial来创建与codeigniter和jQuery的分页。
一切正常,而且我已经使用Ajax从数据库中更改了加载类型。

我遇到了codeigniter的问题。 当我试图从表格中随机记录时,我得到了重复的产品。

这是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;
}

更新脚本<​​/ p>

<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 个答案:

答案 0 :(得分: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附加到你正在显示的每个产品上使用数据属性或其他东西并以这种方式生成数组。