Tuesday, December 27, 2011

Clear cache programmatically in Drupal 7



This will reduce the work by going to performance settings page (admin/settings/performance) & clicking "cleared cached data" by scrolling down

But make sure that this page is only accessed by administrator

For Drupal 7 is same as Drupal 6

<?php
  drupal_flush_all_caches();
  drupal_set_message('cache flushed.');
?>

Clear cache programmatically in Drupal 6


This will reduce the work by going to performance settings page (admin/settings/performance) & clicking "cleared cached data" by scrolling down

But make sure that this page is only accessed by administrator


For Drupal 6 create page with the following code:

<?php
  drupal_flush_all_caches();
  drupal_set_message('cache flushed.');
?>

Clear cache programmatically in Drupal 5



This will reduce the work by going to performance settings page (admin/settings/performance) & clicking "cleared cached data" by scrolling down

But make sure that this page is only accessed by administrator


<?php
// only allow site administrators to visit this page:
if (!user_access('administer site configuration')) {
  drupal_not_found();
}else {
  drupal_clear_css_cache();
  $tables = array('cache','cache_content','cache_filter','cache_menu','cache_page',  'cache_views');
  foreach ($tables as $table) {
    cache_clear_all('*', $table, TRUE);
  }
  drupal_set_message('Cache cleared.');
  drupal_goto();
}
?>

Monday, December 19, 2011

Get block content in drupal 6 , display block in drupal 6


In drupal 6 it was

<?php

$block = module_invoke('views', 'block', 'view', 'map-block_1');
print $block['content'];

?>

arg 1 : module name
arg 2 : hook name like block
arg 3 : $op of hook_block e.g. info, view, configure
arg 4 : id or delta of the block e.g 30, map-block_1

Get block content in drupal 7 , display block in drupal 7


If you want to view blocks in drupal 7, you have to use

<?php

$block = module_invoke('views', 'block_view', 'map-block_1');
print $block['content'];

?>

arg 1 : module name
arg 2 : hook name like block_view, block_info
arg 3 : id or delta of the block e.g 30, map-block_1

variable_set in drupal

If you are going to save a single value then:

$set = variable_set(‘name’,’value’);

If you are going to save an array then:

$array = array(‘value1’,’value2’);

$seralizedvalue = serialize($array)

$set = variable_set(‘name’,$seralizedvalue);

variable_get in drupal

$set = variable_get (‘name’,’defaultvalue’);

Variable get will retrieve the value from drupal variable table

If  ‘name’ is not there in drupal variable table this function will return the ’defaultvalue’.

This function will return serialized data.

If it return more than one value inside the serialized data then use unserialize() function to convert the serialized data into array.

If it contains only one value then you can fetch it directly i.e. the returned value will get saved to $set directly.

Monday, December 12, 2011

Remove last character of a string in javascript


//Input : "Kumar,Raj,Ram,"
//Removes the last charecter comma from the string

var ins  =  'Kumar,Raj,Ram,';
ins = ins.substring(0, ins.length-1);
alert(ins);

//Output = "Kumar,Raj,Ram"

in_array in javascript


 /*
   *PHP in_array function  in  javascript
   */
   function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array
    //
    // *     example 1: in_array('Raj', ['Kumar', 'Tom', 'Raj']);
    // *     returns 1: true
    // *     example 2: in_array('Hari', {0: 'Kathir', Hari: 'Passat', 1: 'Raj'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
     var key = '',
        strict = !! argStrict;

     if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
     } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
     }

     return false;
   }

Wednesday, December 7, 2011

Convert xml string into an array using php

<?php

   $string = <<<XML
   <?xml version='1.0'?>
   <document>
   <title>Forty What?</title> 
   <from>Joe</from>
   <to>Jane</to>
   <body>
   I know that's the answer -- but what's the question?
   </body>
   </document>
   XML;

   $xml = simplexml_load_string($string);

   print_r($xml);

?>

Output:

SimpleXMLElement Object ( [title] => Forty What? [from] => Joe [to] => Jane [body] => I know that's the answer -- but what's the question? )

Select & unselect all check box using jquery or Check & uncheck all check box using jquery

$(document).ready(function() {

$("#checkall").click(function()
{
var chk_sts = this.checked;
$('input:checkbox[name=checkboxname]').each(function(i)
{
this.checked = chk_sts;
});
});

});

* Include latest jquery js file
* "#checkall" is the id to check box where you want a click to check all other check boxes

Friday, December 2, 2011

Get row count in Drupal 6 , Get row count in drupal 6

The below function has to be written after the execution of query using db_query();

db_affected_rows()

function will return the number of rows the query return;

Thursday, December 1, 2011

Get checked check box value in jquery

IF you have only one check box group in the form

$('input:checkbox:checked').each(function(i){
    alert($(this).val());
});


If you have many check box group you have to mention the group name

$('input:checkbox[name=checkboxname]:checked').each(function(i){
    alert($(this).val());
});