Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //http://embed.plnkr.co/db8SXs/preview
- include_once ("connect.php");
- $searchTerm = (isset($_REQUEST['searchTerm']))?$_REQUEST['searchTerm']:'';
- $page = (isset($_REQUEST['page']))?$_REQUEST['page']:'';
- $country_id = (isset($_POST['country_id']))?$_POST['country_id']:'';
- $resultCount = 10;
- $end = ($page - 1) * $resultCount;
- $start = $end + $resultCount;
- $sState = "SELECT * FROM tbl_state_master WHERE country_id = ".$country_id." AND name LIKE '%".$searchTerm."%' ORDER BY name ASC LIMIT ".$end.",".$start."";
- $sResult = mysqli_query($conn, $sState);
- $count = mysqli_num_rows($sResult);
- $json = array();
- foreach ($sResult as $row) {
- $json[] = array('id' => $row['id'], 'text' => $row['name'], 'total_count'=> $count);
- }
- if (empty($json)){
- $empty[] = ['id'=>'', 'text'=>'', 'total_count'=>''];
- echo json_encode($empty);
- }else{
- echo json_encode($json);
- }
- ?>
- <script type="text/javascript">
- //Select2 jQuery
- $("#state_id").select2("trigger", "select", {
- data: { id: select_id}
- });
- $("#state_id").select2({
- ajax: {
- url: 'json_state_search.php',
- type: "post",
- dataType: 'json',
- delay:250,
- data: function (params) {
- return {
- searchTerm: params.term || "",
- page: params.page ||1,
- country_id: $("#country_id").val()
- }
- },
- processResults: function (data, params) {
- // Transforms the top-level key of the response object from 'items' to 'results'
- page = params.page || 1;
- return {
- results: $.map(data, function (item) { return {id: item.id, text: item.text}}),
- pagination: {
- more: (page * 10) <= data[0].total_count
- }
- };
- },
- cache:false,
- }
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement