Tag Archives: json

jQuery Communicator Singleton (Manage & Cache ajax requests)

Written by Garry Lachman (Admin). Filed under JavaScript, jQuery. Tagged , , , , , , , , , , , , , . .

Hey,
I wrote this class for cache ajax request if the same request sent twice.
The second reason why i use this class is a like there is only one exit point
from the application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var Communicator =(function(){
    var instantiated;
    var retry = new Array();
    var cached_data = new Array();

    function init (){
        return {
            dispatch:function(url, callback){
                var self = this;
                if (Communicator.getInstance().cached_data[url])    {
                    callback(Communicator.getInstance().cached_data[url]);
                    return;
                }
                if (!retry[url])    {
                    retry[url] = 0;
                }
                var _rq = $.ajax({
                    url: url,
                    timeout: 30000,
                    success: function(_data){
                        Communicator.getInstance().cached_data[url] = _data;
                        callback(_data);
                    },
                    error: function(){
                        if (retry[url] < 3) {
                            retry[url]++;
                            dispatch(url, callback, retry[url]);
                        }   else    {
                            callback("");  
                        }
                    }
                });
            },
            cached_data: {}
        }
    }

    return {
        getInstance :function(){
            if (!instantiated){
                instantiated = init();
            }
            return instantiated;
        }
    }
})()

Using the class

1
2
3
4
5
6
7
8
9
10
11
12
Communicator.getInstance().dispatch(url,function(data){
    var error = false;
    try {
            data = eval("("+data+")");
    } catch(err)    {
        error = true;
    }

    if (!error)    {
        console.log(data);
    }                    
});

Have fun :)
Garry

Share

read POST variables with Javascript using PHP

Written by Garry Lachman (Admin). Filed under JavaScript, PHP + mySQL. Tagged , , , , , , . .

Hi,
Today i will show very easy way to read POST variables from Javascript using PHP.

1
2
3
<script type="text/javascript">// <![CDATA[
var post_json = "<?php echo json_encode($_POST); ?>";
// ]]></script>

We encode all POST vars with php to javascript JSON.
Now we can access the vars in simple way:

1
post_json.var_name;

Have a nice day,
Garry

Share

mySQL to JSON PHP Class

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , . .

Hi,
A little class the i found here.
The class is very useful to generate json from mySQL result object.

The class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class mysql2json{
     static public function getJSON($resultSet,$affectedRecords){
        mb_internal_encoding("UTF-8");
         $numberRows=0;
         $arrfieldName=array();
         $i=0;
         $json="";
        //print("Test");
         while ($i < mysql_num_fields($resultSet))  {
             $meta = mysql_fetch_field($resultSet, $i);
            if (!$meta) {
            }else{
            $arrfieldName[$i]=$meta->name;
            }
            $i++;
         }
         $i=0;
          $json="{\n\"data\": [\n";
        while($row=mysql_fetch_array($resultSet, MYSQL_NUM)) {
            $i++;
            //print("Ind ".$i."-$affectedRecords<br>");
            $json.="{\n";
            for($r=0;$r < count($arrfieldName);$r++) {
                $json.="\"$arrfieldName[$r]\" :    \"$row[$r]\"";
                if($r < count($arrfieldName)-1){
                    $json.=",\n";
                }else{
                    $json.="\n";
                }
            }
             
             if($i!=$affectedRecords){
                 $json.="\n},\n";
             }else{
                 $json.="\n}\n";
             }
             
        }
        $json.="]\n};";  
            return $json;
     }
}

Usage:

1
2
3
4
5
6
7
8
require_once("mysql2json.class.php");
mb_internal_encoding("UTF-8");

$result = mysql_query("select * from users");
$count=mysql_affected_rows();

$objJSON=new mysql2json();
print(trim($objJSON->getJSON($result,$count)));

Have a nice day,
Garry Lachman

Share