By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

How to handle json data in zend while using flexigrid

pradeepjain
100+
P: 563
i have a page post2.php which prints the json array like

Expand|Select|Wrap|Line Numbers
  1. {
  2. page: 1,
  3. total: 239,
  4. rows: [
  5. {id:'239',cell:['239','ZW','ZIMBABWE','Zimbabwe','ZWE','716']},
  6. {id:'238',cell:['238','ZM','ZAMBIA','Zambia','ZMB','894']},
  7. {id:'237',cell:['237','YE','YEMEN','Yemen','YEM','887']},
  8. {id:'236',cell:['236','EH','WESTERN SAHARA','Western Sahara','ESH','732']},
  9. {id:'235',cell:['235','WF','WALLIS AND FUTUNA','Wallis and Futuna','WLF','876']},
  10. {id:'234',cell:['234','VI','VIRGIN ISLANDS, U.S.','Virgin Islands, U.s.','VIR','850']},
  11. {id:'233',cell:['233','VG','VIRGIN ISLANDS, BRITISH','Virgin Islands, British','VGB','92']},
  12. {id:'232',cell:['232','VN','VIET NAM','Viet Nam','VNM','704']},
  13. {id:'231',cell:['231','VE','VENEZUELA','Venezuela','VEN','862']},
  14. {id:'230',cell:['230','VU','VANUATU','Vanuatu','VUT','548']}]
  15. }
and here is the post2.php code

Expand|Select|Wrap|Line Numbers
  1. <?
  2. error_reporting(1);
  3. function runSQL($rsql) {
  4.         $hostname = "localhost";
  5.         $username = "root";
  6.         $password = "root";
  7.         $dbname   = "hms1";
  8.         $connect = mysql_connect($hostname,$username,$password) or die ("Error: could not connect to database");
  9.         $db = mysql_select_db($dbname);
  10.         $result = mysql_query($rsql) or die ('test');
  11.         return $result;
  12.         mysql_close($connect);
  13. }
  14.  
  15. function countRec($fname,$tname,$where) {
  16. $sql = "SELECT count($fname) FROM $tname $where";
  17. $result = runSQL($sql);
  18. while ($row = mysql_fetch_array($result)) {
  19. return $row[0];
  20. }
  21. }
  22. $page = $_POST['page'];
  23. $rp = $_POST['rp'];
  24. $sortname = $_POST['sortname'];
  25. $sortorder = $_POST['sortorder'];
  26.  
  27. if (!$sortname) $sortname = 'pa_name';
  28. if (!$sortorder) $sortorder = 'desc';
  29.                 if($_POST['query']!=''){
  30.                         $where = "WHERE `".$_POST['qtype']."` LIKE '%".$_POST['query']."%' ";
  31.                 } else {
  32.                         $where ='';
  33.                 }
  34.                 if($_POST['letter_pressed']!=''){
  35.                         $where = "WHERE `".$_POST['qtype']."` LIKE '".$_POST['letter_pressed']."%' ";
  36.                 }
  37.                 if($_POST['letter_pressed']=='#'){
  38.                         $where = "WHERE `".$_POST['qtype']."` REGEXP '[[:digit:]]' ";
  39.                 }
  40. $sort = "ORDER BY $sortname $sortorder";
  41.  
  42. if (!$page) $page = 1;
  43. if (!$rp) $rp = 10;
  44.  
  45. $start = (($page-1) * $rp);
  46.  
  47. $limit = "LIMIT $start, $rp";
  48. $sql = "SELECT pa_id,pa_name,pa_pd_patient_id,pa_name FROM patient_appointment $where $sort $limit";
  49. $result = runSQL($sql);
  50.  
  51. $total = countRec('pa_id','patient_appointment',$where);
  52.  
  53. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
  54. header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
  55. header("Cache-Control: no-cache, must-revalidate" );
  56. header("Pragma: no-cache" );
  57. header("Content-type: text/x-json");
  58. $json = "";
  59. $json .= "{\n";
  60. $json .= "page: $page,\n";
  61. $json .= "total: $total,\n";
  62. $json .= "rows: [";
  63. $rc = false;
  64. while ($row = mysql_fetch_array($result)) {
  65. if ($rc) $json .= ",";
  66. $json .= "\n{";
  67. /*$json .= "cell:['".$row['pa_name']."'";
  68. $json .= ",'".addslashes($row['time'])."'";
  69. $json .= ",'".addslashes($row['pa_um_id'])."'";
  70. $json .= ",'".addslashes($row['pa_pd_patient_id'])."']";*/
  71. $json .= "id:'".$row['pa_id']."',";
  72. $json .= "cell:['".$row['pa_id']."','".$row['pa_name']."'";
  73. $json .= ",'".addslashes($row['pa_pd_patient_id'])."']";
  74.  
  75. $json .= "}";
  76. $rc = true;
  77. }
  78. $json .= "]\n";
  79. $json .= "}";
  80. echo $json;
  81. ?>
  82.  
and in zend views i am trying to use flexigrid like this

Expand|Select|Wrap|Line Numbers
  1. $(document).ready(function(){
  2.  
  3.         $("#flex1").flexigrid
  4.                         (
  5.                         {
  6.                         url: '/public/**post2.ph**p',
  7.                         dataType: 'json',
  8.                         colModel : [
  9.                                 {display: 'ID', name : 'id', width : 40, sortable : true, align: 'center'},
  10.                                 {display: 'ISO', name : 'iso', width : 40, sortable : true, align: 'center'},
  11.                                 {display: 'Name', name : 'name', width : 180, sortable : true, align: 'left'},
  12.                                 {display: 'Printable Name', name : 'printable_name', width : 120, sortable : true, align: 'left'},
  13.                                 {display: 'ISO3', name : 'iso3', width : 130, sortable : true, align: 'left', hide: true},
  14.                                 {display: 'Number Code', name : 'numcode', width : 80, sortable : true, align: 'right'}
  15.                                 ],
  16.                         buttons : [
  17.                                 {name: 'Add', bclass: 'add', onpress : test},
  18.                                 {name: 'Delete', bclass: 'delete', onpress : test},
  19.                                 {separator: true},
  20.                                 {name: 'A', onpress: sortAlpha},
  21.                                 ],
  22.                         searchitems : [
  23.                                 {display: 'ISO', name : 'iso'},
  24.                                 {display: 'Name', name : 'name', isdefault: true}
  25.                                 ],
  26.                         sortname: "id"
  27.                         sortorder: "asc",
  28.                         usepager: true,
  29.                         title: 'Countries',
  30.                         useRp: true,
  31.                         rp: 10,
  32.                         showTableToggleBtn: true,
  33.                         width: 700,
  34.                         height: 255
  35.                         }
  36.                         );  
  37.  
  38. });
  39.  
  40.  
this prints the flexigrid layout well.. but never gets the data.it keeps telling processing. is there some problem in the json array or in the flexigrid calling... or in zend with json.

i tried out like this also in the views page and the 'hai' is also not getting alerted .

Expand|Select|Wrap|Line Numbers
  1. $.post("/public/server_processing1.php",{},function(data){
  2. alert('hai');
  3. alert(data);
  4. }, "json");
  5.  

is there a way to handle json data in zend .
May 19 '10 #1
Share this Question
Share on Google+
2 Replies


Markus
Expert 5K+
P: 6,050
I imagine you should take this up with the developer of the JS library - this isn't a PHP issue.
May 19 '10 #2

pradeepjain
100+
P: 563
@Markus
i run the code outside zend . it works well. but inside zend it does not.so asked here
May 20 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.