programmatically create or add new Taxonomy Terms in drupal

To create a new taxonomy term through PHP you can use taxonomy_save_term(&$form_values) which is mainly intended for use as a helper function to by the real drupal taxonomy add term form.
The relevant term details are passed as parameter and the Term Id of the newly created taxonomy term is placed back in this array and can be accessed by the calling code.

here I explain small code to assign taxonomy term to node if taxonomy is new , first you save in taxonomy in db.

/**
* Save Node
*/
foreach($arrNodeIds as $intNodeId){
$objNodeInfo = node_load($intNodeId);
$arrTermsIds = getTermIds($_POST['txt_tag_val_'.$intNodeId]);
$objNodeInfo->taxonomy = $arrTermsIds;
node_save($objNodeInfo);
}
/**
* See If the term exists in the chosen vocabulary and return the tid; otherwise, add a new record.
* @param $strTerms
* - Terms Name
* @return
* - Terms Ids
*/
function getTermIds($strTerms){
$arrTerms = explode(",",$strTerms);
if(!is_array($arrTerms)){
$arrTerms[0] = $strTerms;
}
foreach ($arrTerms as $strTerm){
if(strlen($strTerm) > 0){
$possibilities = taxonomy_get_term_by_name(trim($strTerm));
$intTermTid = NULL;
foreach ($possibilities as $possibility){
$intTermTid = $possibility->tid;
}
// Check new term , If yes then insert this new term in db
if (!$intTermTid){
$arrParam = array('vid' => 2, 'name' => $strTerm);
$status = taxonomy_save_term($arrParam);
$intTermTid = $arrParam['tid'];
}
$arrTermIds[] = $intTermTid;
}
}
return $arrTermIds;
}

/**
* add a new record as Term of Texonomy.
* @param $arrParam
* - param (vid , name)
* @return
* - status
*/
function addTaxonomyTerm($arrParam){
$vid = $arrParam['vid'];
$name = trim($arrParam['name']);
return taxonomy_save_term($form_values);
}

Create block by drupal code

If you most likely want to generate a block using code / programming , it is simple develope following code for that as I explain here.

Once you know the module and the delta, It is simple for creating the block.
The trick is you need to send an object with the right parameters. The code below should get you started. Remember this code may be different in Drupal7

/**
* Load drupal Block By Delta & Module
*
* @param $strModule
* - Module Name
*
* @param $intDelta
* - Module Name
*
* @return
* - Themed Block HTML
*/

function loadDrupalBlock($strModule, $intDelta) {
$block = new stdclass(); // empty object
$module = $strModule;
$delta = $intDelta; // could also be a string

$array = module_invoke($module, 'block', 'view', $delta);
if (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->$k = $v;
}
}
$block->module = $module;
$block->delta = $delta;
return theme('block', $block);
}


This is calling by this way
$staticBlockHtml = loadDrupalBlock('block', 7);

Delete many thousands of nodes

We recently had reason to delete over a thousand Drupal 6 nodes of the same type,
but we wanted to leave all other content on the site intact.

What do you do when you want to delete multiple nodes from a drupal site?
You go to admin » content and then delete nodes one by one or delete up to 50 nodes at one time?

In this example we will delete all nodes of a particular type (page), It is quick way to delete


$node_type = 'image_album'; //fetch the nodes we want to delete
$result = db_query("SELECT nid FROM {node} WHERE type='%s'",$node_type);
while ($row = db_fetch_object($result)){
node_delete($row-?>nid);
$deleted_count+=1;
}
//simple debug message so we can see what had been deleted.
drupal_set_message("$deleted_count nodes have been deleted");

conformation alert using javascript

Sometimes it is useful to display a confirmation message before processing any action. Some application often asks for confirmation before doing something irreversible, like deleting content. 
the user to conform once again the deleting process. Here the user can change his mind and can cancel the operation or conform his action by clicking the submit button.

function confirmDelete(item){
 var strMsg = "Are you sure you want to delete this "+item+" ? ";
 var intAgree = confirm(strMsg);
 if (intAgree)
  return true ;
 else
  return false ;
}


Pass an array from an HTML form using Hidden field

It is simple way to pass your array using hidden field of form and get to another side  where you want to use this array's value


Snippet

Create Array

$arrNodeIds = array();
foreach ($data as $k => $objNode) {
$arrNodeIds[] = $objNode->nid;
}

Assign array to hideen field of form
<form method="post" action="/ess_upload/save_images">
<input type="hidden" name="hdnArrNodeIds" value="">
</form>

Get value from other side

$arrNodeIds = unserialize(urldecode($_POST["hdnArrNodeIds"]));
foreach($arrNodeIds as $intNodeId){
    echo $intNodeId;
}

Check Node is Exists In Drupal

It is good way to check Node is exists or not bellow example code would be helpful 

In this example , If node is exists in our database, then It will be deleted. otherwise give a error message and redirect to perticular page.

Example :
  $bolIsNodeAvail = node_load(array('nid' => $intNodeId)); 
  if($bolIsNodeAvail != false)
  {
        node_delete($intNodeId);  
  }else{
        drupal_set_message(t("node id is not exists."), 'error');  
  }
  drupal_goto('ess_upload/album_images');
 

Insert and Update Record using drupal_write_record()

It saves a record to the database based upon the schema.

you can simply call the new drupal_write_record function telling it what database table to write to and the information to write and the function will do the rest. SO you'll find yourself writing less and less of those long db_query insert and update commands!

Insert Record To Table
This would insert all the data in the $node object into the node database table.

drupal_write_record('node', $node);

Example :
$objFile = new stdClass();
$objFile->uid = $uId;
$objFile->filemime = $arrFile['str_file_mime'];
$objFile->filesize = $arrFile['int_file_size'];
$objFile->status = FILE_STATUS_PERMANENT;
$objFile->timestamp = time();
drupal_write_record('files', $objFile);


Update Record In Table
This would update the node in the node database table with the data in the $node object 
where nid is equal to the $node->nid

drupal_write_record('node', $node, 'nid')


Example :
$strFileExtension = getFileExtension($arrFile['str_orgin_file_name']);
$strFileNewName = $objFile->fid.".".$strFileExtension;
$objFile->filename = $strFileNewName;
$objFile->filepath = $arrFile['str_rel_file_path'].$uId.DIRECTORY_SEPARATOR.$strFileNewName;
drupal_write_record('files', $objFile,'fid');