If i am converting an empty array into json using json_encode it is comming as null object.
My output was :
Networkcheckins":[null]
To avoid that we can use this :
/*** create an object ***/
$checkinObjNew = new stdClass;
$checkinObj = array();
$encodedArray = array_map(utf8_encode, $checkinObj);
$checkinObjNew->Networkcheckins = $encodedArray;
print_r(json_encode($checkinObjNew));
Now it will return :
Networkcheckins":[]
Monday, March 28, 2011
Enable Popups in google Chrome
When Google Chrome blocks pop-ups for you, a small blue and white icon with a red "X"on it appears in the far right side of the address bar.
If you click on this icon, you can see a list of the pop-ups blocked for that page. You can click on the items in this list to view them, and you can also select the box to "Always allow pop-ups" from the site you're on (refresh for this change to take effect).
To manage the sites on this pop-up "whitelist," or to disable the pop-up blocker entirely, go to Tools > Options > Under the Hood > Content settings... > Pop-ups.
If you click on this icon, you can see a list of the pop-ups blocked for that page. You can click on the items in this list to view them, and you can also select the box to "Always allow pop-ups" from the site you're on (refresh for this change to take effect).
To manage the sites on this pop-up "whitelist," or to disable the pop-up blocker entirely, go to Tools > Options > Under the Hood > Content settings... > Pop-ups.
Wednesday, March 16, 2011
Convert Object To Array With PHP
Converting an object to an array using PHP comes with a small gotcha. One would be forgiven for thinking it is merely a task of casting the object and the job can be completed with a single line of code. This works well for simple objects, however, the task increases in complexity in line with the objects complexity.
Consider the following object that is cast to an array.
/*** create an object ***/
$obj = new stdClass;
$obj->foo = 'foo';
$obj->bar = 'bar';
$obj->baz = 'baz';
/*** cast the object ***/
$array = (array) $obj;
/*** show the results ***/
print_r( $array );
?>
The result from the code above produces an array representation of the object.
Array
(
[foo] => foo
[bar] => bar
[baz] => baz
)
Lets now increase the complexity of the object, so that the object bar is itself an object.
/*** a slightly more complex array ***/
$obj = new stdClass;
$obj->foo = 'foo';
$obj->bar = new stdClass;
$obj->bar->baz = 'baz';
/*** cast the object to array ***/
$array = (array) $obj;
/*** show the results ***/
print_r( $array );
?>
From the code above, the issue becomes a little clearer, as the resulting array contains an instance of stdClass, and not an array.
Array
(
[foo] => foo
[bar] => stdClass Object
(
[baz] => baz
)
)
To remedy this situation, some recursion is required to check for an object, and if an object is found, it is give an array representation.
/*** a complex object ***/
$obj = new stdClass;
$obj->foo = new stdClass;
$obj->foo->baz = 'baz';
$obj->bar = 'bar';
/**
*
* Convert an object to an array
*
* @param object $object The object to convert
* @reeturn array
*
*/
function objectToArray( $object )
{
if( !is_object( $object ) && !is_array( $object ) )
{
return $object;
}
if( is_object( $object ) )
{
$object = get_object_vars( $object );
}
return array_map( 'objectToArray', $object );
}
/*** convert the array to object ***/
$array = objectToArray( $obj );
/*** show the array ***/
print_r( $array );
?>
Now the results show a multi-dimensional array which is a true array representation of the object.
Array
(
[foo] => Array
(
[baz] => baz
)
[bar] => bar
)
Consider the following object that is cast to an array.
/*** create an object ***/
$obj = new stdClass;
$obj->foo = 'foo';
$obj->bar = 'bar';
$obj->baz = 'baz';
/*** cast the object ***/
$array = (array) $obj;
/*** show the results ***/
print_r( $array );
?>
The result from the code above produces an array representation of the object.
Array
(
[foo] => foo
[bar] => bar
[baz] => baz
)
Lets now increase the complexity of the object, so that the object bar is itself an object.
/*** a slightly more complex array ***/
$obj = new stdClass;
$obj->foo = 'foo';
$obj->bar = new stdClass;
$obj->bar->baz = 'baz';
/*** cast the object to array ***/
$array = (array) $obj;
/*** show the results ***/
print_r( $array );
?>
From the code above, the issue becomes a little clearer, as the resulting array contains an instance of stdClass, and not an array.
Array
(
[foo] => foo
[bar] => stdClass Object
(
[baz] => baz
)
)
To remedy this situation, some recursion is required to check for an object, and if an object is found, it is give an array representation.
/*** a complex object ***/
$obj = new stdClass;
$obj->foo = new stdClass;
$obj->foo->baz = 'baz';
$obj->bar = 'bar';
/**
*
* Convert an object to an array
*
* @param object $object The object to convert
* @reeturn array
*
*/
function objectToArray( $object )
{
if( !is_object( $object ) && !is_array( $object ) )
{
return $object;
}
if( is_object( $object ) )
{
$object = get_object_vars( $object );
}
return array_map( 'objectToArray', $object );
}
/*** convert the array to object ***/
$array = objectToArray( $obj );
/*** show the array ***/
print_r( $array );
?>
Now the results show a multi-dimensional array which is a true array representation of the object.
Array
(
[foo] => Array
(
[baz] => baz
)
[bar] => bar
)
Thursday, March 10, 2011
Read mails using Imap
// enter gmail username below e.g.--> $m_username = "yourusername";
$m_username = "gmail username";
// enter gmail password below e.g.--> $m_password = "yourpword";
$m_password = "gmail password";
// enter the number of unread messages you want to display from mailbox or
//enter 0 to display all unread messages e.g.--> $m_acs = 0;
$m_acs = 15;
// How far back in time do you want to search for unread messages - one month = 0 , two weeks = 1, one week = 2, three days = 3,
// one day = 4, six hours = 5 or one hour = 6 e.g.--> $m_t = 6;
$m_t = 0;
//----------->Nothing More to edit below
//open mailbox..........please
$m_mail = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", $m_username . "@gmail.com", $m_password)
// or throw a freakin error............you pig
or die("ERROR: " . imap_last_error());
// unix time gone by or is it bye.....its certanly not bi.......or is it? ......I dunno fooker
$m_gunixtp = array(2592000, 1209600, 604800, 259200, 86400, 21600, 3600);
// Date to start search
$m_gdmy = date('d-M-Y', time() - $m_gunixtp[$m_t]);
//search mailbox for unread messages since $m_t date
$m_search=imap_search ($m_mail, 'ALL' . $m_gdmy . '');
//If mailbox is empty......Display "No New Messages", else........ You got mail....oh joy
if($m_search < 1){
$m_empty = "No New Messages";}
else {
// Order results starting from newest message
rsort($m_search);
//if m_acs > 0 then limit results
if($m_acs > 0){
array_splice($m_search, $m_acs);
}
//loop it
foreach ($m_search as $what_ever) {
//get imap header info for obj thang
$obj_thang = imap_headerinfo($m_mail, $what_ever);
$message = imap_fetchbody($m_mail,$what_ever,2);
echo "Subject : ".$obj_thang->Subject;
echo "From : ".$obj_thang->fromaddress;
echo "To : ".$obj_thang->toaddress;
echo "
";
echo "Message : ".$message ;
echo "
";
echo date("F j, Y, g:i a", $obj_thang->udate);
//close mailbox bi by bye
imap_close($m_mail);
$m_username = "gmail username";
// enter gmail password below e.g.--> $m_password = "yourpword";
$m_password = "gmail password";
// enter the number of unread messages you want to display from mailbox or
//enter 0 to display all unread messages e.g.--> $m_acs = 0;
$m_acs = 15;
// How far back in time do you want to search for unread messages - one month = 0 , two weeks = 1, one week = 2, three days = 3,
// one day = 4, six hours = 5 or one hour = 6 e.g.--> $m_t = 6;
$m_t = 0;
//----------->Nothing More to edit below
//open mailbox..........please
$m_mail = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", $m_username . "@gmail.com", $m_password)
// or throw a freakin error............you pig
or die("ERROR: " . imap_last_error());
// unix time gone by or is it bye.....its certanly not bi.......or is it? ......I dunno fooker
$m_gunixtp = array(2592000, 1209600, 604800, 259200, 86400, 21600, 3600);
// Date to start search
$m_gdmy = date('d-M-Y', time() - $m_gunixtp[$m_t]);
//search mailbox for unread messages since $m_t date
$m_search=imap_search ($m_mail, 'ALL' . $m_gdmy . '');
//If mailbox is empty......Display "No New Messages", else........ You got mail....oh joy
if($m_search < 1){
$m_empty = "No New Messages";}
else {
// Order results starting from newest message
rsort($m_search);
//if m_acs > 0 then limit results
if($m_acs > 0){
array_splice($m_search, $m_acs);
}
//loop it
foreach ($m_search as $what_ever) {
//get imap header info for obj thang
$obj_thang = imap_headerinfo($m_mail, $what_ever);
$message = imap_fetchbody($m_mail,$what_ever,2);
echo "Subject : ".$obj_thang->Subject;
echo "From : ".$obj_thang->fromaddress;
echo "To : ".$obj_thang->toaddress;
echo "
";
echo "Message : ".$message ;
echo "
";
echo date("F j, Y, g:i a", $obj_thang->udate);
//close mailbox bi by bye
imap_close($m_mail);
Recursively create folder
This is an easy way to recursively create folders in PHP
$oldumask = umask(0);
mkdir('./backupfiles/depth1/depth2/depth3/', 0777, true); // or even 01777 so you get the sticky bit set
umask($oldumask);
$oldumask = umask(0);
mkdir('./backupfiles/depth1/depth2/depth3/', 0777, true); // or even 01777 so you get the sticky bit set
umask($oldumask);
Wednesday, March 9, 2011
Recordmydesktop in Ubuntu
http://recordmydesktop.sourceforge.net
a nice screen/desktop video recorder, try it
note: you can now install it from the repository through ADD/REMOVE
installation command: sudo apt-get install gtk-recordmydesktop
you can find it in Applications menu --> sound and video --> gtk-recordmydesktop
Using Recordmydesktop
in command line console type the command recordmydesktop
you can stop the recording by pressing ctrl-c
or in gui-front just click record, and just click the white square on system tray to stop
the output is in ogg video format however you can convert it to avi by mencoder
example
Quote:
mencoder -idx input.ogg -ovc lavc -oac mp3lame -o output.avi
*note install mencoder if its not installed by doing
Code:
sudo aptitude install mencoder
then you can upload it to youtube or wherever.
a nice screen/desktop video recorder, try it
note: you can now install it from the repository through ADD/REMOVE
installation command: sudo apt-get install gtk-recordmydesktop
you can find it in Applications menu --> sound and video --> gtk-recordmydesktop
Using Recordmydesktop
in command line console type the command recordmydesktop
you can stop the recording by pressing ctrl-c
or in gui-front just click record, and just click the white square on system tray to stop
the output is in ogg video format however you can convert it to avi by mencoder
example
Quote:
mencoder -idx input.ogg -ovc lavc -oac mp3lame -o output.avi
*note install mencoder if its not installed by doing
Code:
sudo aptitude install mencoder
then you can upload it to youtube or wherever.
utfencode and utfdecode
Everybody has a file where the database connection is established. They key is to add one more line to set the char encoding to utf8!
$database = mysql_connect($host, $user, $password);
mysql_select_db($db, $database);
mysql_set_charset('utf8', $database);
NOTE: don’t forget that your web page must contain the following meta tag:
meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /
Sample code:
$str = "L'élèvevaà";
$s1 = utf8_encode($str);
$s2 = utf8_decode($s1);
echo $s1;
echo $s2;
$database = mysql_connect($host, $user, $password);
mysql_select_db($db, $database);
mysql_set_charset('utf8', $database);
NOTE: don’t forget that your web page must contain the following meta tag:
meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /
Sample code:
$str = "L'élèvevaà";
$s1 = utf8_encode($str);
$s2 = utf8_decode($s1);
echo $s1;
echo $s2;
Wednesday, March 2, 2011
Imap Install in Ubuntu
Commands to install imap in Ubuntu
sudo apt-get install libc-client2007b libc-client2007b-dev
While installing i got an error in crossplatformui. So i removed this package using
sudo apt-get remove crossplatformui
Then give the following commands to continue the installation
sudo apt-get install php5-imap
sudo /etc/init.d/apache2 start
When I run phpinfo(), I get the following imap values:
IMAP c-Client Version: 2004
SSL Support: enabled
Kerberos Support: enabled
sudo apt-get install libc-client2007b libc-client2007b-dev
While installing i got an error in crossplatformui. So i removed this package using
sudo apt-get remove crossplatformui
Then give the following commands to continue the installation
sudo apt-get install php5-imap
sudo /etc/init.d/apache2 start
When I run phpinfo(), I get the following imap values:
IMAP c-Client Version: 2004
SSL Support: enabled
Kerberos Support: enabled
Subscribe to:
Comments (Atom)