Tuesday, January 22, 2013

Find Odd And Even Number in Php

Find Odd Or Even Number using php Script.Here two way to find this solution just see the below php script.

First : Modulo Operator (%)

$num=10;
if($num%2==0)
{
echo "Even";
}
else
{
echo "Odd";
}  

Second Way :Bitwise Operator '&'
$num=9;
if($num&1)
{
echo "Odd"; 
}
else
{
echo "Even"; 
}

Wednesday, January 9, 2013

Detecting Ajax Request with PHP Script

Here’s a script that  useful to check if a request that comes to a PHP page was made via an Ajax call or a simple form post. This method uses the $_SERVER['HTTP_X_REQUESTED_WITH'] request to determine if data was sent to a specific page using an xmlhttprequest.The script is pretty self explanatory,we are  checking to see if the request was sent via an xmlhttprequest.





Example Script :
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
// If its an ajax request execute the code below
echo 'This is an ajax request!';
exit;
}
//if it's not an ajax request echo the below.
echo 'This is clearly not an ajax request!';

Automatic Load a page Using Ajax

Here we can learn about how to Automatically load page after 5 second using ajax and jquery.First we get little information about Ajax.
AJAX = Asynchronous JavaScript and XML.
AJAX is not a new programming language, but a new way to use existing standards.
AJAX is the art of exchanging data with a server, and updating parts of a web page - without reloading the whole page.


Example script :
Create two files :
1) main file
2) load file
create main.php this is main file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js">
</script>
<script>
 setInterval(
function()
{
$('#content').fadeOut('slow').load('load.php').fadeIn("slow");}, 3000);
</script>
<title>Auto Load Page Div using Jquery - similar to twitter Live Search</title>
</head>
<body>
<div id="content" style="background-color:#ffffcc;font-size:24px;font-weight:bold;width:600px;margin:auto 10px;">
Please wait.</div>
<a href="#" target="_blank">Home</a>
</body>
<html>
The second file is load.php this file load every 5 second.
echo 'This content is loaded  via ajax every 5 seconds ..';

Facebook Share by php script

Share your content into facebook wall.you can use facebook in build code or javascript code or php code.
here we can use php code to share link into facebook wall.its very simple to use.
Just Add Your URL and Title.
  

Example Script:
//msg to share 
 $tt = "hello boss"; 
 //which url you want to share  
$url_p = "http://tutjunction.com"; 
 $url = urlencode($url_p);  
//share by 
 $text = "jaspal rana";  
$title = urlencode($text); 
 //facebook api  
$fb = "http://www.facebook.com/sharer.php?t=".$title."&u=".$url;

<!--click this link to share-->
<a href="<?php echo $fb; ?>">share link</a>

Facebook Like Button Script

Here we learn how to add facebook subscribe into your application or project.Facebook like button code for any url.Its done by facebook javascript library.





Use following code :

<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

//data-href =add you facebook link

<div class="fb-subscribe" data-href="https://www.facebook.com/url" data-layout="box_count" data-show-faces="true" data-width="450">
</div> 
 
Screenshot :