Tag Archives: snippet

Using Curl with APC

Function to do a curl request or grab results from apc.

Useful when requesting a source that takes to long to grab and does not change very often.

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
function curl_get_cache($url, $use_cache = false, $cache_ttl = 86400)
{
    if (true == $use_cache)
    {
        $apc_key = 'curl_' . md5($url);
        $apc_fetch = apc_fetch($apc_key);
       
        if ($apc_fetch)
        {
            return $apc_fetch;
        }
    }
   
    $curl_handle = curl_init();

    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_HEADER, 0);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT_MS, 5000);
    curl_setopt($curl_handle, CURLOPT_TIMEOUT_MS, 5000);

    $result = curl_exec($curl_handle);
   
    if (true == $use_cache)
    {
        apc_store($apc_key, $result, $cache_ttl);
    }

    curl_close($curl_handle);
   
    return $result;
}

The missing php function: mysql_fetch_all

The function:

1
2
3
4
5
6
7
8
function mysql_fetch_all($query, $kind = 'assoc')
{
    $result = array();
    $kind = $kind === 'assoc' ? $kind : 'row';
    eval('while(@$r = mysql_fetch_' . $kind . '($query)) array_push($result, $r);');
    return $result;

}

Usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$query = "SELECT * FROM my_table;"
$res = mysql_query($query, $mysqll);
$res_array = mysql_fetch_all($res);

print_r($res_array);

/*
Output:
Array
(
    [0] => Array
        (
            [col_A] => val_A1
            [col_b] => val_B1
        )
    [1] => Array
        (
            [col_A] => val_A2
            [col_b] => val_B2
        )
* /

Check element visibility with Javascript

The following function checks if a given element is within browser’s viewport:

1
2
3
4
5
6
7
8
9
10
function isScrolledIntoView(elem)
{
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}

Sample Usage

On this example I’ll be loading iframes only when they are on the browsers viewport. For this to work I initially printed all iframe’s without the SRC attribute. Instead, I used the REL attribute as a placeholder for the iframe destination:

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
    var my_interval = setInterval(checkIframesVisibility, 1000);

    function checkIframesVisibility()
    {
        $("iframe").each(function(i) {
            if (isScrolledIntoView($(this)))
            {
                if ($(this).attr('src') == "")
                {
                    $(this).attr('src', $(this).attr('rel'));
                    $(this).iframeAutoHeight({debug: false, diagnostics: false});
                }
            }
        });
    }

    function isScrolledIntoView(elem)
    {
        var docViewTop = $(window).scrollTop();
        var docViewBottom = docViewTop + $(window).height();

        var elemTop = $(elem).offset().top;
        var elemBottom = elemTop + $(elem).height();

        return ((elemBottom <= docViewTop) && (elemTop <= docViewBottom));
    }

PS: The iframeAutoHeight is a plugin that auto resizes the iframe to the content’s height.

Hope you find this is useful!

jQuery Serialize form to JSON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$.fn.serializeObject = function()
{
   var o = {};
   var a = this.serializeArray();
   $.each(a, function() {
       if (o[this.name]) {
           if (!o[this.name].push) {
               o[this.name] = [o[this.name]];
           }
           o[this.name].push(this.value || '');
       } else {
           o[this.name] = this.value || '';
       }
   });
   return o;
};