Hello,

What is the best way to display the node body of a given node (based on the node id and/or other parameters), inside the body of another node?

Goal: To create a custom node type called "Archive" that in the body displays a tab set of six tabs. Each tab is a unique "Archive Item", each item is associated to an archive based on a value (in this case the year).

See actual representation of what I'm trying to accomplish in the example below HERE.

I've been able to make this work, but it's not pretty at all and I'm sure it's not entirely correct.

I would love some help here, and if someone can provide a better solution I'm willing to compensate them.

There are plenty of people who would like to accomplish this and there seems to be no clear answers available in any of the many threads that I have read.

1.) So far I've created two custom node types one called "Archive" and another called "Archive Item" with the following values:

Archive Fields:
1.) Image
2.) Title
3.) Sub-title
4.) Year
5.) Body

Archive Item Fields:
1.) Title
2.) Category ( 1 of 6 choices)
3.) Year
4.) Body

2.) I've created test content for each of the 6 categories each for the same year (ex. 2007).

3.) I've created a test archive for the year 2007 and I've used the following code inside of a php page:

<?php 
 
class DBJT {
   function DB() {
       $this->host = "localhost";
       $this->db = "database";
       $this->user = "user";
       $this->pass = "pass";
       $this->link = mysql_connect($this->host, $this->user, $this->pass);
       mysql_select_db($this->db);
       register_shutdown_function($this->close);
   }
   function close() {
       mysql_close($this->link);
   }
}
 
$myyear = $_GET["year"];
 
    define(WC_DB_HOST, 'localhost');
    define(WC_DB_NAME, 'db');
    define(WC_DB_USER, 'user');
    define(WC_DB_PASS, 'pass');
   
mysql_connect (WC_DB_HOST, WC_DB_USER, WC_DB_PASS) or die("Couldn't connect to MySql");
mysql_select_db (WC_DB_NAME);
 
$result = mysql_query("select content_type_archive_item.*,node_revisions.title,node_revisions.body from node_revisions,content_type_archive_item where content_type_archive_item.field_archive_item_year_value = '$myyear' and content_type_archive_item.nid = node_revisions.nid  ");
 
while ($resultrow= mysql_fetch_array($result)) {
  $field_subtitle_value = $resultrow["field_subtitle_value"];
  $title = $resultrow["title"];
  $cat = $resultrow["category"];
  $i = $resultrow["field_archive_category_value"];
            
 switch ($i) {
            case "Overview":
                $overview = $resultrow["body"];
                break;
            case "Target Story":
                $target = $resultrow["body"];
                break;
            case "Reader":
                $reader = $resultrow["body"];
                break;
            case "Scripts":
                $scripts = $resultrow["body"];
                break;
            case "Company":
                $company = $resultrow["body"];
                break;
            case "Staff":
                $staff = $resultrow["body"];
                break;
               
            } // end of switch
 
} // end of while
 

$tabs = array();
  $tabs['container'] = array(
    '#type' => 'tabset',
  );
  $tabs['container']['tab1'] = array(
    '#type' => 'tabpage',
    '#title' => t('Overview'),
    '#content' => t("<br/><br/><br/>$overview"),
  );
  $tabs['container']['tab2'] = array(
    '#type' => 'tabpage',
    '#title' => t('Target Story'),
    '#content' => t("<br><br><br>$target"),
  );
  $tabs['container']['tab3'] = array(
    '#type' => 'tabpage',
    '#title' => t('Reader'),
    '#content' => t("<br><br><br>$reader"),
  );
  $tabs['container']['tab4'] = array(
    '#type' => 'tabpage',
    '#title' => t('Scripts'),
    '#content' => t("<br><br><br>$scripts"),
  );
 $tabs['container']['tab5'] = array(
    '#type' => 'tabpage',
    '#title' => t('Company'),
    '#content' => t("<br><br><br>$company"),
  );
 $tabs['container']['tab6'] = array(
    '#type' => 'tabpage',
    '#title' => t('staff'),
    '#content' => t("<br><br><br>$staff"),
  );

  return theme('tabs_tabset', $tabs);



