How to display content from other website using your own simple Drupal 6 module

How to prepare other database connection

At first, you have to edit your settings.php file and provide a connection string for other database. Go to the sites/default folder and edit the settings.php.
$db_url['default'] = 'mysqli://uname:pwdt@lhost/first_site_db';
$db_url['abc'] = 'mysqli://uname:pwdt@host/second_site_db';

Now you can start to write your own custom module. Go to the sites/all/modules folder and create a new folder with the name of your module, I will name it as ‘borrowcontent’ in this article. After that create two files:
* modulename.info
* modulename.module

How to create block in Drupal 6 module
My example module will create a block with links to ten latest articles coming from another website. So we will use two functions which tells Drupal about the block availability and about its content:

function borrowcontent_block($op = 'list', $delta = 0, $edit = array()) {

if ($op == 'list') {

$blocks[0] = array( 'info' => t('Borrow Content'),

                          'cache' => BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE,

                       );
  return $blocks;

}
else if ($op == 'view') {

         switch ($delta) {

         case 0:

         $block = array( 'subject' => t('Latest articles borrowed from other website'),

                              'content' => borrowcontent_other_website_latest(),

                           );
         break;

     }
    return $block;

  }

}


Ref: hook_block

How to display data from other website within a block on Drupal 6 website
Now you are ready for the latest part of our example module. Check the function name you have provided as the $block[‘content’] generator above. Create this function within your *.module file. Here is my example:

function borrowcontent_other_website_latest() {

  db_set_active('borrowed');

  $query = "SELECT nid, title FROM {node} WHERE type = 'story' AND status = 1 ORDER BY created DESC LIMIT 0,10";

  $result = db_query($query); 

  $return = '<ul>';

  foreach ($result as $record) {

    $base = 'http:// www.otherwebsiteurl.com /';

    $relative = drupal_get_path_alias('node/'.$record->nid);

    $return .= '<li>'.l($record->title, $base.$relative, array('title'=>$record->title)).'</li>'

  }

  $return .= '</ul>'

  db_set_active('default');

  return $return;

}


Now a few words about how this function works and what is it doing. First, very important calling – db_set_active(). This function gets only one parameter – it is a name of the connection strings for the other database. If you forget check the part describing the changes inside the settings.php, which I mentioned above. What you have to know and probably shouldn’t forget is that as soon as you call the db_set_active() all database operations are performed against the other database, not into your Drupal installation. So be extremely care and be sure to call the db_set_active('default') as soon as you don’t need to work with other database. Calling the db_set_active('default') brings you back to the Drupal. Once again, do not forget to call it.

Now you can provide a string, which will select the data from the database. It is a simple SQL SELECT query. I work here with another Drupal installation. The table name could be inside the {} brackets so the Drupal will replace it with appropriate prefixes when configured in the connection string. I call here for nids and titles of ten latest nodes of the type story. I am also interested only in published content (status = 1).

Now I call db_query() to get the result from my query strings. I will display the data as HTML unordered list so I start preparing the output string with <ul> mark.

Now the most important part. Using the foreach statement I browse all rows from the query results and create a link for the articles selected. Because the Drupal API’s l() function doesn’t work with absolute paths by default I will need three rows of code (another reason is I would like to be cleared for you).

I put the base URL of the other website into the $base variable. From the query I have nid for each node selected. So I use the drupal_get_path_alias() to get relative node’s URL(). On the third row inside the foreach statement I use the Drupal’s l() function the provide a link (<a> mark). The parameters are the linked string (the name of the node from other website), the path of the link and array with attributes where I provide the title=”” attribute for the <a></a>.

After the foreach statement I provide the end of the unordered list and call the db_set_active('default'). At the end I return the string with unordered list and links prepared above.