Friday, 28 October 2011

Facebook login in drupal

In drupal Facebook connect module used to add Facebook login.

This module also used for twitter login.

This video show the basic integration of Facebook connect module in drupal

 
INSTALLATION

1.  Copy facebook connect module to sites/all/modules folder

2. Create new facebook app in this url https://developers.facebook.com/apps

3. Download php-sdk libraries from  http://github.com/facebook/php-sdk/tarball/v2.1.1

4. Copy this php-sdk to facebook connect module folder
   <fbconnect folder>/facebook-php-sdk/src/facebook.php
5.Enable fbconnect module

6. Enter facebook app key and id in admin/settings/fbconnect page.
 

Tuesday, 25 October 2011

Upload file extension restriction in jquery

Image upload field only accept following extension gif, png, jpg,jpeg.
<form action="" method="post" enctype="multipart/form-data">
Upload Image:<input type="file" name="image" id="image_id"  />
</form>
following code used to limit the upload file extension
<script type="text/javascript">
$(function() {
   $('#image_id').change( function() {
    var ext = $('#image_id').val().split('.').pop().toLowerCase();
    if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
      $('#image_id').val("");
        alert('Please Enter the files in the format jpeg,gif,png');
       
    }
   });
});
</script>

Sunday, 23 October 2011

Create node theme for content type in drupal

The node.tpl.php is a default theme for all nodes.
If need to change node theme for specific content type follow following steps

1. If u create node theme for page content type, create node-page.tpl.php in your template folder.
2. Copy node.tpl.php to node-page.tpl.php , Importent do not delete default node.tpl.php file
3. Change the theme display for your needs
4. Use variables node.tpl.php
5. If u create node theme for movie page content type, create node-movie_page.tpl.php file.

For more: Theming node by content type

Saturday, 22 October 2011

How delete own comment in drupal

In Drupal user can not delete own comment
So need Comment Delete module.
  1. Unzip and copy this module in sites/all/modules folder.
  2. Enable comment delete module.
  3. Enable required permission in user permission page

How add facebook like button


1. Create your App in facebook https://developers.facebook.com/apps

2.Copy and paste following code in your site after opening body tag and enter your App id
<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&appId=YOUR APP ID";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
3.Paste following code in which place you need like button
<div class="fb-like" data-href="http://phpand.blogspot.com/" data-send="true" data-width="450" data-show-faces="true" data-font="arial"></div>
4. For more refer https://developers.facebook.com/docs/reference/plugins/like/

Friday, 21 October 2011

How create facebook login

1. Go https://developers.facebook.com/apps and click create new App

2. After create your App, you get App ID/API Key and App Secret code.



3. Enter your website name to include Facebook login


4.Add following code in your login page
<html>
    <head>
      <title>My Facebook Login Page</title>
    </head>
    <body>
      <div id="fb-root"></div>
      <script>
        window.fbAsyncInit = function() {
          FB.init({
            appId      : 'YOUR_APP_ID',
            status     : true, 
            cookie     : true,
            xfbml      : true
          });
        };
        (function(d){
           var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
           js = d.createElement('script'); js.id = id; js.async = true;
           js.src = "//connect.facebook.net/en_US/all.js";
           d.getElementsByTagName('head')[0].appendChild(js);
         }(document));
      </script>
      <div class="fb-login-button" data-perms="email,user_checkins">
        Login with Facebook
      </div>
    </body>
 </html>

5.For more https://developers.facebook.com/docs/guides/web/

Monday, 17 October 2011

How get current username in drupal

Following code used to get current username
<?php
       global $user;
       print_r($user->username);
?>

To get user details
<?php
       global $user;
       print_r($user);
?>

Saturday, 15 October 2011

Create user gallery in drupal

Following module used for create user photo gallery
  1. Views
  2. CCK
  3. Image field
  4. Nodereference_url
  5. Views_attach
  6. CSS_injector

Wednesday, 12 October 2011

Create facebook share URL

Share your article in Facebook following code is used

<a href="http://www.facebook.com/sharer.php?u=<url to share>
&t=<title of content>" target="_blank"> link or image</a>
  • url to share – what URL  you want to share to Facebook
  • title of content – the title of what you are sharing
<a name="fb_share" type="button" 
   share_url="YOUR_URL">Compartir en Facebook</a> 
<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" 
        type="text/javascript">
</script> 
For more http://developers.facebook.com/docs/share/

Tuesday, 11 October 2011

find odd or even number in php

If number divided by 2 and remainder is zero , that number is even number.
8 is divided by 2 and reminder is zero , this is even number
8/2 = 4
4/2 = 2
2/2 = 0 (remainder)

5 is odd number

5/2     = 2.5
2.5 /2 = 1.25 (remainder)


if remainder = 0 , that number is even number

In php % (Modulus result is Remainder of $a divided by $b ) operator used to find remainder value

<?php

if (($i % 2) == 0 ) {
  echo "$i is even";
} else {
  echo "$i is odd";
}

?>
Another method to find odd or even number in php, by using Logical AND operator
<?php

function is_odd($num)
{
  return( $num & 1 );
}

if (is_odd(8)) {
    echo "8 is odd";
} else {
    echo "8 is even";
}

Output: 8 is even 

