Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:19:58+00:00 2026-06-17T11:19:58+00:00

I’ve created a page that load 4 products everytime you scroll down the page

  • 0

I’ve created a page that load 4 products everytime you scroll down the page with codeigniter and Ajax.

I followed this tutorial for create pagination with codeigniter and jQuery.
Everything works fine, moreover I’ve changed the load type from database using Ajax.

I’ve got a problem with codeigniter.
When I try to get random record from the table I’ve got duplicate products.

This is the functions in codeigniter:

UPDATE CONTROLLER

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:19:59+00:00Added an answer on June 17, 2026 at 11:19 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.