Subscribe For Free Updates!

We'll not spam mate! We promise.

May 26, 2013

Rounding Numbers to ‘N’ Decimal

Rounding Numbers to ‘N’ Decimal

 This will let you round off a number to ‘N’ decimal places. In the below example we are rounding of a number to 2 decimal places.

var num = 4.56789;
alert(num.toFixed(2));

It will alert as 4.56.

Another function to display a number upto a length is toPrecision(x).

num = 520.2375;
result = num.toPrecision(4);
alert(result);

It will display as 520.2.
 


Generate Random number from 1 to N

Generate Random number from 1 to N

Below little script helps you in generating random number between 1 to N.


var random = Math.floor(Math.random() * N + 1);




May 23, 2013

Basic MySQL Query Commands

Basic MySQL Query Commands :

Syntax for Query Commands

1. CREATE Command

The Create command is used to create a table by specifying the tablename, fieldnames and constraints as shown below:

Syntax:
$createSQL=("CREATE TABLE tblName");

Example:
$createSQL=("CREATE TABLE tblstudent(fldstudid int(10) NOTNULL AUTO_INCREMENT PRIMARY KEY,fldstudName VARCHAR(250) NOTNULL,fldstudentmark int(4) DEFAULT '0' ");

2. SELECT Command

The Select command is used to select the records from a table using its field names. To select all the fields in a table, '*' is used in the command. The result is assigned to a variable name as shown below:

Syntax:
$selectSQL=("SELECT field_names FROM tablename");

Example:
$selectSQL=("SELECT * FROM tblstudent");

3. DELETE Command

The Delete command is used to delete the records from a table using conditions as shown below:

Syntax:
$deleteSQL=("DELETE * FROM tablename WHERE condition");

Example:
$deleteSQL=("DELETE * FROM tblstudent WHERE fldstudid=2");

4. INSERT Command

The Insert command is used to insert records into a table. The values are assigned to the field names as shown below:

Syntax:
$insertSQL=("INSERT INTO tblname(fieldname1,fieldname2..) VALUES(value1,value2,...) ");

Example:
$insertSQL=("INSERT INTO Tblstudent(fldstudName,fldstudmark)VALUES(Baskar,75) ");

5. UPDATE Command

The Update command is used to update the field values using conditions. This is done using 'SET' and the fieldnames to assign new values to them.

Syntax:
$updateSQL=("UPDATE Tblname SET (fieldname1=value1,fieldname2=value2,...) WHERE fldstudid=IdNumber");

Example:
$updateSQL=("UPDATE Tblstudent SET (fldstudName=siva,fldstudmark=100) WHERE fldstudid=2");

6. DROP Command

The Drop command is used to delete all the records in a table using the table name as shown below:

Syntax:
$dropSQL=("DROP tblName");

Example:
$dropSQL=("DROP tblstudent");



May 21, 2013

Get body message content from mail using PHP

Get body message content from mail using PHP :

 Here i will explain how to get mail contents(body of mail) by using php. Here i used two functions like imap_num_msg() and imap_body() which return the contents of a specific message respectively and the number of messages in the mail box.

Syntax of these function:

int imap_num_msg ( resource imap_stream)
string imap_body ( resource imap_stream, int msg_number, int options)

1. To get number of mail in inbox :


 For get this we need to pass IMAP stream into imap_num_msg() function and it return the result.


<?php

    $imap = imap_open("{mail.yourserver.com:143}INBOX", "username", "password");

    $message_count = imap_num_msg($imap);

    print $message_count;

    imap_close($imap); 

?>

2. To get message content :

 <?php

    $imap = imap_open("{mail.yourserver.com:143}INBOX", "username", "password");

    $message_count = imap_num_msg($imap);

    for ($i = 1; $i <= $message_count; ++$i) {

        $header = imap_header($imap, $i);

        $body = trim(substr(imap_body($imap, $i), 0, 100)); // 100 specifies no.of character u need.

        $prettydate = date("jS F Y", $header->udate);

        if (isset($header->from[0]->personal)) {

            $personal = $header->from[0]->personal;

        } else {

            $personal = $header->from[0]->mailbox;

        }

        $email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";

        echo "On $prettydate, $email said \"$body\".\n";

    }

    imap_close($imap); 

