Pages

How to Backup Database

Class to back up entire databases and email them out, or individual tables.

Detection iPad Visitor Using .htaccess

Of course, the iPad is a pretty large screen and a fully capable browser, so most websites don't need to have iPad specific versions of them. But if you need to, you can detect for it with .htaccess

RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://ipad.yourdomain.com [R=301]
 
This will redirect iPad users to a URL you specify. This is probably the best way to do it (assuming you are running an Apache server), but if you aren't, there are PHP and JavaScript methods here.

Automatic Display Copyright Year

The following is a trick to display the copyright year ini php

Current year only

&copy; <?php echo date("Y") ?>

With start year

&copy; 2008-<?php echo date("Y") ?>

Start date with error protection

<?php function auto_copyright($year = 'auto'){ ?>
   <?php if(intval($year) == 'auto'){ $year = date('Y'); } ?>
   <?php if(intval($year) == date('Y')){ echo intval($year); } ?>
   <?php if(intval($year) < date('Y')){ echo intval($year) . ' - ' . date('Y'); } ?>
   <?php if(intval($year) > date('Y')){ echo date('Y'); } ?>
<?php } ?>
 
Usage:
<?php auto_copyright(); // 2014?>

<?php auto_copyright('2005');  // output 2005 - 2014 ?>

JavaScript call and apply

JavaScript call and apply are functions that are used to call function within the context of another function. There is a fundamental difference between the two, call() takes parameters as a name while as apply() accepts parameters as an array.

I will show how to use call and apply to change the background of a page in JavaScript. Usually you won’t do such a basic thing as changing background via call or apply methods, but I wont to touch the basics of apply and call and show how to use them in function context. Otherwise you would never want to change the page background or for that matter any style property with JavaScript call and apply methods.

JavaScript call() function with Example.
   
function changeBackgroundColor(color) 
{
            this.style.backgroundColor = color;
            this.innerHTML = "<h1>Body Background Changed Via JavaScript Call Function</h1>";
 }
 var bodyBackground = document.getElementsByTagName("body")[0];
 changeBackgroundColor.call(bodyBackground,"#990000");


Quite simple, we use a function changeBackgroundColor(), pass it a parameter color. In this function we set the bodybackground with the follow statement
this.style.backgroundColor = color;
we also set the innerHTML of body to be a h1 tag with some descriptive text.

Now to call changeBackgroundColor function we use call like changeBackgroundColor.call(bodyBackground,”#990000″);

As I said call takes parameters as name, the bodyBackground is the node that we want the background to be changed. In our case body tag, the we pass the color that page background should take.

See the Demo of the above example in jsFiddle:

How to get the text value of a clicked link

 
In your case I wouldn't use the text of the link, as it's possible it may change in the future (ie. you need to translate your website). The better solution is to add custom attribute to links:

<div id="my-div">
  <div><a href="#" sectionId="someId1">tag 1</a></div>
  <div><a href="#" sectionId="someId2">tag 2</a></div>
</div>
 
And then put the id of the hidden tag there, so you and up with:

$('#my-div a').click(function() {
  var sectionId = $(this).attr('sectionId');
  $('#' + sectionId).show();
  return false; // return false so the browser will not scroll your page
});
 
:) 

Java Script Right Click Disable

Wanna script or snippets to disable right click on your website? check it out..

Java Script Right Click Disable
 
<script language="javascript"
type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script type="text/javascript" language="javascript">
$(function() {
 $(this).bind("contextmenu", function(e) {
                       e.preventDefault();
            });
        });
</script>

PHP / MySQL select data and split on pages

This tutorial is going to show you how to SELECT data from a MySQL database, split it on multiple pages and display it using page numbers.

PHP & Javascript

All the PHP code will execute on the server before the page is sent to the client, meaning all of the PHP calls have all been replaced by whatever text they returned once the JavaScript is able to execute.

<script>
document.write("<?php echo 'hello!'; ?>");
</script>
 
<?php 
echo "<script language=\"JavaScript\">"; 
echo "alert(\"Hello World\");"; 
echo "</script>"; 
?>
 

<?php $phpvar=1; ?>
<script type="text/javascript">
jsvar = 3;
//Assign jsvar javascript variable value to php variable $phpvar
'<?php $phpvar="<script>document.write(jsvar)</script>"; ?>';
</script>
<?php echo $phpvar; ?>
 
‘Loop’ Example
 
<script type="text/javascript">
var counter = 0;
for(i=0;i<10;i++){
counter++;
}
</script>

<?php 
$phpvar= "<script>document.write(counter)</script>"; 
echo $phpvar;
?>
 
‘Session’ Example

<?php session_start(); $_SESSION['nume']='Sanat';?>
<!-- HTML code -->
<script type="text/javascript">
alert("Welcome <?php echo $_SESSION['nume']; ?>");
</script>
 
‘Switch’ Example

<script type="text/javascript"> 
function test(){ 
 document.getElementById("php_code").innerHTML="<?php  
  $w="do"; 
  switch($w){ 
  case 'do': echo "You will be logged in?"; break; 
  case 'loguot': echo "You will be logged out?" ; break;
  } 
 ?>"; 
} 
</script> 

<a href="" onclick="test(); return false;"> test </a> <br>
<span id="php_code"> </span>
 
Pass the value within url.

<?php 
    if(isset($_GET[action])){
 // Retrieve the GET parameters and executes the function
   $funcName  = $_GET[action];
   $vars   = $_GET[vars];
   $funcName($vars); 
  } else if (isset($_POST[action])){
   // Retrieve the POST parameters and executes the function
   $funcName  = $_POST[action];
  $vars   = $_POST[vars];
  $funcName($vars);    
  } else {   
  echo "<INPUT NAME='click_me' TYPE='button' ONCLICK='javascript:javaFunction()' VALUE='Click Me'>";
  }
  
   function phpFunction($v1){
 // makes an array from the passed variable  
 // with explode you can make an array from string. 
 $varArray = explode(",", $v1); 
  
 echo "First Value: $varArray[0] <BR>";
 echo "Second Value: $varArray[1]<BR>"; 
   }
  ?> 
 
  <SCRIPT language="javascript">
   function javaFunction(){
 // In the varArray are all the variables you want to give with the function
 var varArray = new Array();
 varArray[0] = "Hello";
 varArray[1] = "World";
   
 // the url which you have to pass an action to the GET- or POST-variable
 var url="<?php echo $_SERVER[PHP_SELF];?>?action=phpFunction&vars="+varArray;
 
 // Opens the url in the same window
    window.open(url, "_self");
   }
  </SCRIPT>
Output :
First Value: Hello
Second Value: World

htaccess to redirect root domain to a subfolder

If you want to redirect your domain yourdomain.com to yourdomain.com/blog then you need to add below code into a .htaccess file in your public_html folder.

Add the following lines to that file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?YourDomain.com$
RewriteRule ^(/)?$ blog [L]

Htaccess code to redirect from http to www

Add following code on your htaccess file to redirect http://yourdomain.com to http://www.yourdomain.com. Just change your domain name there, no other changes are required.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]