A
A
annabilyk942016-11-15 00:11:13
PHP
annabilyk94, 2016-11-15 00:11:13

Working with a database in Drupal 7?

Hello.
You need to redo the database query to display tags according to the dictionary with the number of nodes on Drupal 7, here is the query itself

db_query("SELECT tid, name, (SELECT COUNT(*) FROM {taxonomy_index} ti LEFT JOIN {node} n ON ti.nid = n.nid WHERE ti.tid = td.tid AND n.status = 1) node_count FROM {taxonomy_term_data} td WHERE vid = :vid ORDER BY weight", array(':vid' => 3));

needs to be converted to Drupal requests. For those who don't understand, here's an example:
$query = db_select('node', 'n');
$query->innerJoin('users', 'u', 'n.uid = u.uid');
$query->fields('n', array('title'));
$query->fields('u', array('name'));
$nodes = $query->execute()->fetchAll();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
afi13, 2016-11-15
@annabilyk94

$query = db_select('taxonomy_term_data', 'td');
$query->fields('td', array('tid', 'name'));
$query->condition('td.vid', 3);
$query->addExpression("(SELECT COUNT(*) FROM {taxonomy_index} ti LEFT JOIN {node} n ON ti.nid = n.nid WHERE ti.tid = td.tid AND n.status = 1)", 'node_count');
$terms = $query->execute()->fetchAll();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question