Friday 26 July 2013

Getting Random values from Array

To Get Random Values from Array :

$arr = array("a"=>"apple","b"=>"ball","c"=>"cat","d"=>"dog" );

shuffle($arr);

Eg :

Before Shuffle :

Array ( [a] => apple [b] => ball [c] => cat [d] => dog )

After Shuffle :

Array ( [0] => ball [1] => cat [2] => apple [3] => dog1 )   // will change order every time we refresh the page


To Pick Random Key from Array :

Eg : picking 2 random keys from  an array

$val = array_rand($arr,2);

print_r($val);

Array ( [0] => b [1] => d )




Converting Object to Array


Convert all objects to Array :

function objectToArray( $object ) {
    if( !is_object( $object ) && !is_array( $object ) ) {
        return $object;
    }
    if( is_object( $object ) ) {
        $object = (array) $object;
    }
    return array_map( 'objectToArray', $object );
}

(or)


function object_to_array($data)
{
    if (is_array($data) || is_object($data))
    {
        $result = array();
        foreach ($data as $key => $value)
        {
            $result[$key] = object_to_array($value);
        }
        return $result;
    }
    return $data;
}


Converting First object to Array :


function app_convertObjectToArray($objectValue) {
if (is_array ( $objectValue )) {
$arrayValue = $objectValue;
} else {
$arrayValue = array ();
if (isset ( $objectValue )) {
array_push ( $arrayValue, $objectValue );
}
}
return $arrayValue;
}


Friday 12 July 2013

To Rewrite example.com to www.example.com using apache


<VirtualHost *:80>
    ServerAdmin admin@example.com
    DocumentRoot /opt/data/www/example.com
    ServerName example.com
    ServerAlias www.example.com
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.com$ [NC]
    RewriteRule (.*) http://www.example.com$1 [R=301,L]
    CustomLog /var/log/httpd/example.com_access.log combined
</VirtualHost>
 

Refer for more :          

https://my.bluehost.com/cgi/help/htaccess_redirect

http://httpd.apache.org/docs/current/rewrite/proxy.html

Monday 24 June 2013

Checking the uploading image size without submitting the form / Posting the upload file details without submitting the form


html:
--------

<form id="upload_form" >
      <input type="file" class="file" name="filename[]"  onchange="validateFileSize()" />
 </form>


js:
----


function validateFileSize(){
    var form_obj = document.getElementById("upload_form");  //upload_form is a form id
        try{
            var formData = new FormData(form_obj);   
            $.ajax({
                url: 'upload.php',  //server script to process data
                type: 'POST',
                success: function(data){},
                error: function(data){},
                data: formData,
                cache: false,
                contentType: false,
                processData: false
            });
        }catch(e){
        }
 }   



upload.php:
-----------------


  if (isset($_FILES["filename"]["name"])) {
    try {          
           echo "Upload: " . $_FILES["filename"]["name"] . "<br />";
            echo "Type: " . $_FILES["filename"]["type"] . "<br />";
            echo "Size: " . ($_FILES["filename"]["size"] / 1024) . " Kb<br />";
            echo "Temp file: " . $_FILES["filename"]["tmp_name"] . "<br />";
          } catch (Exception $e) {
             print_r($e);
          }    
    }








Note : FormData will not work in old browsers and IE
 For old browser, we can use iframe

http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery
http://stackoverflow.com/questions/9516251/upload-files-without-refresh-page   ===> using iframe
   
  

To Override document.write / alternative solution for document.write using javascript



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hai
<div id="div"></div>

<script>
document.write = function(w) {
    document.getElementById("div").innerHTML += w;    //w has the write statement from document.write
}

document.write("Hello");
document.write("world");

</script>
</body>
</html>

Getting ViewPort / Device Screen Size using Javascript (subtracting from browser scrollbar size):


function getViewport() {

    var viewPortWidth;
    var viewPortHeight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined') {
      viewPortWidth = window.innerWidth,
      viewPortHeight = window.innerHeight
    }

   // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined'
    && typeof document.documentElement.clientWidth !=
    'undefined' && document.documentElement.clientWidth != 0) {
       viewPortWidth = document.documentElement.clientWidth,
       viewPortHeight = document.documentElement.clientHeight
    }

    // older versions of IE
    else {
      viewPortWidth = document.getElementsByTagName('body')[0].clientWidth,
      viewPortHeight = document.getElementsByTagName('body')[0].clientHeight
    }

   if(window.orientation != undefined){
      if(window.screen){
          var deviceWidth = window.screen.width;
          var deviceHeight = window.screen.height;
      }else {
          var deviceWidth = viewPortWidth;
          var deviceHeight = viewPortHeight;
      }   
      viewPortWidth = window.orientation == 0 ? deviceWidth : deviceHeight;
      viewPortHeight = window.orientation == 0 ? deviceHeight : deviceWidth ;
      if (navigator.userAgent.indexOf('Android') >= 0 && window.devicePixelRatio) {     
          viewPortWidth = viewPortWidth/window.devicePixelRatio;
          viewPortHeight = viewPortHeight/window.devicePixelRatio;
      }
   }
  
   viewPortWidth = viewPortWidth - getScrollbarWidth();

   return viewPortWidth+'x'+ viewPortHeight;
 }

Getting the browser scrollbar width:
----------------------------------------------------------

function getScrollbarWidth() {
    var outer = document.createElement("div");
    outer.style.visibility = "hidden";
    outer.style.width = "100px";
    document.body.appendChild(outer);
   
    var widthNoScroll = outer.offsetWidth;
    // force scrollbars
    outer.style.overflow = "scroll";
   
    // add innerdiv
    var inner = document.createElement("div");
    inner.style.width = "100%";
    outer.appendChild(inner);       
   
    var widthWithScroll = inner.offsetWidth;
   
    // remove divs
    outer.parentNode.removeChild(outer);
   
    return widthNoScroll - widthWithScroll;
}


alert(getViewport());


Note : subtracting the scrollbar width from the screen width will give absolute screen width

To find your absolute screen size  refer url : http://responsejs.com/labs/dimensions/