?>
Number 8 binary value is 1 0 0 0
number 1 binary value is  0 0 0 1
AND                              ---------
                                    0 0 0 0

return false so output is 8 is even 

Thursday, 6 October 2011

Print array values in php


To print array values following method are used. For your need use correct method

This code will display the text "apple"

$fruit[0] = "apple";
$fruit[1] = "banana";
$fruit[2] = "orange";
echo $fruit[0]; 


This display all elements in the array. but u need to count a array

for ($i=0;$i<=2;$i++){
    echo $fruit[$i]; 
}

Using count array

$count = count($fruit);
for ($i=0;$i<=$count;$i++){
    echo $fruit[$i];
}

This is a best way to display all the element in the array

foreach ($fruit as $value){
    echo $value;   
}

Difference between get and post method


Get and Post Method used for pass data to data processing page, and are used for form data handling.

GET
POST
Get method transfer data through URL 
http://www.phpand.blogspot.com/index.php?fname=phpand&age=20
In post method data transfer will not appear in the browser
Get method have character restriction depends on browser. In old browser URL length is very less. 
Post method is no limit. The post method depends on post_max_size in the php.ini file
Get method data will be visible for every one(displayed in the browser's address bar)
Post data not visible and secure.
Get method data transfer speed is high
Data transfer speed is less.

Phone number validation in php


In phone number validation mostly php regular expression was used.

Example1  91-9874563210
if(ereg("^[0-9]{2}-[0-9]{10}$", $phone_number)) {
      echo "valid phone number";
}
else {
       echo "invalid phone number";
}


Example2  555-655-7516
if(ereg("^[0-9]{3}-[0-9]{3}-[0-9]{4}$", $phone_number)) {
       echo "valid phonenumber";
}
else {
      echo "invalid phonenumber";
}
  

Differencce between strstr and stristr function


strstr and stristr are the same functionalites that used to find the first occurrence of the string.
Difference is strstr is used to find the first occurrence of the string with case sensitive but stristr is used for case-insensitive searches.

Syntex:
strstr($string , $needle string , $before needle);
$before needle :
If TRUE, strstr() returns the part of the haystack before the first occurrence of the needle.

Example :
<?php
    $email  = 'name@example.com';
    $domain = strstr($email, '@');
    echo $domain; // prints @example.com

    $user = strstr($email, '@', true); // As of PHP 5.3.0
    echo $user; // prints name
?>

Wednesday, 5 October 2011

Show page only for owner in elgg

In elgg tool following page pg/friends/user_name only display only owner.

For example logged in user name is john he will access pg/friends/john page and he will not access pg/friends/balaji page.

This done by using following code
if($_SESSION['user']->guid != page_owner()){
       forward($_SERVER['HTTP_REFERER']);
}  

Tuesday, 4 October 2011

Find second max salary in MySQL

To find max second salary without using sub-query
SELECT salary FROM `salary_table` GROUP BY `salary` DESC LIMIT 1 , 1;

By using sub-query, get all second salary records
SELECT max(salary) FROM `salary_table` WHERE salary = (SELECT max(salary)FROM `salary_table`);

Find register actions in elgg

In elgg to find your plugins register action , to use the following code
global $CONFIG;
echo "<pre>";
print_r($CONFIG->actions);

Difference between htmlentities and htmlspecialchar

htmlentities         — Convert all applicable characters to HTML entities.
htmlspecialchars — Convert special characters to HTML entities.

Monday, 3 October 2011

Add captcha in elgg form

If you need to add captcha  in your form, to follow the below steps

Step 1:

In your form add captcha image and text box
$form_body .= elgg_view('input/captcha');

Step 2:

There two method used to check the capcha

Method 1
Open mod/capcha/start.php and
add your action name in $retrunvalue[] array

function captcha_actionlist_hook($hook, $entity_type, $returnvalue, $params)
    {
        if (!is_array($returnvalue))
            $returnvalue = array();
           
        $returnvalue[] = 'register';
        $returnvalue[] = 'user/requestnewpassword';
        $returnvalue[] = 'your action name';
           
        return $returnvalue;
    }

Method 2

In your module form action file call the captcha_verify_action_hook
captcha_verify_action_hook();

Sunday, 2 October 2011

Add syntax highlighter in drupal

<?php 
if(ereg("^[0-9]{2}-[0-9]{10}$", $phone_number)) {
      echo "valid phone number";
}
else {
       echo "invalid phone number";
}
?>

To activate syntax highlighter in your drupal site to follow the below steps

1. Download syntax highlighter module to following link http://drupal.org/project/syntaxhighlighter
    and extract to sites/all/modules directories

2. The module required following Syntax highlighter javascript library
     download and  extract sites/all/libraries directories

3. Now enable Syntax highlighter module

4. Enable the Syntaxhighlighter filter in an input format Site configuration > Input formats > Configure




5. Go to admin/settings/syntaxhighlighter to configure syntaxhighlighter module


6. If u need to highlight code to use following pre tag

<pre class="brush: js; highlight: [1, 5]; html-script: true">
     your javascript and html code here
</pre>

<pre class="brush: php; highlight: [1, 5]; html-script: true">
      your php and html code here
</pre>

 highlight: [1, 5] is used to highlight the code lines 1 and 5