Codeigniter Ajax Load more data when page scroll tutorial; Throughout this guide, you will find out how to easily integrate load more data on page or when window scroll with the help of jquery Ajax in Codeigniter. To up the user interface we will use the dynamic bootstrap CSS framework.
User experience is the top priority of the web applications; theoretically, to make the ux , we try to implement various variations in the ui.
The most famous ui strategy is to display data on or load more data on page scroll; the great thing about load more data is you don’t bother to reload the entire web page. This Codeigniter jQuery ajax load more data tutorial will explain how to show more data or load data when page scroll and display in the view.
Facebook, Twitter, Instagram and other innumerable software applications where you can load data on a scroll or scroll up for the infinite times; this way, the user engagement increases & makes the user love the application.
How to Load Data on Page Scroll in CodeIgniter 4 using Ajax jQuery
- Step 1: Install New Codeigniter App
- Step 2: Commission Error Handling
- Step 3: Create Table and Insert Data
- Step 3: Connect to Database
- Step 4: Set Up Model
- Step 5: Create Ajax Load More Controller
- Step 6: Define Route
- Step 7: Set Up Load More Views
- Step 8: Start CI Project
Install New Codeigniter App
If you have composer set up on your system then execute the create-project command to install the archetype of the CodeIgniter app.
Change the appllication folder name as per your choice.
There is another easy , fast method, and you can download the Codeigniter directly from the official website.
Commission Error Handling
While developing an app, seldom we need to debug the errors. Consequently you have to enable the display_errors and get inside the app/Config/Boot/development.php likewise app/Config/Boot/production.php file and set the display_errors
prop to 1 from 0.
Create Table and Insert Data
To load the data when scroll requires to have the database set up, the table created with the test data if you haven’t done this then create the database, create the new table and test data into it, run SQL command from the PHPMyAdmin.
Connect to Database
Open app/Config/Database.php file and then inside the public default array, include the database name, username, and password for connecting the CI app to the MySQL database.
CodeIgniter\Database\Exceptions\DatabaseException
Sometimes this error Unable to connect database : Codeigniter error might manifest on the app screen. Preferably if you are using either WAMP ,MAMP or XAMP local servers. In that case, you may replace the hostname property based on the server that you are using.
Set Up Model
In this section get into the app/Models folder and create the User.php file. After that, open the app/Models/User.php file and then add the following code and save the file.
Create Ajax Load More Controller
This step commands you to get to the app/Controllers folder, and then create AjaxLoadMore.php file, place the suggested code inside the app/Controllers/AjaxLoadMore.php file.
In this file, we are getting the users’ data page wise from the database, and we will use ajax to make the request and display it when page scroll.
Define Route
In this step, we have to define the new route for displaying the view file with the load more data. Open and update the code inside this app/Config/Routes.php file.
Set Up Load More Views
Ultimately, in the final step, we have to get into the app/Views folder and then create two files (index.php and load_more.php); one file will hold the loaded data, and another is the archetype where we get the users data and display it on the view and also load more data when page scroll.
Update app/Views/index.php file:
Update app/Views/load_more.php file:
Start CI Project
Next, use the php spark serve command to serve the Codeigniter development server:
Open the browser and run the following url to test the app.
Conclusion
So, this was it, the Codeigniter ajax load more example is over; we hope you have comprehended the whole process and understood how to load more data without the page refresh in the Codeigniter app.