16 November 2012

sort multi dimentional array by the value of specific key

 

Sample Array

Array (
[0] => Array
    (
        [iid] => 1
        [invitee] => 174
        [nid] => 324343
        [showtime] => 2010-05-09 15:15:00
        [location] => 13
        [status] => 1
        [created] => 2010-05-09 15:05:00
        [updated] => 2010-05-09 16:24:00
    )
[1] => Array
    (
        [iid] => 1
        [invitee] => 220
        [nid] => 21232
        [showtime] => 2010-05-09 15:15:00
        [location] => 12
        [status] => 0
        [created] => 2010-05-10 18:11:00
        [updated] => 2010-05-10 18:11:00
    ))

 Snippet

function cmp($a, $b) {
    if ($a['status'] == $b['status']) {
        return 0;
    }
    return ($a['status'] < $b['status']) ? -1 : 1;
}

usort($array, "cmp");

Note

here array sorted by the value of specifed key(status),u can change status withany key that your array has :)

convert stdClass object array to normal array and normal array to stdClass object




stdClass obect to normal array




function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}

if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}


normal array to stdClassobject

function arrayToObject($d) {
        if (is_array($d)) {
            /*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            return (object) array_map(__FUNCTION__, $d);
        }
        else {
            // Return object
            return $d;
        }
    }

Sample Output

stdClass Object
(
    [foo] => Test data
    [bar] => stdClass Object
        (
            [baaz] => Testing
            [fooz] => stdClass Object
                (
                    [baz] => Testing again
                )

        )

    [foox] => Just test
)

Array
(
    [foo] => Test data
    [bar] => Array
        (
            [baaz] => Testing
            [fooz] => Array
                (
                    [baz] => Testing again
                )

        )

    [foox] => Just test
)

  

Thanks to:

http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/

 

Another simple way

json_decode(json_encode(simplexml_load_string('xml string')),1);


24 October 2012

group by,order by sum value together

Table Structure and Data

employer_id    salary

111              1000
112              2000
113              1000
111              1000
112              5000
113              2000
111              1000
113              1000


this is just an example for understanding purpose..


so here we have 3 employers,employe(111) got salary 3 times,employe(112) got salary 2times and employe(113) got salary 3 times

so we are going to sort the list by who got maximum money

SQL Query


SELECT sum( salary ) AS amt , employ_id FROM TABLE GROUP BY employer_id ORDER BY amt DESC

 
SAMPLE OUTPUT

amt     employer_id
7000   112
4000   113
3000   111
No matter howmany times the employe got salary just we are listing out based on highest earner..
















30 September 2012

way2sms php script

Last Update on 17/Jan/2013

<?php
$to=$_GET['to'];
$msg=$_GET['msg'];
$user=$_GET['user'];
$pass=$_GET['pass'];
$nos=explode(",",$to);
foreach($nos as $to)
{
$ch = curl_init("http://site5.way2sms.com/Login1.action");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, "username=$user&password=$pass&userLogin=yes&button=Login");
$response = curl_exec($ch);
curl_close($ch);
list($header, $body) = explode("\r\n\r\n", $response, 2);
$jid=explode("JSESSIONID=",$header);
$jid=explode(";",$jid[1]);
$jid=$jid[0];
$e=explode("~",$jid);
$e=$e[1];
$ch = curl_init("http://site5.way2sms.com/jsp/InstantSMS.jsp");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Cookie: JSESSIONID=$jid"));
$response = curl_exec($ch);
curl_close($ch);
$action=explode('expensive" value="',$response);
$action=explode('"',$action[1]);
$action=$action[0];
$p="embassy=$e&HiddenAction=instantsms&catnamedis=Birthday&chkall=on&expensive=$action&MobNo=$to&textArea=$msg";
$ch = curl_init("http://site5.way2sms.com/quicksms.action");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Cookie: JSESSIONID=$jid"));
curl_setopt($ch,CURLOPT_POSTFIELDS,$p);
curl_exec($ch);
curl_close($ch);
echo "<p>sms sent to $to</p>";
}
?>


download script here http://files.mspn.in/952185

singlesms usage w2s.php?user=your mobile number&pass=your password&to=destination mobile number&msg=your message ;)
multisms usage  w2s.php?user=your mobile number&pass=your password&to=mobilenumber1,mobilenumber2,mobilenumber3&msg=your message

share your feedbacks 




simple oauth with google

function code2token($code) {
    $oauth2token_url = "https://accounts.google.com/o/oauth2/token";
    $clienttoken_post = array(
    "code" => $code,
    "client_id" => '524478858957-keh6dvesm9ml3kdv67qtk8vlqori8l3r.apps.googleusercontent.com',
    "client_secret" => 'cws9DqO',
    "redirect_uri" => 'http://cc.cr/redir.php',
    "grant_type" => "authorization_code"
    );
    
    $curl = curl_init($oauth2token_url);

    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $json_response = curl_exec($curl);
    curl_close($curl);

    $authObj = json_decode($json_response);
    
    if (isset($authObj->refresh_token)){
        global $refreshToken;
        $refreshToken = $authObj->refresh_token;
    }
              
    $accessToken = $authObj->access_token;
    return $accessToken;
}
function call_api($accessToken,$url){
    $curl = curl_init($url);
 
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $curlheader[0] = "Authorization: Bearer " . $accessToken;
    curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);

    $json_response = curl_exec($curl);
    curl_close($curl);
        
    $responseObj = json_decode($json_response);
    
    return $responseObj;      
}
if(isset($_REQUEST['code'])){
$data = call_api(code2token($_REQUEST['code']),"https://www.googleapis.com/oauth2/v1/userinfo");
echo print_r($data);//array of user profile details name,email,etc
}

4 July 2012

unknown file size while downloading via php script

if your browser doesn't appear to be obeying the headers generated by your PHP script—especially Content-Length—it is fairly likely that Apache's mod_deflate extension is enabled.
You can easily disable it for a single script using the following line in an applicable .htaccess file: 
 
SetEnvIfNoCase Request_URI ^/download\.php no-gzip dont-vary
 
 
 
thanks to http://paul.luminos.nl/show_post.php?p=471

7 June 2012

reset mysql root password

ps -ef | grep mysql      - checks if mysql/mysqld is one of the running processes. 
 
 
pkill mysqld             - kills the daemon, if it is running.
 
 
mysqld_safe --skip-grant-tables & - Run MySQL safe daemon with skipping grant tables
  
 
mysql -u root mysql - Login to MySQL as root with no password
 
 
UPDATE user SET password=PASSWORD("your-new-passowrd") WHERE user="root"; 
FLUSH PRIVILEGES;
 
 
 
Done