0

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

我按照本教程使用 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;
}

更新脚本

<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>
4

1 回答 1

1

you'll need to keep track of the products you've already queried, throw their id's in an array, and then use something like a where not in. So something like this:

    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>

Then whereever your using that function, before you echo out your results to your ajax function, run a quick foreach to add the ids of the products you just got to the already used array. You can either store this is session, or pass it back and forth between your ajax stuff, or if your ajax stuff is written fine, you don't need to worry about it, just attach the product id's to each product you're displaying using a data attribute or something and generate the array that way.

于 2013-01-17T15:21:22.067 回答