Monday, December 19, 2011

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;