Thursday, September 6, 2012

Sorting Multidimensional array in php

This function helps you to sort multidimensional array based on an particular index in that array

sortmulti($resArray,'INDEXNAME','asc',true,true);

Parameter 1 : It is the multidimensional array.
Parameter 2 : It is the index name in multidimensional array.
Parameter 3 : It is to define the ascending or descending order of index name multidimensional array.Acceptable values(asc,desc).
Parameter 4 : If the index has alphsnumeric values then set this as true else false.
Parameter 5 : If you want index name to case sensistive set this as true.

function sortmulti ($array, $index, $order, $natsort=FALSE, $case_sensitive=FALSE) {
  if(is_array($array) && count($array)>0) {
    foreach(array_keys($array) as $key)
      $temp[$key]=$array[$key][$index];
    if(!$natsort) {
      if ($order=='asc')
      asort($temp);
      else
      arsort($temp);
    }
    else
    {
      if ($case_sensitive===true)
        natsort($temp);
       else
        natcasesort($temp);
      if($order!='asc')
        $temp=array_reverse($temp,TRUE);
    }
    foreach(array_keys($temp) as $key){
      if (is_numeric($key))
        $sorted[]=$array[$key];
      else
        $sorted[$key]=$array[$key];
    }
    return $sorted;
   }
  return $sorted;
}

Wednesday, September 5, 2012

Get all the children from an taxonomy term



You have pass the tid into this function which will return all the child terms under it.
<?php
function taxonomy_get_children_all($tid, $vid = 0, $key = 'tid'){
  $c = taxonomy_get_children($tid, $vid, $key);
  $result = array();
  foreach ($c as $t => $d){
    $result[$t] = $d;
    $below = taxonomy_get_children_all($t, $vid, $key);
    if (!empty($below)) {
      foreach ($below as $nt => $nd){
        $result[$nt] = $nd;
      }
    }
  }
  return $result;
}
?>

Group concat in mysql


Using group concat you can concat an column in an single row.But group concat length which has a default value of 1024.if you want to increase the size you have to use the below query & then execute the group_concat query.

SET group_concat_max_len = 200000;

SELECT GROUP_CONCAT(title) FROM node WHERE nid IS NOT NULL LIMIT 100

Natural sorting in MYSQL or Alphanumeric sorting in mysql

MYSQL is still working on Natural sorting.
Here is some quick solution for natural sorting in MYSQL.
This worked perfectly for me.

SELECT name FROM taxonomy_term_data WHERE ORDER BY name+0<>0 DESC, name+0, name

If you have values in name column like below
   2-test
   Test
   12-test
   22-test
   Ascend

& the Output will be:
   2-test
   12-test
   22-test
   Ascend
   Test

Wednesday, July 18, 2012

Compare two array has equal values


for eg;
$restids = array("apple","mango");
$asvals = array("apple","mango");

You can't get the results for equal array check with array_diff function if the second array dynamically changes

You can do on thing for this

if(count(array_diff($restids,$asvals)) == 0 && count(array_diff($asvals,$restids)) == 0){
    return true;
}

This condition will get you the if two array has equal values or not.

Wednesday, July 4, 2012

Use of in condition & between condition in db_select in drupal7


$res = array(‘test1’,’test2’);
$sql_query = db_select(testtable', 's');
$sql_query->fields('s', array('filename', 'filetype', 'createdon'));
$sql_query->condition('s.filetype',$res,'IN');
$datearray = array(‘2011-02-02’,date('Y-m-d'));
$sql_query->condition('s.createdon',$datearray,'BETWEEN');
$sql_query = $sql_query->limit(5)->execute();

Friday, June 15, 2012

Get single field value from a table in drupal7


<?php

$filename = db_query("SELECT uri from file_managed where fid=:fid", array(':fid' => arg(3)))->fetchField();

echo $filename;

?>

Monday, April 16, 2012

Soap call using curl in php

$xml = '<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2003/03/addressing/" xmlns:xsd="http://www.w3.org/2001/XMLSchema/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance/">
<soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<wsse:Security soap:mustUnderstand="1" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
</wsse:Security>
</soapenv:Header>
<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<EnRequest xmlns="http://xmlns.oracle.com/Enterprise/Tools/schemas/EnrollRequest.V1">
<EnRequestDS>
<CG_ENR_REQST class="R" xmlns="http://xmlns.oracle.com/Enterprise/Tools/schemas/EnRequestDS.V1">
<PERSON_ID>2</PERSON_ID>
</CG_ENR_REQST>
</EnRequestDS>
</EnRequest>
</soapenv:Body>
</soapenv:Envelope>';

$wsdlurl = http://192.162.10.4:8080/test/conector

$header = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"Transfer-Encoding:chunked",
"SOAPAction: \"EnrRqst.v1\"",
"Content-length: ".strlen($xml),
);

$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $wsdlurl );
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $xml);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
$ers = curl_exec($soap_do);


* Change soap action in $header based on your need.

Friday, January 20, 2012

PHP Code to remove directory & its files

<?php

function rrmdir($dir) {
  if (is_dir($dir)) {
  if (is_dir($dir)) {
    $objects = scandir($dir);
    foreach ($objects as $object) {
      if ($object != "." && $object != "..") {
        if(filetype($dir."/".$object) == "dir")     rrmdir($dir."/".$object); else unlink($dir."/".$object);
      }
    }
    reset($objects);
    rmdir($dir);
  }
}


?>

Tuesday, January 10, 2012

Theming nodes based on content type in Drupal 7

*The file node.tpl.php is used to theme certain types of nodes.

*This file will be in module/node folder in the root directory.

*To theme individual content types in different ways, you need to create a file node--[type].tpl.php in your theme's folder & copy the content from node.tpl.php to this file, where [type] is the machine readable name of the content type

*only diference between Drupal 6 & Drupal 7 is ,Drupal 7 use two hyphens

node--story.tpl.php
Themes only Story type nodes.

node--forum.tpl.php
Themes only Forum type nodes.

Theming nodes based on content type in Drupal 6

Theming nodes based on content type in drupal 6

*The file node.tpl.php is used to theme certain types of nodes.

*This file will be in module/node folder in the root directory.

*To theme individual content types in different ways, you need to create a file node-[type].tpl.php in your theme's folder & copy the content from node.tpl.php to this file, where [type] is the machine readable name of the content type

node-story.tpl.php
Themes only Story type nodes.

node-forum.tpl.php
Themes only Forum type nodes.