Wednesday, April 8, 2015

Mysql query to find nearest user to your current location

Following query will find records which came under 1.5 km on the basis of user current latitude and longitude

user latitude is 21.153518
user latitude is 79.045513

SELECT *
        FROM users
        WHERE ( 3959 * ACOS( COS( RADIANS( 21.153518 ) ) * COS( RADIANS( users.latitude ) ) * COS( RADIANS( users.longitude ) - RADIANS( 79.045513 ) ) + SIN( RADIANS( 21.153518) ) * SIN( RADIANS( users.latitude ) ) ) )  < 1.5


Here is some more query may be helpful to you guys :)

SELECT users. * ,ACOS( SIN( RADIANS( users.`latitude` ) ) * SIN( RADIANS( 21.153518 ) ) + COS( RADIANS( users.`longitude` ) ) * COS( RADIANS( 21.153518 ) ) * COS( RADIANS( users.`longitude` ) - RADIANS( 79.045513 ) ) ) *6380 FROM users


SELECT * , ( 3959 * ACOS( COS( RADIANS( 21.153518 ) ) * COS( RADIANS( users.latitude ) ) * COS( RADIANS( users.longitude ) - RADIANS( 79.045513 ) ) + SIN( RADIANS( 21.153518) ) * SIN( RADIANS( users.latitude ) ) ) ) AS distance
        FROM users
        ORDER BY distance
Read More »

Wednesday, April 23, 2014

How do you set and get ckeditor value by javascript



How do you get data from ckeditor ?
use below code to get data from ckeditor , assume your editor name is 'description'

alert(CKEDITOR.instances.description.getData());

How do you set data in ckeditor ?
use below code to set data in ckeditor , assume your editor name is 'description'

CKEDITOR.instances.description.setData( '<h1>Hello Ubed Khan PHP programmer</h1>' );
Read More »

Monday, April 21, 2014

How to build cordova project

How to build  cordova project

Step 1) Make sure Node.js is installed on you computer. If not installed please install latest version from http://nodejs.org/

Step 2)  Open terminal and type the following command  to install the Cordova CLI
 sudo npm install -g cordova

Step 3) Change directory where you want to store project by following command
cd ~/project_directory

step 4) Create a project called “myfirstapp” by following command
cordova create myfirstapp com.yourname.myfirstapp myfirstapp

step 5)  Change directory to myfirstapp by following command
cd myfirstapp

step 6) Add platform support for your app here we are adding platform support for IOS by following command
cordova platforms add ios

step 7) Now build cordova project
You need the iOS SDK installed on your computer to build an iOS version of your application using the following command
cordova build ios


The project is built in the myfirstapp/platforms/ios folder. Double-click Workshop.xcodeproj to open the project in XCode, and run it in the emulator.

You will see "Device Ready".....

Note: this guide will work only on Mac operating system
Read More »

Wednesday, December 25, 2013

How to escape quotes in php

Here i have paste two example of how to escape quotes in php we face always when we use php , html and javascript event combine.

First Example
<?php

echo $chkbox = '<input type="checkbox" name="pages" id="chk_'.$val['sb_id'].'" value="'.$val['sb_id'].'" onclick=\'assignSidebar('.$val['sb_id'].',"'.$val['sb_name'].'")\' />';

?>
Second Example
<?php

$name = "khan's";
$name = htmlentities(str_replace("'", "\'", $name));
echo $response = "<img src=\"images/action_delete.gif\" onClick=\"confirmDelete('" . $name . "')\"/>";

?>

php to encode special characters but not html tags

For Latin-1 you can escape characters easily with:
$html = preg_replace('/[\x80-\xFF]/e', '"&#x".dechex(ord("$0")).";"', $html);
For UTF-8 it's a bit more involving:
$html = preg_replace_callback("/(?!\w)\p{L}/u", "xmlent", $html);
function xmlent($m) {
    $str = mb_convert_encoding( $m[0] , "UCS-2BE", "UTF-8");
    return "&#x" . bin2hex($str) . ";";
}
Read More »

Thursday, October 4, 2012

How to take screenshot from video in php


Here i am going to explain you how to take screenshot(thumbnail) from video in php.

First Of All we need to installed ffmpeg on our server.If your server does not have this facility please contact your web hosting provider.

function captureImageFromVideo($imagename,$videoname)
 {

$ffmpeg = '/usr/bin/ffmpeg';

//video dir
$video = '/public_html/video/'.$videoname;


//where to save the image
$image = '/public_html/image/'.$imagename;

//what time to take screenshot from video, here it will take screenshot after 5 second from it being started.
$interval = 5;

//screenshot size
$size = '290x191';

//ffmpeg command
$cmd = "$ffmpeg -i $video -deinterlace -an -ss $interval -f mjpeg -t 1 -r 1 -y -s $size $image 2>&1";

//PHP built in function execute an external program    
exec($cmd);

}

We can also use ffmpeg to convert videos from one format to another.
For example if you uploading video which have .wmv extension. suppose if i want to convert this video to .mp4 format you can easily achieve this using ffmpeg.
We can use this ffmpeg command for this task
$cmd=”$ffmpeg -i $videofile -vcodec libx264 -acodec libfaac test.mp4“;

The installed ffmpeg should support the libx264 and libfaac.

I hope it will help some of you guys !
Read More »

Wednesday, October 3, 2012

How to create zip and download


In this post I am going to explain you how to create zip file and download.
We need some files on some directory which we will zipped by code.
For this task we are using PHP zip library, it must enabled on your server.
Here is function
function zipFilesDownload($file_names,$archive_file_name,$file_path)

{

    $zip = new ZipArchive();

    //create the file and throw the error if unsuccessful

    if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {

                    exit("cannot open <$archive_file_name>\n");

    }

    //add each files of $file_name array to archive

    foreach($file_names as $files)

    {

                    $zip->addFile($file_path.$files,$files);

                    //echo $file_path.$files,$files."<br />";

    }

    $zip->close();

    //then send the headers to force download the zip file

    header("Content-type: application/zip");

    header("Content-Disposition: attachment; filename=$archive_file_name");

    header("Pragma: no-cache");

    header("Expires: 0");

    readfile("$archive_file_name");

    exit;

}
There are three parameters in function  $file_names, $archive_file_name and $file_path
$file_names =>  where you have to passed files in array.
$archive_file_name => It is the name of your zip file which is created by PHP library if it’s not open.
$file_path =>  It is your directory path where your files are placed which you want to zipped.
Read More »

Thursday, September 6, 2012

MySQL Using SUM and CASE in statement


Problems i faced in mysql query

one day i was working on Race project where director can payment online and manually .
I had been stored all Amount in one column "amt".
take a look on below image

MySQL Using SUM and CASE in statement

I was want SUM of manual payment , electronic payment and total of all payment in one query.  That day i had been write following query by using Mysql SUM() function and conditional CASE statement.
Here we need to use conditional CASE statement within a SUM() That's it.
And the query is

SELECT  SUM(CASE WHEN `pay_type`="manual" THEN `amt` ELSE 0 END) AS manual_amt,SUM(CASE WHEN `pay_type`="electronic" THEN `amt` ELSE 0 END) AS electronic_amt,SUM(`amt`) AS total_amt FROM pay

In cakephp you can sum like below
 $condition = array('fields' => array('Message.read_status AS read_status','SUM(CASE WHEN read_status=0 THEN \'1\' ELSE \'0\' END) AS MsgCnt'));

Perhaps it can happen with you so use my query and enjoy! .

Read More »