In this tutorial we explain how to create advanced pagination in PHP and MySql with jQuery in codeigniter. It is a MVC structure framework, so we explain each and every thing as per structure.
Steps to Create AJAX Pagination in Codeigniter
Step 1:
Lets take an example to describe you in detail. Suppose on view page you will showing 3 property listing and below Load More button to show more listing on button click.
So whenever you click that button property list increase by 3. Means first time you click load more button 6 property show than after 9 and more on.
Use foreach loop to show first 3 property on view page. Declare one $count variable outside the for loop and initialize its value by 1. Assign this count variable to another $newproperty variable. After one loop execute increment $count variable to 1. This $count variable store your last showing property id on page. See below code example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$count=1; foreach($package_data as $row) { $newproperty = $count; <div class="col-md-4"> // Show Here first Three property list </div> <?php $count++; } ?> |
Now check if your property list count is greater than $newproperty value, than display load more button. This condition because to check that you have more than 3 property to show on page.
Step 2:
Check below load more button functionality.
Declare $newproperty, $proerty_name, $count and $property_rowcount as hidden input value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php if($property_rowcount > $newproperty) { ?> <div class="show_more_main" id="show_more_main<?php echo $newproperty; ?>"> <input type="hidden" value="<?php echo $newproperty; ?>" id="tutidval" > <input type="hidden" value="<?php echo $property_name; ?>" id="cntryname" > <input type="hidden" value="<?php echo $count; ?>" id="prevval" > <!--input type="hidden" value="<?php //echo $property_rowcount; ?>" id="rowcount" --> <a class="uppercase full-width button btn-large" href="javascript:void(0);" id="show_more">load more listing</a> <span class="loding" style="display: none;"><span class="loding_txt">Loading...</span></span> </div> <?php } ?> |
Step 3:
Now define the ajax functionality for load more on same view page. This ajax function called on the basis of “show_more” id of load more listing text.
In this script we can get last property showing id by #tutidval and count variable by #prevval and property name by #cntryname.
You May Also Like:
How to Install Magento Security Patch via Putty
Code to Use Paypal Payment Gateway on Website
How to Authenticate with D&B Direct API Web Service
How to Add New Product in Bigcommerce via API V2
How to Connect With Cloudshare Rest API
How to Use Google API to Get Google Analytics Data with PHP
Note: We use property name for search the property list from the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// Ajax code <script type="text/javascript"> $(document).ready(function(){ $(document).on('click','#show_more',function(){ var ID = $("#tutidval").val(); var cntname = $("#cntryname").val(); var prevval = $("#prevval").val(); //alert(ID); $('#show_more').hide(); $('.loding').show(); $.ajax({ type:'POST', url:'<?php echo $current_url.'loadmoredata';?>', data:'searchid='+ID + '&cntrynm=' + cntname+ '&prevval='+prevval, success:function(html){ //alert("i am here"); $('#show_more_main'+ID).remove(); $('.hot-deals-list').append(html); } }); }); }); </script> |
Step 4:
Now we define “loadmoredata” function in SITE controller of codeignitor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public function loadmoredata() { $data=$this->site_model->Header(); // Get the ajax post variabel value here $searchquery=$this->input->post('searchid'); $data['prevval']=$this->input->post('prevval'); $cntrynm=$this->input->post('cntrynm'); // Define site model function to get next 3 property list to show on page. $data['property_data']=$this->site_model->loadmore_listing($cntrynm,$searchquery); $packagerowcount=$this->site_model->search_querypackage_count($cntrynm); $data['country_name']=$cntrynm; $data['property_rowcount']=count($packagerowcount); $this->load->view('front/loadmorelist',$data); } |
Step 5:
Now define “loadmore_listing” Model function to get next 3 property list from database. Also get total property count entry in database table using another “search_querypackage_count” function in model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public function loadmore_listing($cntrynm,$searchquery) { $this->load->database(); $query = $this->db->query("SELECT * FROM Property Tbale Name WHERE poperty_name = '$cntrynm' AND property_status = '1' LIMIT $searchquery , 3"); return $query->result(); } public function search_querypackage_count($searchquery) { $this->load->database(); $query = $this->db->query("SELECT * FROM property table name WHERE package_name = '$searchquery' AND property_status='1'"); return $query->result(); } |
Step 6:
Now create an another “loadmorelist” page in View section to show load more property content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php // Now previpus property count value store in $count variable. Means it take value 4. $count=$prevval; foreach($package_data as $row) { $newproperty = $r; ?> <div class="col-md-4"> </div> <?php $count++; } ?> // Again mentioned the load more section for futher load more property. <?php if($property_rowcount > $newproperty) { ?> <div class="show_more_main" id="show_more_main<?php echo $newproperty; ?>"> <input type="hidden" value="<?php echo $newproperty; ?>" id="tutidval" > <input type="hidden" value="<?php echo $property_name; ?>" id="cntryname" > <input type="hidden" value="<?php echo $count; ?>" id="prevval" > <!--input type="hidden" value="<?php //echo $property_rowcount; ?>" id="rowcount" --> <a class="uppercase full-width button btn-large" href="javascript:void(0);" id="show_more">load more listing</a> <span class="loding" style="display: none;"><span class="loding_txt">Loading...</span></span> </div> <?php }?> |
Through follow above complete process you can easily implement Pagination Load More functionality on your codeigniter website. make sure use right code on correct web page. This is the Advanced Pagination in PHP in Mysql.
Note: Above define variable change as per your website and database table structure. So change it as per your need.
Really appreciate this post…