How do you accomplish Dynamic pages in Drupal?

More specifically, I would like to simply draw data from a database (not the Drupal database) to populate a page based on the URL string (GET). For example, mysite.com/cities.php?n=Atlanta would show database data for Atlanta and and mysite.com/cities.php?n=NewYork would show database data for New York.

Instead of having a separate static page for every city, I would think it would still be easiest to do what I am currently doing outside of CMS and show all cities with the single cities.php page that uses the URL string to specify what to return from the database.

Is there a method for doing this in Drupal? I could probably do it by using "Basic Page" and enabling the PHP filter and just calling the database as I would normally in that, but it would be optimal if Drupal recognized each Dynamic page as a separate page with separate comments, etc. Since there is a "Basic Page" content option for "static" content, I thought there might be something else that actually recognizes dynamic pages.

I am also not opposed to using static pages if there is a good way to dynamically create static pages from the database.

Thanks in advance for any advice.

Comments

WorldFallz’s picture

All drupal pages are dynamic and the recommended method of doing this with drupal is to actually use drupal, lol. Using the php filter in a node is exactly how not to do it (in general storing code in the db is bad practice).

Honestly, from what you describe above, I'm not sure why you would use drupal at all. That said however, you could either use feeds to pull the data into nodes or perhaps just expose the external data to drupal using the views api (see views advanced help) and then use views to display like any other drupal content.

dnewkerk’s picture

You should create a simple custom module for this (it's not too hard). Don't use Drupal's "Basic Page" content type for this.

In a nutshell, some links/notes to get you started...

Example modules with tons of internal comments/instructions/demos: http://drupal.org/project/examples

To query your external database from within your Drupal module: http://drupal.org/node/18429

You will need to make a menu callback with hook_menu() in which you tell Drupal that when a path of /cities/% is accessed (where % is an argument/placeholder for the city name), your specific function in your module should be called to handle that page. Unfortunately Drupal uses the word "menu" in a confusing way in this case - it doesn't refer to a menu in a UI sense (though it can also put pages into the menu UI), but is the terminology for how Drupal internally thinks about all URLs on the site. Your function will receive the % (city name) from the URL, run it through some checks to make sure it is sanitized for security before you work with it, and then perform a query against your external database to get back whatever data you need. Your function's return value will be output as the content of the page. If you confirm it is a valid city name, and your external database call returned the data you needed, you could format and output the content for the page however you need, and if the city name provided was invalid, return a 404 error instead: return drupal_not_found();.

This is almost the same concept as your current cities.php file, except done "the Drupal way".

Hope this helps :)

yelvington’s picture

Yikai Wang’s picture

I have the same requirements, but I still haven't found an appropriate answer, have you found out how to do this?