?>

4.) Last but not least I pass in the following year variable to link to the actual archive:

http://yoursite.com/?q=node/15&year=2007

The actual variable is [ &year=2007 ].

I firmly believe that there must be an easier way, I'm just not sure what to do. I'm not very good with PHP and this is my first time doing this. Can someone please help out?

Thanks,

Will

Comments

pobster’s picture

*splutter* whilst your way is a workable solution its completely unnecessarily complicated and has a MASSIVE security hole passing an unchecked/ un-validated $_GET variable straight into a database query (someone could potentially delete your entire database) added to that it appears to be possible to view your database information in plain text as well! Dangerous!

Is there a reason why this code has to be in a separate php file? If it really does then you can save yourself an awful lot of trouble by simply bootstrapping your page to include Drupal like this for instance; http://cvs.drupal.org/viewcvs/drupal/contributions/modules/logotool/logo...

That then allows you to use the Drupal API (well depending on what you've bootstrapped, LATE_PAGE_CACHE is used there simply because I required use of Drupals 'variable_get', but you can use DRUPAL_BOOTSTRAP_FULL to have access to everything). Doing it this way is better because you then don't have to connect to the database as you're already connected from Drupal - you can then use the Drupal API to call the database a lot more securely than before. I'd suggest taking a look at the snippets page for how to go about structuring your code, it's pretty simple... There's actually a filter module which does the same thing as well - Attached Node. Maybe you could use this for some guidance?

Pobster

BKWill’s picture

Thanks, I'm not very good at this, and I appreciate you pointing me in a better direction. I'm looking to display the body of a node inside of another node based on the node id.

The code above I placed directly inside of the node body for the Archive content type that I created, it's not separate. This is my first time trying to do this, and I don't completely understand PHP, or the options that Drupal provides.

pobster’s picture

I'm not very good at this...

Sometimes jumping straight in isn't the best thing to do!!! PLEASE first have a play around with some of those php (page) snippets to get a better idea of how to do things/ how things work. You'll pick it up quickly I'm sure, I did!

Pobster

nevets’s picture

This is a bit of stab in the dark and is based on the other examples I have found for javascript tabs. While the code is based on your code above it is highly simplified and uses the approriate Drupal functions

<?php
$myyear = $_GET["year"];
$tabs = array();

$result = db_query("select content_type_archive_item.*,node_revisions.title,node_revisions.body from node_revisions,content_type_archive_item where content_type_archive_item.field_archive_item_year_value = '%d' and content_type_archive_item.nid = node_revisions.nid", $myyear);

while ($node = db_fetch_object($result)) {

  $tabs[] = array(
    '#type' => 'tabpage',
    '#title' =>$node->field_archive_category_value,
    '#content' =>$node->body
  );
 
} // end of while

// Since this snippet is used inside of a node with input filter set to PHP, print the results
  print theme('tabs_tabset', $tabs);

?>

This may (or may not) getting you closer. If it does not work, what are the results?

nevets’s picture

I tested the basic logic for producing the tabs and it works. One difference from your starting point is it call tabs_render() instead of the theme function which did not work. This example though has not been tested since it relies on your data.

<?php
$myyear = $_GET["year"];
$tabs = array();
  $tabs['container'] = array(
    '#type' => 'tabset',
  );

$result = db_query("select content_type_archive_item.*,node_revisions.title,node_revisions.body from node_revisions,content_type_archive_item where content_type_archive_item.field_archive_item_year_value = '%d' and content_type_archive_item.nid = node_revisions.nid", $myyear);

while ($node = db_fetch_object($result)) {

  $tabs['container'] [] = array(
    '#type' => 'tabpage',
    '#title' =>$node->field_archive_category_value,
    '#content' =>$node->body
  );

} // end of while

// Since this snippet is used inside of a node with input filter set to PHP, print the results
  print  tabs_render($form);

?>