?>





May 19, 2013

.htaccess Automatically redirect to Mobile Website

.htaccess Automatically redirect to Mobile WebSite :


 Here i will explain how to redirect your website automatically to mobile scree website. This method is functioned from your website .htaccess. so this method is more efficient compare with find screen resolution and then redirect.

The .htaccess only assign our websites url functionality in on-load of site front-end coding.

Here is the coding :


 >>Note : # is used to comand the line in .htaccess.

RewriteEngine on
RewriteBase /
# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.techmiqzone.blogspot.in]

# Check if this looks like a mobile device
# (You could add another [OR] to the second one and add in what you
#  had to check, but I believe most mobile devices should send at
#  least one of these headers)
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT}  "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m\.
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) 

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE}        !^.*mredir=0.*$ [NC]

# Now redirect to the mobile site
RewriteRule ^ http://m.techmiqzone.blogspot.in [R,L]


Use JSON in PHP 4 or PHP 5.1.x

Use JSON in PHP 4 or PHP 5.1.x : 


Here i will explain how to use JSON (json_encode() , json_decode()) in versions of PHP earlier than 5.2. Add the following custom function to use (json_encode() , json_decode()) this functions in php. IF u don't have this JSON PEAR Package You can download in HERE 

1. for json_encode() :

if (!function_exists('json_encode')) {
    function json_encode($content) {
        require_once 'classes/JSON.php';
        $json = new Services_JSON;
        return $json->encode($content);
    }
}

2. for json_decode() :

if (!function_exists('json_decode')) {
    function json_decode($content, $assoc=false) {
        require_once 'classes/JSON.php';
        if ($assoc) {
            $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
        }
        else {
            $json = new Services_JSON;
        }
        return $json->decode($content);
    }
}


Now you can work with JSON, even when you eventually upgrade to PHP 5.2.

 The JSON PEAR Package download page link is given below :
 
   >>> Download the Services_JSON PEAR package


May 10, 2013

php to remove last two characters from string



php remove last two characters from string

 The following code is used to remove character from the last of the string. here "substr()" function is used.
this function is used to get substring from original string.

<?php
echo substr('abcdef', 1);     // bcdef
echo substr('abcdef', 1, 3);  // bcd
echo substr('abcdef', 0, 4);  // abcd
echo substr('abcdef', 0, 8);  // abcdef
echo substr('abcdef', -1, 1); // f

// Accessing single characters in a string
// can also be achieved using "square brackets"
$string = 'abcdef';
echo $string[0];                 // a
echo $string[3];                 // d
echo $string[strlen($string)-1]; // f

?>

May 9, 2013

Check String Contain Specific words - PHP



Check String Contain Specific words - PHP

The following code is used to find the specific words or char from a string by using php.
here "strpos()" is used. This function is used to get string position from the original string. here i just get true or false result if the original string contains the specific word or character,

if (strpos($a,'are') !== false) 
{
    echo 'true';
}
else
{
    echo 'false';
}

May 7, 2013

Get URL from address bar - PHP


Get URL from address bar - PHP

 The following code is used to get full url address by using php.

<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>

Notes:
 Here we are using 2 functions in php to get full url from address bar.
1. $_SERVER['HTTP_HOST']
2. $_SERVER['REQUEST_URI']
$_SERVER['HTTP_HOST'] - This function will show only server name.
$_SERVER['REQUEST_URI'] - This function will show you the path to file of your url.

 When you run $_SERVER['HTTP_HOST'];  for this website you'll get result "www.techniqzone.blogspot.in" only, no "http://" no "/Get URL from address bar - PHP"

