Advertisement
Virajsinh

Select2 With Pagination Using CodeIgniter

Feb 11th, 2020 (edited)
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. <?php
  2.     //http://embed.plnkr.co/db8SXs/preview
  3.     include_once ("connect.php");
  4.     $searchTerm = (isset($_REQUEST['searchTerm']))?$_REQUEST['searchTerm']:'';
  5.     $page = (isset($_REQUEST['page']))?$_REQUEST['page']:'';
  6.     $country_id = (isset($_POST['country_id']))?$_POST['country_id']:'';
  7.  
  8.     $resultCount = 10;
  9.     $end = ($page - 1) * $resultCount;
  10.     $start = $end + $resultCount;
  11.  
  12.     $sState = "SELECT * FROM tbl_state_master WHERE country_id = ".$country_id." AND name LIKE '%".$searchTerm."%' ORDER BY name ASC LIMIT ".$end.",".$start."";
  13.  
  14.     $sResult = mysqli_query($conn, $sState);
  15.     $count = mysqli_num_rows($sResult);
  16.     $json = array();
  17.  
  18.     foreach ($sResult as $row) {
  19.         $json[] = array('id' => $row['id'], 'text' => $row['name'], 'total_count'=> $count);
  20.     }
  21.  
  22.     if (empty($json)){
  23.         $empty[] = ['id'=>'', 'text'=>'', 'total_count'=>''];
  24.         echo json_encode($empty);
  25.     }else{
  26.         echo json_encode($json);
  27.     }
  28. ?>
  29.  
  30.  
  31. <script type="text/javascript">
  32.  
  33.                 //Select2 jQuery
  34.                 $("#state_id").select2("trigger", "select", {
  35.                     data: { id: select_id}
  36.                 });
  37.  
  38.  
  39.                 $("#state_id").select2({
  40.                     ajax: {
  41.                         url: 'json_state_search.php',
  42.                         type: "post",
  43.                         dataType: 'json',
  44.                         delay:250,
  45.                         data: function (params) {
  46.                             return {                               
  47.                                 searchTerm: params.term || "",
  48.                                 page: params.page ||1,
  49.                                 country_id: $("#country_id").val()
  50.                             }
  51.                         },
  52.                         processResults: function (data, params) {
  53.                         // Transforms the top-level key of the response object from 'items' to 'results'
  54.                         page = params.page || 1;
  55.                           return {
  56.                             results: $.map(data, function (item) { return {id: item.id, text: item.text}}),
  57.                             pagination: {
  58.                                 more: (page * 10) <= data[0].total_count
  59.                             }
  60.                           };
  61.                         },
  62.                         cache:false,
  63.                     }
  64.                 });
  65. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement