Let me start by saying that I have a somewhat basic understanding of PHP; I can code what I need to do, but often I am not 100% it is the 'best' way to do things.

I have read somewhere that using PHP within the page.tpl.php isn't such a good idea; it is better to keep all code within a separate file.

My questions:

1) Is the above statement true?

2) Is it better just to use an includes file or use the Views module?

That said, I find it much easier to use my own code instead of using Views. Moreover, I don't like the extra code output of Views.

Any thoughts/suggestions would be much appreciated.

Cheers in advance.

Comments

styro’s picture

isn't recommended. You can't version control it easily, editing external files is easier with a PHP aware editor, a bug in the code can stop you loading the page to edit it etc.

I recommend creating a simple module. Here is a quick example I wrote up recently:

http://drupal.org/node/215183

You could just call those functions from inside your nodes.

Later on your module could use a menu hook to have your code create pages at certain paths rather than embedding PHP in nodes. That way you don't need to enable the PHP code filter.

--
Anton
New to Drupal? | Troubleshooting FAQ
Example knowledge base built with Drupal

field4000’s picture

Thanks for your reply and your example.

I tried making my own module, but I am unsure how to call it.

I would like to display contents from the database. When I put the PHP in the page.tpl.php file, it works as it should (I know the code is working).

Any help on a better way to display database content would be much appreciated.

Cheers.

styro’s picture

I tried making my own module, but I am unsure how to call it.

If your module is enabled, you can call the functions in it from anywhere. eg you could call the functions in a node using the PHP input filter, or call them in a template file etc. The whole using module hooks stuff can wait until later after you've got these basic parts working.

I would like to display contents from the database. When I put the PHP in the page.tpl.php file, it works as it should (I know the code is working).

As I can't see your code, the only thing I can think of is that your code is printing its output directly. You don't want a function to directly print its output - that is a job for the code that calls the function. Just get your functions to return their output to the calling code.

eg:

instead of calling this module function

<?php
function field4000_foo() {
  print "output";
}
?>

with this code (eg from a node or template)

<?php
  field4000_foo();
?>

try calling functions that look more like

<?php
function field4000_foo() {
  return "output";
}
?>

called from code that looks more like

<?php
  print field4000_foo();
?>

make sense?

--
Anton
New to Drupal? | Troubleshooting FAQ
Example knowledge base built with Drupal

field4000’s picture

Thank you very much for you help.

I think a lot of people will find this very handy.

Cheers styro!

field4000’s picture

Hey Anton,

I found a snippet that works but I am unsure how to make it into a function.

I'd like to turn this bit of code into a function to call from somewhere else:

<?php foreach ((array)$taxonomy as $item) { ?>
<a href="<?php print base_path() . $item->name; ?>"><?php print $item->name ?></a>
<?php }

Any help with this would be much appreciated.

Cheers.

styro’s picture

That is code intended for templates (the presentation front end) rather than a module (the back end logic). Templates tend to be HTML style files with little islands of PHP (eg lots of <?php ... ?> stuff, whereas modules are solid PHP code and return strings containing generated HTML. Generally the template style is intended to be easy to edit by designers, while modules are aimed more at programmers.

So a function version of that would be something like:

<?php

function yourmodulename_yourfunctionname($taxonomy) {
  $output = '';
  foreach((array)$taxonomy as $item) {
    $output .= l($item->name, $item->name);
  }
  return $output;
}
?>

The l() function is the preferred way to create links in Drupal
http://api.drupal.org/api/function/l/5

In fact the l() function will automatically look up any path aliases, so the following version will work (ok should work - I haven't tested it) even if those taxonomy terms don't have path aliases:

<?php

function yourmodulename_yourfunctionname($taxonomy) {
  $output = '';
  foreach((array)$taxonomy as $item) {
    $output .= l($item->name, 'taxonomy/term/'. $item->tid);
  }
  return $output;
}
?>

--
Anton
New to Drupal? | Troubleshooting FAQ
Example knowledge base built with Drupal

field4000’s picture

You sir are awesome!

You helped me out enormously.

Cheers Anton!