5/14/2012

PHP: Custom Array Filter

$my_array = array(2, "a", null, 2.5, NULL, 0, "", 8, -1,array());
 
class ArrayFilter
{
 public static function not_empty($v) 
 {
   if($v !== null && trim($v) !== '' && $v !== array())
   {
  return true;
   }
 }
 
 function filter($values)
 {
  return array_filter($values, "self::not_empty");
 }
}
$arrayFilter = new ArrayFilter();
print_r($arrayFilter->filter($my_array));
Result:
C:\>php -f filter.php
Array
(
    [0] => 2
    [1] => a
    [3] => 2.5
    [5] => 0
    [7] => 8
    [8] => -1
)
C:\>

5/06/2012

MongoDate và mktime trong PHP

Một vấn đề gặp phải khi sử dụng mktime() để khởi tạo timestamp truyền vào constructor của MongoDatemktime() sử dụng timezone mặc định trong php.ini trong khi MongoDate lại dùng timestamp theo GMT. Nếu default timezone trong php.iniAsia/Saigon (GMT+7) thì đối tượng ISODate được khởi tạo trong MongoDB sẽ có thời gian bị lùi về 7h.

Để giải quyết vấn đề này, sử dụng hàm gmmktime() để tạo timestamp theo giờ GMT.

Lưu ý là, hàm date() cũng dùng default timezone mặc định của php.ini. Để có thể chuyển timezone cho hàm date(), dùng hàm date_default_timezone_set(). Ví dụ:
date_default_timezone_set('UTC'); // Set default timezone to GMT

5/01/2012

Facebook - Check users' allowed permission for your app

In this entry, we will discover the answer of the question "How to check if the user has already allowed publish_stream for your app?". And luckily, we can achieve this goal by using Grap API. For example I will check whether the user has allowed publish_stream permission or not.
FB.api('/me/permissions', function (response) {
  //please note that we use data[0]
  if(response.data[0].publish_stream==1)
  {
     //Do some stuff such as post a photo to user wall        
  }
  else
  {
    alert('You need to grant "Post on your behalf" permission in order to save to Facebook!');
  }
}
It is easy, isn't it? There is my complete code for force user allow some permission for my app. It is not optimized yet, however it still run OK and meets my need.
FB.getLoginStatus(function(response) {
 if (response.status === 'connected') 
 {
  FB.api('/me/permissions', function (response) {
   if(response.data[0].publish_stream==1)
   {
    FB.api('/me', function(response) {
     
    });
   }
   else
   {
    FB.login(function(response) {
     FB.api('/me/permissions', function (response) {
      if(response.data[0].publish_stream==1)
      {
       //Do some stuff that need permissions
      }
      else
      {
       alert('You need to grant "Post on your behalf" permission in order to save to Facebook!');
      }
     });
    },
                                  {scope:'publish_stream,user_photos,user_birthday,email'});
   }
  });
 }
 else if (response.status === 'not_authorized')
 {
  FB.login(function(response) {
   if (response.authResponse) 
   {
    FB.api('/me/permissions', function (response) {
      if(response.data[0].publish_stream==1)
      {
      
      }
      else
      {
        alert('You need to grant "Post on your behalf" permission in order to save to Facebook!');
      }
    });
   } 
   else 
   {
    alert('You need to authorize the application before you do other action');
   } 
  }, {
   scope : 'publish_stream,user_photos,user_birthday,email'
  });
 }
});
Hung Nguyen

The 0/1 Knapsack Problem - Dynamic Programming

 References: https://www.youtube.com/watch?v=EH6h7WA7sDw  Class Item class Item { private $_weight; private $_value; public functi...