Monday, November 28, 2011

Change to hook_nodeapi in drupal 7

hook_nodeapi() in Drupal 6 became (see Node API page) hook_node_delete(), hook_node_insert(), hook_node_load(), hook_node_prepare(), hook_node_prepare_translation(), hook_node_search_result(), hook_node_presave(), hook_node_update(), hook_node_update_index(), hook_node_validate(), and hook_node_view() in Drupal 7.

Example in Drupal 6 :

/**
* Implements hook_nodeapi().
*/
function hook_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'load':
// Load a book.
}
}

Example in Drupal 7 :

/**
* Implements hook_node_load().
*/
function hook_node_load($nodes, $types) {
// Load a book.
}


/**
* Implements hook_node_update().
*/

function hook_node_update($node) {
db_update('mytable')
->fields(array('extra' => $node->extra))
->condition('nid', $node->nid)
->execute();
}

/**
* Implements hook_node_insert().
*/

function hook_node_insert($node) {
db_insert('mytable')
->fields(array(
'nid' => $node->nid,
'extra' => $node->extra,
))
->execute();
}

Thursday, November 24, 2011

Check whether text contain html tags using php

There are two ways you can do it, one is using a regular expression and the other is comparing strings.

First the regular expression approach:

if (preg_match("/([\<])([^\>]{1,})*([\>])/i", $string)) {
echo "string contains html";
}

Second a comparing string approach:

if(strlen($string) != strlen(strip_tags($string)){
echo "string contains HTML";
}

Wednesday, November 23, 2011

Usage of Like query in drupal6

You have to out two percentage between %s

$sql = "SELECT title FROM node WHERE type='%%%s' AND title LIKE '%%%S%%'";
$type = "type to use in query";
$title = "title to use in query";
$result = db_result(db_query($sql, $type, $title));

Export mysql schema alone in command line

MysqlExport Only schema :

Go to mysql bin directory in command prompt
mysqldump --no-data --tables –u username –p dbname --routines --triggers > c:/file.sql
Press enter
Enter Password:
Enter your password
You are done with it