2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
13 // my simple JSON encode/decode function, similar to PHP5.2
14 function json_encode_result($phpdata) {
15 if (!$phpdata) return "[]";
17 if(is_array($phpdata))
18 return php2js_array($phpdata);
20 return php2js_object($phpdata);
24 //function json_decode($jsonata) {
29 // convert a PHP object to javascript object
30 function php2js_object($phpobj) {
33 if (!is_array($phpobj)) return "[]";
35 foreach($phpobj as $col => $val) {
37 $str = $col .":'" . escapeString($val) . "'";
39 $str = $str . "," . $col .":'" . escapeString($val) . "'";
42 return "{" . $str . "}";
45 // convert a PHP object to javascript object
46 function php2js_array($phparr) {
49 if (!is_array($phparr)) return "[]";
51 foreach ($phparr as $e) {
53 $str = php2js_object($e) ;
55 $str = $str . "," . php2js_object($e);
58 return "[" . $str . "]";
61 function escapeString($string) {
69 return str_replace(array_keys($escape), array_values($escape), addslashes($string));