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

  • SEARCH
  • Home
  • 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 9173215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:32:15+00:00 2026-06-17T16:32:15+00:00

I need to load data on scroll event. After searching for it, I got

  • 0

I need to load data on scroll event. After searching for it, I got a code from this site: Code to load data on scroll.

They have 2 classes in php named Demo and MiscellaneousModel.
I used the same code just changed the name of the table.

Demo.php

<?php
  class Demo extends Controller {

function Demo(){
    parent::Controller();       
    $this->load->model('MiscellaneousModel');       
}

public function index(){
    $query = $this->MiscellaneousModel->getPhotos();
    echo json_encode (array($query));       
}
 }
?>

MiscellaneousModel.php

<?php
   class MiscellaneousModel extends Model {
function MiscellaneousModel(){
    parent::Model();        
}

function getPhotos(){   
    $this->db->select("Photo_Id,url,title");
    $this->db->from('photos');              
    $query = $this->db->get();      
    return $query->result();            
}

   }
  ?>

test.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
p{margin: 10px;
padding: 5px;}
#finalResult{
list-style-type: none;
margin: 10px;
padding: 5px;
width:300px;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="json2.js"></script>
<script>
$(document).ready(function () {
$(window).scroll(function () {          
    if ($(window).scrollTop() == ( $(document).height() - $(window).height())) {
        loadData();
    }
});

function loadData() {
    $.ajax({
        type: "post",
        url: "http://localhost/test/test.php",
        cache: false,               
        data:'',
        success: function(response){

            var obj = JSON.parse(response);
            try{
                var str = '';
                var items=[];   
                $.each(obj[0], function(i,val)
                                    {                                                           
                                                items.push($('<li/>').text(val.url));
                                                items.push($('<li/>').text(val.title));
                }); 

                $('#finalResult').fadeOut('slow', function() {
                $(this).append(str).fadeIn('slow').fadeIn(3000);
                $('#finalResult').css({backgroundColor: ''});
             $('#finalResult').append.apply($('#finalResult'),items);
                }).css({backgroundColor: '#D4ED91'});

            }catch(e) {     
                alert('Exception while request..');
            }       
        },
        error: function(){                      
            alert('Error while request..');
        }
     });


}

});
</script>
</head>
<body>      
<h1>Load data on page scroll using jQuery Php Codeigniter and Mysql  </h1>  
<ul id="finalResult">
</ul>   
</body>
</html>

I am completely new to json. So please tell me where am i getting wrong. I also want to know how is the connection to database made in json. I mean in the example on the site i mentioned, i could not understand how was the connection made to database.

  • 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-17T16:32:16+00:00Added an answer on June 17, 2026 at 4:32 pm

    Save this in a file. Call it whatever, dbCalls.php for simplicity

    class db{
        protected $db = NULL;
    
        public function __construct(){
            $this->db = new mysqli('localhost', 'username', 'password', 'database');
        }
    
        public function getPhotos(){
            $stmt  = $this->db->query('SELECT Photo_Id, url, title FROM photos');
    
            $rows = array();
            while($row = $stmt->fetch_assoc()):
                $rows[] = $row;
            endwhile;   
    
            return json_encode($rows);
        }
    }
    

    Now make a new file. controller.php.

    include_once('dbCalls.php');
    $db = new db; //instantiate class
    
    if(!empty($_GET['getPhotos'])):
        echo $db->getPhotos(); //send the json encoded photos back
    endif;
    

    Now change your ajax function like so.

    $.ajax({
        type: "GET",
        url: "http://localhost/test/controller.php",
        cache: false,               
        data:'getPhotos=1',
        dataType:'json',
        success:function(json){
            $.each(json, function(i,row){
                //assuming your rows were just simple rows, and you used the above code..
                console.log(row.Photo_Id, row.url, row.title);
            });
        }
    }); 
    

    You can substite that call to the console with whatever jQuery functions you want to use to manipulate the response.

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

Sidebar

Related Questions

i have got <div><a class='link' href='index.php' data-container='target'>Load</a></div> <div class='target'></div> i need to write jquery
Hello i need to create a progressView when i load data from my webservice.
I need to load a lot of data fetched from a mysql db in
Need to load data from a single file with a 100,000+ records into multiple
Need to load data from a single file with a 100,000+ records into multiple
I'm in a somehow tough situation here. I need to load data from multiple
I have a two dimensional array that I need to load data into. I
I need to load data from source table to Flat file detination(txt). For example
I need to load data from different remote database into our own database. I
I need to load data into my treestore. My ajax request give me XML

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.