How to add seprate css for IE Browsers in Drupal Theme

It is very normal scenario  that your main css file is not affecting in IE browsers. For that, you will have to create separate css files and add those files in page.tpl.php inside your drupal theme.

Note:- Paste following code inside head tag in page.tpl.php file.
Code for adding css file in page.tpl.php :-

For IE6:-
<!--[if IE 6]>
     <link type="text/css" rel="stylesheet" media="all" href="<?php print $base_url;?>/sites/all/themes/splendapro/css/ie6_splenda.css" />
   <![endif]-->

For IE7:-
<!--[if IE 7]>
     <link type="text/css" rel="stylesheet" media="all" href="<?php print $base_url;?>/sites/all/themes/splendapro/css/ie7_splenda.css" />
   <![endif]-->

For IE8:-
<!--[if IE 8]>
     <link type="text/css" rel="stylesheet" media="all" href="<?php print $base_url;?>/sites/all/themes/splendapro/css/ie8_splenda.css" />
   <![endif]-->

Note:-The code above looks like commented but it is not.So use as it is.
 
For Safari browser:-
<?php    
   if (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE) {
          ?>
      <link type="text/css" rel="stylesheet" media="all" href="<?php print $base_url;?>/sites/all/themes/splendapro/css/safari.css" />
    <?php
    }
?>


Note:- Yo just need to change the css file path in above codes,and use the rest code as it is.

Add more dynamically in PHP

This is very common feature that you have to integrate add more feature in your application.
It could be use in adding input field or adding any div or anything.

Here I am giving steps to use this feature by simple example. In this example, I an dynamically adding First Name and Last Name in PHP form.

1.Use the following code to create dynamic form in PHP and save this file with .php extension.

File Name - index.php

<?php
$count = 0;
?>
<html>
<head>
<script type = "text/javascript" src = "jquery.js" ></script>
<script type = "text/javascript" src = "add_more.js" ></script>
</head>
<form method = "POST" action = "insert.php"> 
<div id = "count_<?php echo $count; ?>">
    FirstName<input type = "textfield" name = "firstname_<?php echo $count; ?>" value = ""><br><br>
    LastName<input type = "textfield" name = "lastname_<?php echo $count; ?>" value = ""><br><br>
</div>
<input type = "hidden" value ="<?php echo $count; ?>" name = "hidden_val" id = "hidden_val">
<input type = "button" id = "add_more" value = "Add More"><br><br>
<input type = "submit" name = "Submit" Value = "Submit">
</form>
</html>

2. Add JQuery library in your working directory. And create one java-script file in same working directory.
   In above example, I have included both JS files. Paste the following code in your java-script file.

File Name - add_more.js

 $(document).ready(function() {
 
  $("#remove").hide();
  $('#add_more').click(function() {
        return addMore();
    });
});

function addMore(){
  var hidden = parseInt($("#hidden_val").val());
  var countNewVal = hidden+1;

$('#count_'+hidden).append('<div id="count_'+countNewVal+'">FirstName<input type="textfield" value="" name="firstname_'+countNewVal+'"><br><br>LastName<input type = "textfield" name = "lastname_'+countNewVal+'" value = ""><br><br></div>');
$("#hidden_val").val(hidden+1);
}
In java-script file, I am increasing count value by 1 after every click on Add More button, And assigning that value to the hidden_val id. 

Note:- Please do not forget to add the jquery library in your working directory.  



How to enable clean url in Drupal

There are two ways to enable Clean URLs feature in Drupal.
1. You can navigate to Drupal admin and inside Site Configuration, click on Clean URLs.
    Enable radio button and click Save Configuration.

2. This way of enabling Clean URLs is most important. Because sometimes after installing new Drupal instance, By    default Clean URLs feature will not work. To enable it, follow these steps.        
  • Go to the Apache configuration file in following path:-                                                                      C:\wamp\bin\apache\Apache2.2.11\conf  //change the path according to your working directory
  •  Now open httpd.conf file. Search for mod_rewrite.so keyword and enable the same line by removing # in front of that. 
  • Restart Apache server and now again navigate to clean url in Drupal admin, You will see that clean url feature is enable now. You can enable radio button and Save Configuration.

