I’m very new to drupal and i need to query a table and pass a result from that query to my page.tpl file as a variable. Ive tried for hours but nothing i find works.
php code in template.php
<?php
$result = db_select("SELECT COUNT(node.nid) AS num FROM node WHERE node.type = 'service'");
foreach ($result as $record) {
$number = $record->num;
}
return $number;
I even tried adding an echo 'hello world'; but that didnt show up either.
Code in page.tpl
<?php if ($number > 0) { ?>
testing
<?php } ?>
There are two nodes with the type as service. Any help would be great. thanks.
You need to use a hook in your template.php file.
For example (replace THEME_NAME with your theme’s name):
This will pass a variable called “foo” to your page.tpl.php file.