How do I create block content that shows a list of links to all my pages from a certain category?

I am vaguely familiar with making blocks and I know how to write sql statements in the traditional sense but I do not know how to write quieries *for drupal*.

The query would look something like this:

Select * from page.nodes where category ='navigation';

And the output I want to look like this:

< a href="page1 url" >page1< / a >
< a href="page2 url" >page2< / a >
< a href="page3 url" >page3< / a >
< a href="page4 url" >page4< / a >

I think I would need an example. I do not know how to write the code to get a query or manipulate the results. I do not know how to format the query for drupal (I do not know the table structures to select from).

Comments

neofactor’s picture

here is a block that pulls profile info for a user and displays the array key/values

<?php
global $user;
/* default ?id= to the current user */
if(!isset($_GET['id'])) { $_GET['id'] = $user->uid; }
print "Profile Info for User:" . $_GET['id']."<hr>";
$result = db_query('SELECT f.name, f.type, v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE uid = %d', $_GET['id']);
while ($field = db_fetch_object($result)) {
print "<b>" . $field->name ."</b>: ". $field->value . "<br>";
}
?>

You get the idea?

judah’s picture

i get the idea. i don't know how to write out the query. does this code, as it is, work in a block? i trimmed it down to what i think i need but the sql is wrong because i made it up out of thin air. i don't know how to write the sql still.

print "All pages in Navigation Category:" ."<hr>";
$result = db_query('SELECT f.title, f.url FROM {nodes} f INNER JOIN {cat_id} v ON f.fid = v.fid WHERE catname= %d', 'navigation');
while ($field = db_fetch_object($result)) {
 print "<a href='". $field->url . "'>" . $field->title ."</a>: ".  "<br>";
}
chx’s picture

$result = db_query("SELECT DISTINCT(n.nid), n.title FROM {node} n LEFT JOIN {term_node} t ON n.nid=t.nid LEFT JOIN {term_data} td ON t.tid=td.tid WHERE td.name='%s' ORDER BY n.sticky DESC, n.created DESC", 'yourtermnamehere');
while ($node = db_fetch_object($result)) {
  print l($node->title, 'node/'. $node->nid);
}

This does not add any formatting. I'd suggest NOT using brs but ul li and using CSS. If you have only one term named yourtermnamehere you can replace DISTINCT(n.nid) with the simpler and faster n.nid

--
Drupal development: making the world better, one patch at a time. | A bedroom without a teddy is like a face without a smile.

judah’s picture

Doh! I think I made a mistake. I meant a query to show a list of all the subcategory nodes under a category term.
I'll clarify a little bit more now.
I have a category called, "Subject Matter"
and under that is:
Children & Families (edit term)
Civil Rights (edit term)
Economy (edit term)
Education (edit term)
etc.
Thank you!

chx’s picture

there is no such thing as a subcategory node. However, there are things which can be called children of a term. These are printed as

foreach (taxonomy_get_term_by_name('yourtermnamehere') as $result) {
  print $result->name .' ('. l(t('edit term'), "admin/taxonomy/edit/term/$result->tid") .')<br />'; /**/
  foreach (taxonomy_get_tree($result->vid, $result->tid) as $term) {
    print _taxonomy_depth($term->depth+1) .' '. $term->name .' ('. l(t('edit term'), "admin/taxonomy/edit/term/$term->tid") .')<br />';
  }
}

You may want to delete the line marked by /**/, if you do so, delete the +1 from the depth, too. Basic idea for this block comes from the function taxonomy_overview.

--
Drupal development: making the world better, one patch at a time. | A bedroom without a teddy is like a face without a smile.

judah’s picture

This has been very helpful. Thank you.

I think I got what I needed.

foreach (taxonomy_get_tree(1) as $term) {
  print l(t($term->name), "taxonomy/term/$term->tid") .'<br />';
}