Guest User

Untitled

a guest
Aug 16th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. jqGrid search data returned from php file
  2. <?php
  3. $page = $_GET['page']; // get the requested page
  4. $limit = $_GET['rows']; // get how many rows we want to have into the grid
  5. $sidx = $_GET['sidx']; // get index row - i.e. user click to sort
  6. $sord = $_GET['sord']; // get the direction
  7. if(!$sidx) $sidx =1;
  8.  
  9. //array to translate the search type
  10. $ops = array(
  11. 'eq'=>'=', //equal
  12. 'ne'=>'<>',//not equal
  13. 'lt'=>'<', //less than
  14. 'le'=>'<=',//less than or equal
  15. 'gt'=>'>', //greater than
  16. 'ge'=>'>=',//greater than or equal
  17. 'bw'=>'LIKE', //begins with
  18. 'bn'=>'NOT LIKE', //doesn't begin with
  19. 'in'=>'LIKE', //is in
  20. 'ni'=>'NOT LIKE', //is not in
  21. 'ew'=>'LIKE', //ends with
  22. 'en'=>'NOT LIKE', //doesn't end with
  23. 'cn'=>'LIKE', // contains
  24. 'nc'=>'NOT LIKE' //doesn't contain
  25. );
  26. function getWhereClause($col, $oper, $val){
  27. global $ops;
  28. if($oper == 'bw' || $oper == 'bn') $val .= '%';
  29. if($oper == 'ew' || $oper == 'en' ) $val = '%'.$val;
  30. if($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') $val = '%'.$val.'%';
  31. return " WHERE $col {$ops[$oper]} '$val' ";
  32. }
  33. $where = ""; //if there is no search request sent by jqgrid, $where should be empty
  34. $searchField = isset($_GET['searchField']) ? $_GET['searchField'] : false;
  35. $searchOper = isset($_GET['searchOper']) ? $_GET['searchOper']: false;
  36. $searchString = isset($_GET['searchString']) ? $_GET['searchString'] : false;
  37. if ($_GET['_search'] == 'true') {
  38. $where = getWhereClause($searchField,$searchOper,$searchString);
  39. }
  40.  
  41. // connect to the database
  42. $dbhost = "host_address";
  43. $dbuser = "db_user";
  44. $dbpassword = "db_pass";
  45. $database = "db_name";
  46. $tablename
  47. $db = mysql_connect($dbhost, $dbuser, $dbpassword)
  48. or die("Connection Error: " . mysql_error());
  49.  
  50. mysql_select_db($database) or die("Error conecting to db.");
  51. mysql_set_charset('utf8',$database);
  52. mysql_query("SET NAMES 'utf8'");
  53. $result = mysql_query("SELECT COUNT(*) AS count FROM $tablename");
  54. $row = mysql_fetch_array($result,MYSQL_ASSOC);
  55. $count = $row['count'];
  56.  
  57. if( $count >0 ) {
  58. $total_pages = ceil($count/$limit);
  59. } else {
  60. $total_pages = 0;
  61. }
  62. if ($page > $total_pages) $page=$total_pages;
  63. $start = $limit*$page - $limit; // do not put $limit*($page - 1)
  64. $SQL = "SELECT field1, field2, field3, field4, field5 FROM $tablename "
  65. .$where." ORDER BY $sidx $sord LIMIT $start , $limit";
  66. $result = mysql_query( $SQL ) or die("Couldn?t execute query.".mysql_error());
  67.  
  68. if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
  69. header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
  70. header("Content-type: text/xml;charset=utf-8");
  71. }
  72. $et = ">";
  73.  
  74. echo "<?xml version='1.0' encoding='utf-8'?$etn";
  75. echo "<rows>";
  76. echo "<page>".$page."</page>";
  77. echo "<total>".$total_pages."</total>";
  78. echo "<records>".$count."</records>";
  79. // be sure to put text data in CDATA
  80. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  81. echo "<row id='". $row[field1]."'>";
  82. echo "<cell>". $row[field1]."</cell>";
  83. echo "<cell>". $row[field2]."</cell>";
  84. echo "<cell><![CDATA[". $row[field3]."]]></cell>";
  85. echo "<cell>". $row[field4]."</cell>";
  86. echo "<cell>". $row[field5]."</cell>";
  87. echo "</row>";
  88. }
  89. echo "</rows>";
  90. ?>
Add Comment
Please, Sign In to add comment