<?php 
$server=$_SERVER['HTTP_HOST'];
echo $server;
?>

Output is "www.techniqzone.blogspot.in".

This script "$_SERVER['REQUEST_URI']" display the result like below.
no "http://" and no "www.techniqzone.blogspot.in

Example:

<?php
$request_url=$_SERVER['REQUEST_URI'];
echo $request_url;
?>

Output of $_SERVER['REQUEST_URI'] is "/Get URL from address bar - PHP"

If you want to get full URL from this site you, we have to use (.) to connect 2 functions together and create http:// by yourself.

<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>

May 6, 2013

Random Password Generator


Random Password Generator

 This tool is used to generate the random password that is generate random string with given "n" length.
This tool is very use full for generate password with full secured.


Password length :
Password :

May 5, 2013

Get details about an image in PHP


Get details about of an image in PHP:

The following code is used to get the all details about image by using php and the result will be store in an array.



<?php
$filename = image.jpg; //image file path
$imgDetails = getimagesize($filename);
print_r($imgDetails);
?>

The beloow code is simple to get width and height, type of the image file.


<?php
$img = "images/empty.gif";
list($width, $height, $type, $attr) = getimagesize($img);
print "Width : $width and height = $height";
?> 



May 4, 2013

Get cursor position in a textarea - Jquery


Get cursor position in a textarea - Jquery:

The following code is used to get the cursor point in textarea. if you change the id of textarea with particular div id then you can get the cursor points within the div.


(function ($, undefined) {
    $.fn.getCursorPosition = function() {
        var el = $(this).get(0);
        var pos = 0;
        if('selectionStart' in el) {
            pos = el.selectionStart;
        } else if('selection' in document) {
            el.focus();
            var Sel = document.selection.createRange();
            var SelLength = document.selection.createRange().text.length;
            Sel.moveStart('character', -el.value.length);
            pos = Sel.text.length - SelLength;
        }
        return pos;
    }
})(jQuery);

the bellow code is for set textarea id to call the above function.

$("#myTextBoxSelector").getCursorPosition();

May 3, 2013

JQuery to get Current Date


JQuery to get Current Date:

 The following code is used to get current date and time by using jquery.

 Before that, Date() function is not the part of JQuery. it is one of JavaScript's features.

var date = new Date();

var month = date.getMonth()+1;
var day = date.getDate();
var output = date.getFullYear() + '-' + (month<10 :="" day="" font="" month="">


May 2, 2013

Random String Generator in PHP



Random String Generator in PHP:


 The following function "getrandum_str()" is used to get the random string with specified string length.
This function may used to get encrypted key value or generate the random password and etc...


<?php
function getrandum_str()
{
  $abc = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $lengt = 10; // set the length of result
  $strn = "";
  for($i=0;$i<$lengt;$i++)
  {
    $strn.=$abc[rand(0,strlen($abc)-1)];
  }
  return $strn;
}
?>

JQuery to Find window Height


JQuery to set div in center of screen:

 The following code is used to find the center position of the any size of the screen. here i set the div in center of the screen using jquery.

  "$(window).height()" this function is used to get hight of the window(Screen).


var divCenter= $('#div_id');
divCenter.css("top", ($(window).height() - divCenter.height())/2  + 'px');

You can change this code whatever you find like screen height, width etc.

May 1, 2013

Find Browser and Platform Details Using JavaScript


Find Browser and Platform Details Using JavaScript:

The following code is used to find the Browser and Platform details. This script displays browser code name, browser name, browser version, cookies enabled disabled details, platform details and user agent header details.


<script>
"Browser CodeName: " + navigator.appCodeName + "";
"Browser Name: " + navigator.appName + "";
"Browser Version: " + navigator.appVersion + "";
"Cookies Enabled: " + navigator.cookieEnabled + "";
"Platform: " + navigator.platform + "";
"User-agent header: " + navigator.userAgent + "";
</script>