Search This Blog

Tuesday, 20 February 2018

Basic code for drupal 7

To apply database simple query for getting node title.
Query: To print node title with term id 2

Table format is as follow:
Node Table:


taxonomy_index Table:


taxonomy_term_data Table:


Result:




<?php
function general_menu() {
 $items['first'] = array(
   'title' => 'First Page',
   'page callback' => 'first_page',
   'access callback' => TRUE,
  );
 return $items;
}
function first_page(){
 $query = db_query("SELECT taxonomy_index.nid
      FROM taxonomy_index
      JOIN taxonomy_term_data ON taxonomy_index.tid = taxonomy_term_data.tid
      WHERE taxonomy_index.tid=2")->fetchAll();
 $output = '';
 $nid = '';
  foreach($query as $results){
   $nid = $results->nid;
   $query2 = db_query("SELECT * FROM node WHERE nid = :nid", array(':nid' => $nid));
   $title = '';
   foreach($query2 as $result2){
    $title .= $result2->title;
    $output .= $title.'<br>';
   }
  }
 return $output;
}

There is other solutions too like from hooks. I have only described one of the method.

No comments:

Post a Comment

First HTML blog

Hello there


echo 'dddd';
?>

Js to show Hide Content

Js: var showChar = 250;  // How many characters are shown by default     var ellipsestext = "...";     var moretext = "Sh...