Drupal Snippets

The basic code:

<?php
$output = '';
$sql = 'SELECT * FROM {node} WHERE type = "%s"';
$result = db_query($sql, 'page');
while ( $data = db_fetch_object($result) ) {
$output .= '' . l($data->title, 'node/' . $data->nid) . '';
}
return $output;
?>

Using db_rewrite_sql()

It's good practice to use db_rewrite_sql() when retrieving nodes from the database because it allows other modules (such as Internationalization) to add their own joins and conditions to your query (not always, but usually, desirable). We simply wrap this function around our $sql variable and send the primary table {node} and the primary field nid as arguments.

<?php
$output = '';
$sql = 'SELECT * FROM {node} WHERE type = "%s"';
$result = db_query(db_rewrite_sql($sql, '{node}', 'nid'), 'page');
while ( $data = db_fetch_object($result) ) {
$output .= '' . l($data->title, 'node/' . $data->nid) . '';
}
return $output;
?>

Pagination

It makes sense to add a pager to our listing when we have a lot of nodes. Here we'll replace db_query() with pager_query() and add a pager at the end of our listing with theme_pager() (note that this actually called using theme()). In this example we are asking to view only 10 nodes at a time.

<?php
$output = '';
$sql = 'SELECT * FROM {node} WHERE type = "%s"';
$result = pager_query(db_rewrite_sql($sql, '{node}', 'nid'), 10, 0, NULL, 'page');
while ( $data = db_fetch_object($result) ) {
$output .= '' . l($data->title, 'node/' . $data->nid) . '';
}
$output .= theme('pager', NULL, 10);
return $output;
?>

Accessing all node object data

Using the examples above won't give you access to all node data, such as the node teaser, or data that has been added by other modules such as CCK. Bringing this in is easy using node_load().

<?php
$output = '';
$sql = 'SELECT * FROM {node} WHERE type = "%s"';
$result = pager_query(db_rewrite_sql($sql, '{node}', 'nid'), 10, 0, NULL, 'page');
while ( $data = db_fetch_object($result) ) {
$node = node_load($data->nid);
$output .= '';
$output .= '' . l($data->title, 'node/' . $data->nid) . '';
$output .= '' . $node->teaser . '';
$output .= '';
}
$output .= theme('pager', NULL, 10);
return $output;
?>

Sorting

If you want to sort your content by a field, modify your $sql string. The following example will order nodes by the creation date, in descending order.

<?php
$output = '';
$sql = 'SELECT * FROM {node} WHERE type = "%s" ORDER BY created DESC';
$result = pager_query(db_rewrite_sql($sql, '{node}', 'nid'), 10, 0, NULL, 'page');
while ( $data = db_fetch_object($result) ) {
$node = node_load($data->nid);
$output .= '';
$output .= '' . l($data->title, 'node/' . $data->nid) . '';
$output .= '' . $node->teaser . '';
$output .= '';
}
$output .= theme('pager', NULL, 10);
return $output;
?>

Here is the final working code, inserted into sites/all/themes/mytheme/template.php. Replace "mytheme" with your theme name.

<?php
function mytheme_page_listing() {
$output = '';
$sql = 'SELECT * FROM {node} WHERE type = "%s" ORDER BY created DESC';
$result = pager_query(db_rewrite_sql($sql, '{node}', 'nid'), 10, 0, NULL, 'page');
while ( $data = db_fetch_object($result) ) {
$node = node_load($data->nid);
$output .= '';
$output .= '' . l($data->title, 'node/' . $data->nid) . '';
$output .= '' . $node->teaser . '';
$output .= '';
}
$output .= theme('pager', NULL, 10);
return $output;
}
?>

To print the results in sites/all/themes/mytheme/page.tpl.php (or a block body):

<?php print mytheme_page_listing() ?>

Many thanks to Lara:
http://www.othermachines.com/blog/lara/drupal-example-generate-content-l...

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options