/demo/post-xml.php

https://github.com/bobzhai/Flexigrid · PHP · 112 lines · 45 code · 3 blank · 64 comment · 6 complexity · b35a8e5110b23e2a61343042007db5ac MD5 · raw file

  1. <?php
  2. $page = isset($_POST['page']) ? $_POST['page'] : 1;
  3. $rp = isset($_POST['rp']) ? $_POST['rp'] : 10;
  4. $sortname = isset($_POST['sortname']) ? $_POST['sortname'] : 'name';
  5. $sortorder = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'desc';
  6. $query = isset($_POST['query']) ? $_POST['query'] : false;
  7. $qtype = isset($_POST['qtype']) ? $_POST['qtype'] : false;
  8. /* -- To use the SQL, remove this block
  9. $usingSQL = true;
  10. function runSQL($rsql) {
  11. $db['default']['hostname'] = "localhost";
  12. $db['default']['username'] = '';
  13. $db['default']['password'] = "";
  14. $db['default']['database'] = "";
  15. $db['live']['hostname'] = 'localhost';
  16. $db['live']['username'] = '';
  17. $db['live']['password'] = '';
  18. $db['live']['database'] = '';
  19. $active_group = 'default';
  20. $base_url = "http://".$_SERVER['HTTP_HOST'];
  21. $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
  22. $connect = mysql_connect($db[$active_group]['hostname'],$db[$active_group]['username'],$db[$active_group]['password']) or die ("Error: could not connect to database");
  23. $db = mysql_select_db($db[$active_group]['database']);
  24. $result = mysql_query($rsql) or die ('test');
  25. return $result;
  26. mysql_close($connect);
  27. }
  28. function countRec($fname,$tname) {
  29. $sql = "SELECT count($fname) FROM $tname ";
  30. $result = runSQL($sql);
  31. while ($row = mysql_fetch_array($result)) {
  32. return $row[0];
  33. }
  34. }
  35. $page = $_POST['page'];
  36. $rp = $_POST['rp'];
  37. $sortname = $_POST['sortname'];
  38. $sortorder = $_POST['sortorder'];
  39. if (!$sortname) $sortname = 'name';
  40. if (!$sortorder) $sortorder = 'desc';
  41. $sort = "ORDER BY $sortname $sortorder";
  42. if (!$page) $page = 1;
  43. if (!$rp) $rp = 10;
  44. $start = (($page-1) * $rp);
  45. $limit = "LIMIT $start, $rp";
  46. $where = "";
  47. if ($query) $where = " WHERE $qtype LIKE '%".mysql_real_escape_string($query)."%' ";
  48. $sql = "SELECT iso,name,printable_name,iso3,numcode FROM country $where $sort $limit";
  49. $result = runSQL($sql);
  50. $total = countRec('iso','country');
  51. $rows = array();
  52. while ($row = mysql_fetch_array($result)) {
  53. $rows[] = $row;
  54. }
  55. */
  56. if(!isset($usingSQL)){
  57. include dirname(__FILE__).'/countryArray.inc.php';
  58. if($qtype && $query){
  59. $query = strtolower(trim($query));
  60. foreach($rows AS $key => $row){
  61. if(strpos(strtolower($row[$qtype]),$query) === false){
  62. unset($rows[$key]);
  63. }
  64. }
  65. }
  66. //Make PHP handle the sorting
  67. $sortArray = array();
  68. foreach($rows AS $key => $row){
  69. $sortArray[$key] = $row[$sortname];
  70. }
  71. $sortMethod = SORT_ASC;
  72. if($sortorder == 'desc'){
  73. $sortMethod = SORT_DESC;
  74. }
  75. array_multisort($sortArray, $sortMethod, $rows);
  76. $total = count($rows);
  77. $rows = array_slice($rows,($page-1)*$rp,$rp);
  78. }
  79. header("Content-type: text/xml");
  80. $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  81. $xml .= "<rows>";
  82. $xml .= "<page>$page</page>";
  83. $xml .= "<total>$total</total>";
  84. foreach($rows AS $row){
  85. $xml .= "<row id='".$row['iso']."'>";
  86. $xml .= "<cell><![CDATA[".$row['iso']."]]></cell>";
  87. $xml .= "<cell><![CDATA[".utf8_encode($row['name'])."]]></cell>";
  88. //$xml .= "<cell><![CDATA[".print_r($_POST,true)."]]></cell>";
  89. $xml .= "<cell><![CDATA[".utf8_encode($row['printable_name'])."]]></cell>";
  90. $xml .= "<cell><![CDATA[".utf8_encode($row['iso3'])."]]></cell>";
  91. $xml .= "<cell><![CDATA[".utf8_encode($row['numcode'])."]]></cell>";
  92. $xml .= "</row>";
  93. }
  94. $xml .= "</rows>";
  95. echo $xml;