Graham's Blog

Categories:

Blog Posts by Category

<?php
$vid = 4; /* <---- put correct vocabulary ID here */
$terms = taxonomy_get_tree($vid);
 
print "<ul>";
foreach ( $terms as $term ) {
//echo '<pre>'; print_r($term); echo '</pre>';
if(taxonomy_term_count_nodes($term->tid)) {
print "<li>".
l($term->name,'taxonomy/term/'.$term->tid, array('title' => $term->name)).
"</li>";
} /* end foreach */
}
print "</ul>";
?>

Convert a string / number to float

function convert_to_money( $s, $dec=2 )
{
  // converts a string / number to float  
  $f = floatval( $s );
  if( $f<0 ): // if negative
    $p = array( '($', ')' ); // formatting negative numbers
    $f = str_replace( '-', '', $f ); // removes the minus sign
  else: // positive no.
    $p = array( '$', NULL );
  endif;
  return $p[0].number_format( $f, $dec ).$p[1];
}

Recursively Delete all DS_Store Files

find . -name *.DS_Store -type f -exec rm {} \;

Send standard output to a file

echo hi > file.txt

single ‘>’ replaces file

echo hi >> file.txt

double ‘>>’ appends to file

Send error output to text file

somecommandthatmakeserrors 2> error_log.txt

Remove colons from form labels

// remove colons from form labels template.php
function phptemplate_form_element() {
  $arg = arg();
  $args = func_get_args();
  return preg_replace('@(\.*):\s*(<span.*?>.*?</span>)?\s*(</label>)@i', '$1$2$3',
    call_user_func_array('theme_form_element', $args));  
}

Tar list of files

cat file-list.txt | xargs tar rvf archive.tar

Export list of files with a certain extension

 find . -name '*.html' > ~/Desktop/filecountmm.txt

Add body classes via template.php

function phptemplate_body_class($left, $right) {
  if ($left != '' && $right != '')
  {
    $class = 'sidebars';
  }
  else
  {
    if ($left != '')
    {
      $class = 'sidebar-left';
    }
    if ($right != '')
    {
      $class = 'sidebar-right';
    }
  }
 
  if (isset($class) && arg(0) == 'admin')
  {
    $class .= " admin";
  }
  elseif (arg(0) == 'admin')
  {
    $class = "admin";
  }
 
  if (isset($class))
  {
    print ' class="' . $class . '"';
  }
}