I have spend quite some time looking through the Handbook but I can't seem to locate any sample code for php boxes or "static" php pages. While there are sections describing how to make modules and themes, I can't find any that give examples for pages and boxes. I'd suggest that this would be very helpful for the large number of people like me who are not professional programmers but who can figure out how to tweek code appropriately.

For example, what would the php code be in order to return all the titles for nodes associated to a particular taxonomy (e.g. tid=5) in a page. What if one wanted to return the five Imost recent nodes associated to a particular taxonomy? What if one wanted the titles and the summary details, or the titles and the complete details, etc.

Similarly, it would be useful to provide examples for boxes. I had to learn the hard way that for boxes the code must end with a "return" statement whereas for pages a "print" statement should be used instead.

If such guidelines/examples already exist, then IMHO they could/should be made more visible in the handbook.

Thanks in advance for any feedback that I get on this.

Comments

carlmcdade’s picture

The manual does have 25 to 30 pages of links to sites and their description. I downloaded and created a MDI file to read offline. It was 234 pages. But I got frustrated when I saw that 30 pages were site links. I am all for dropping these in place of some more database information and example code.
---------------------------
info for Drupal installation
__________________________
Carl McDade
Information Technology Consult
Team Macromedia
www.heroforhire.net

Steven’s picture

The drupal sites list is generated automatically by sites that ping drupal.org using drupal.module.

tostinni’s picture

Hi,
I also feel a little bit sad not to have much examples code connected to handbook, it should be very usefull.
Well I do find a little more help, maybe it can help you, havec look at http://drupaldocs.org/ and search for "example", you will find code for writing node, block...

omar’s picture

Here is the PHP code for a block to list the last five nodes associated to a taxonomy, in this case tid=17.

<?php 
$taxo_id = 17; 
$sql = "SELECT node.title, node.nid FROM node INNER JOIN term_node ON node.nid = term_node.nid WHERE term_node.tid = $taxo_id LIMIT 5";

  $output .= "<ul>";
$result = db_query($sql);
while ($anode = db_fetch_object($result)) {
  $output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
  $output .= "</ul>";
return $output;

?>

Clearly, you can modify the HTML used here as well as add ID and/or CLASS tags for use with CSS styles.

Thanks go out to CyberSteph for his help with this.

multiplex’s picture

I'd have to agree with you about the documentation, I remember having exactly the same problem when I started using Drupal.

Here's some code samples, I checked it for the 4.4.0 release (although there shouldn't be much difference).

** Return 15 titles for taxonomy:

$taxonomy_id = 17;
$result = taxonomy_select_nodes(array2object(array ("str_tids" => "$taxonomy_id", "tids" => array($taxonomy_id), "operator" => "or")), 0);
while ($node = db_fetch_object($result)) {
  $node = node_load(array('nid' => $node->nid));
  $titles[] = l($node->title, 'node/view/' .$node->nid);
}

print theme('item_list', $titles);

** Five most recent posts for block:

$taxonomy_id = 17;
$result = taxonomy_select_nodes(array2object(array ("str_tids" => "$taxonomy_id", "tids" => array($taxonomy_id), "operator" => "or")), 0);
while ($node = db_fetch_object($result)) {
  if ($i < 5) {
    $node = node_load(array('nid' => $node->nid));
    $titles[] = l($node->title, 'node/view/' .$node->nid);
  }
  $i++;
}

return theme('item_list', $titles);

** Listing nodes with summary:

$taxonomy_id = 17;
$result = taxonomy_select_nodes(array2object(array ("str_tids" => "$taxonomy_id", "tids" => array($taxonomy_id), "operator" => "or")), 0);
while ($node = db_fetch_object($result)) {
  $node = node_load(array('nid' => $node->nid));
  $content .= theme('node', $node, 1);
}
print $content;

** Listing nodes full:

$taxonomy_id = 17;
$result = taxonomy_select_nodes(array2object(array ("str_tids" => "$taxonomy_id", "tids" => array($taxonomy_id), "operator" => "or")), 0);
while ($node = db_fetch_object($result)) {
  $node = node_load(array('nid' => $node->nid));
  $content .= theme('node', $node, 0);
}
print $content;

-------------
MultipleX
Don't click this if you don't know Farsi!!! :)
www.hambastegimeli.com

omar’s picture

I'll try to do some test with 4.5 but in the meantime thanks for your input! I'll post more samples as I come across them. Who knows, maybe there is a section in the Developper Handbook to put this kind of thing but I still haven't found it.

czarphanguye’s picture

Any input as to why this doesn't work for v4.5? Here are the errors:

warning: Illegal offset type in /home/public_html/modules/taxonomy.module on line 596.

user error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'or) AND n.status = 1 AND '1'' at line 1

query: SELECT COUNT(DISTINCT(n.nid)) FROM node n INNER JOIN term_node tn ON n.nid = tn.nid WHERE tn.tid IN (42,Array,or) AND n.status = 1 AND '1' in /home/public_html/includes/database.mysql.inc on line 125.

user error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'or) AND n.status = 1 AND '1' ORDER BY n.sticky DESC, n.created

query: SELECT DISTINCT(n.nid), n.sticky, n.created FROM node n INNER JOIN term_node tn ON n.nid = tn.nid WHERE tn.tid IN (42,Array,or) AND n.status = 1 AND '1' ORDER BY n.sticky DESC, n.created DESC LIMIT 0, 10 in /home/public_html/includes/database.mysql.inc on line 125.

Regards,

Czar

Bèr Kessels’s picture

Please do not start a new threadm, but add your custom blocks *as child pages* to http://drupal.org/node/17170

And if this solved you problem, would you be so kind to report back that it helped? This will help others whom are looking for the same solution.

[Ber | Drupal Services webschuur.com]

omar’s picture

...where it was? Does somebody know where it is?

sepeck’s picture

Click on custom blocks and then examples.

-sp
---------
Test site, always start with a test site.
Drupal Best Practices Guide -|- Black Mountain

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide