Post your trick

handling custom form in wordpress

I had a custom login form in WP(3.1.2) and the problem i was facing is whenever the form was submitted i used to get empty $_POST array.This was my code

<form method=”post” action=”<?php echo bloginfo(’url’).’/?page_id=221′; ?>”>

After some google search I found the solution which was :

<form method=”post” action=”<?php echo $_SERVER['PHP_SELF'].’/?page_id=221′; ?>”>

which means if you are submitting the form on the same page use $_SERVER else you wont get the data in post


how to create an object of the topmost class in php

$data = array(
‘currency’ => $order->getOrderCurrencyCode(),
‘date_added’ => date(’Y-m-d’)
);
$order_query = new stdClass();
$order_query->row = $data;


php array to sql query

I came across a case where i was getting a post in which i get column names of my table in keys, so i used the following code

$data = array(’column_1′ => 1, ’column_2′ => 45, ’column_3′ => 25 );

$sql = http_build_query($data, ”, ‘, ‘);

$query = “UPDATE table_name SET ” . $sql . ” WHERE id = ‘ 24 ‘ ” ;

Note: This cant be used if you values are coming in string


PHP converting all Errors to Exception

<?php

function exceptions_error_handler($severity, $message,$filename, $lineno) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
set_error_handler(’exceptions_error_handler’);
?>


php array to http url

hi guys,

I came across a beautiful function of php which will convert any php array to the GET parameter for url.

E.g.

$data = array( ‘foo’ => ‘bar’, ‘jimy’ => ‘modi’ );

$url = http_build_query($data);

this will give you

foo=bar&jimy=modi

More info


How to get Latitude and Longitude from IP address

After searching for more than four to five hours i got the link which give latitude, longitude and other information such as address including city and country. So here is a small function which will give you latitude and longitude of the given ip address.

function getLatLng($ip){

//get the contents from the site by file_get_contents.
$sXML = @file_get_contents(’http://ipinfodb.com/ip_query.php?ip=’.$ip.’&timezone=false’);

//make the response in a xml object so we can parse it.
$oXML = @new SimpleXMLElement($sXML);

$array = array(
‘Latitude’ => (string) $oXML->Latitude[0],
‘Longitude’ => (string) $oXML->Longitude[0]
);
return $array;
}


explicitly call super class constructor in php

Unlike Java , the constructor of the super class will not be called implicitly in PHP .

To call the parent class constructor from the child class we can use :

parent::construct();


Adding date and time in PHP

I came across a situation where i want to end an event exactly after two days and before an hour of third day ( i.e. 11:00 pm on third day). After searching on google and checking the date function in php manual i came to this solution

$new_date = ( strtotime(’-1 hour’,strtotime ( ‘+3 day’ , strtotime (date ( ‘Y-m-d’ ) ) ) ));

This will return a unix timestamp which will give you 11:00 pm of third day.
eg. if todays date is 27/05/2010 07:08:15 then it will give you 29/05/2010 23:00:00


Inserting multiple values from a listbox into the database

if you have a listbox called “department” and want to insert multiple values from the listbox into the database,then:
when the user submits the form,store the value in a variable
$company_dept=$_POST['department'];
query:INSERT into table_name values(’”.join(”,”,$company_dept).”‘)


Email check in php
function isValidEmail($email){
return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]
+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
}

Post A Trick !

How does "Kodetricks" work?

We at kodeplay like to share knowledge. With Kodetricks, even you can join us. All you need to do is post a programming related trick if you have one or rate a trick if you like someone else's.



Technology

Contact Us