2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
9 /* This program is free software. You can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /************************************************************************/
14 // my simple JSON encode/decode function, similar to PHP5.2
15 function json_encode_result($phpdata) {
16 if(gettype($phpdata) == "resource")
17 return php2js_sqlresult($phpdata);
18 else if(is_array($phpdata))
19 return php2js_array($phpdata);
21 return php2js_object($phpdata);
25 //function json_decode($jsonata) {
30 // convert a PHP object to javascript object
31 function php2js_object($phpobj) {
34 if (!is_array($phpobj)) return "[]";
36 foreach($phpobj as $col => $val) {
38 $str = $col .":'" . escapeString($val) . "'";
40 $str = $str . "," . $col .":'" . escapeString($val) . "'";
43 return "{" . $str . "}";
46 // convert a PHP object to javascript object
47 function php2js_array($phparr) {
50 if (!is_array($phparr)) return "[]";
52 foreach ($phparr as $e) {
54 $str = php2js_object($e) ;
56 $str = $str . "," . php2js_object($e);
59 return "[" . $str . "]";
62 // convert a SQL result object to javascript object
63 function php2js_sqlresult($phpsql) {
66 while ($line = mysql_fetch_assoc($phpsql)) {
69 mysql_free_result($phpsql);
70 return php2js_array($rows);
73 function escapeString($string) {
81 return str_replace(array_keys($escape), array_values($escape), addslashes($string));