How to export database dump from mysql console

Export Database:-

If the size of the DB dump is large than instead of using PHPMYADMIN for exporting database, you can go for the mysql console.
Here I am mentioning  steps to export the Database:-
1. Start the Server which ever you are using. For examples wamp, xamp, lamp.

2. Open Command Prompt(cmd) not Mysql Console.

3. Now run the following cammand and change the directory to following path.
    C:\>cd wamp\bin\mysql\mysql5.1.36\bin // please check your working directory(C or D)
                                                                         and change accordingly in path.
4. Hit Enter. You will see path like this.
    C:\wamp\bin\mysql\mysql5.1.36\bin>

5. Run following command.
   mysqldump -u username -p db_name>sql_file_path_name
   For Ex: mysqldump -u root -p hemant>D:\sqlfile.sql  
   // You do not need to create sql file in any drive. Just mention the path in command line like above example, Sql file will be created automatically.

6. Hit enter, It will ask for the password.if you do not use password for mysql, just leave it and hit enter.

Note :- Please wait till the cursor is blinking(when database size is huge). 

How to import database dump from mysql console

Import Database:-

If the size of the DB dump is large than instead of using PHPMYADMIN for importing database, you can go for the mysql console.
Here I am mentioning  steps to import the Database:-
1. Start the Server which ever you are using. For examples wamp, xamp, lamp.

2. Open mysql console. It will ask for the password. Enter the password. If you do not use password, leave blank, and just hit enter.

3. Run the following command.
    mysql>show databases; // It will list all the databases

4. Now for selecting any particular database, run the following command.
    mysql>use databasename; //Mention you db name here

5. Now Run last command to import database.
    mysql>source path_to_sql_file.sql; // Ex.  mysql>source D:\my_sql_file.sql 

Submit Hanlder In Custom Drupal Forms

How to invoke two different functions from hook_form() in Drupal?
This can be achieved using submit handler.

Here is the explanation with code.
1:-Use following code in your hook_form() function:-

$form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',

        //here we are defining the first submit handler and
          calling one submit function
        '#submit' => array('test_form_submit'),
        );  


$form['enter'] = array(
        '#type' => 'submit',
        '#value' => t('Enter'),

        //here we are defining the second submit handler and
          calling second submit function
        '#submit' => array('enter_submit'),
        );

    
2:- By using  #submit =>  array('function_name'),you can call N number of functions from custom one form.
 

Different Page Template for Different Content Type in Drupal

This is one of the most frequently asked question in the interviews that
how can you provide different style(in terms of HTML & CSS) to any particulate page or any particulate content type in drupal?

 Here is the step by step answer for this question.
1:-First go to your themes folder(whichever theme is currently active) and search for this function in template.php file.
phptemplate_preprocess_page(&$vars)

2:-Now Copy the following code inside this phptemplate_preprocess_page(&$vars)
function:-
   if (isset($vars['node'])) {
        // This code looks for any page-custom_content_type.tpl.php page
        $vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type); 
  }  

In the above code,I am checking the value for node,if its value is set(means if we are opening any node in our drupal site),than this if condition will be true and the code inside will get execute.

We are using $vars['template_files'] array for loading the template file from themes folder.

3:-Next step is to create new content type(or you can use existing content type).
    For example I am creating product content type.

4:-Now create one content of product content type.

5:-Create one file named page-product.tpl.php inside themes folder.Here product is our custom content type.

6:-Once we click on any content of product content type,the above will search for the template file with page-product.tpl.php name.If file is present inside themes folder,then it will take this template file other wise it will style take from page.tpl.php.    

7:-In this page-product.tpl.php file,we can provide the different style(HTML & CSS).

These Steps are useful for particular content type.You can provide different template to particular  page also by adding this code:- 
$vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->path);
Here we have replaced $vars['node']->type with $vars['node']->path(for specific path).

And create the template file with the name of your page url. For examples if you want different template for
test_image url.So create file page-test_image.tpl.php