3

So if I have a custom function with a DB query within template.php such as

function innovista_get_page_children() {

 $result = db_query("SELECT link_path FROM {menu_links}");
 return $result;

}

and I have a content type specific template page, then how do I echo the results in the template file?

I tried this:

<?php
$results = innovista_get_page_children();
foreach($results as $result){
    print $result;
}
?>

I get the following error

Recoverable fatal error: Object of class stdClass could not be converted to string in include() (line 83 of /var/www/vhosts/vidanlawnes.co.uk/innovista2/sites/all/themes/innovista/node--level_1.tpl.php).

If I remove print $result; and replace with print 'hello'... e.g

<?php
$results = innovista_get_page_children();
foreach($results as $result){
//print $result;
print 'hello ';
}
?>

It echoes hello out multiple times, so the query is providing results, but I can't seem to print them as $result. Any ideas?

Thanks for your time

0

3 Answers 3

3

The error message itself is clear I think. You are trying to print an object as a string. otherwise, your query seems correct.

function innovista_get_page_children() {

 $result = db_query("SELECT link_path FROM {menu_links}");
 return $result;

}


<?php
$results = innovista_get_page_children();
print '<pre>';
foreach($results as $result){
    print_r($result);
}
print '</pre>';
?>

Now you will see a formatted variable information of each $result. it will look like

stdClass Object (
  [link_path] => admin/structure/views 
)

So when you print the results, it will be like,

<?php
    $results = innovista_get_page_children();
    foreach($results as $result){
      print $result->link_path;
      print '<br/>';
    }
?>

However, keep in mind that it's never OK to print link paths or database queries like this. It will cause huge performance problems and will arise security problems. Try to use url() and/or l() functions for links (suggested it because you seem to make a link). Also, in most cases, there is a function for core/contrib modules to retrive data.

2
  • Thank you. The query will be more complex (not printing the link_path but using it for another query). When I have finished my function in Drupal, I will post another question on Drupal Answers to ask for improvement suggestions...I'm aware I'm new to Drupal development and so I can probably do most things more efficiently than my first attempts. Thank you. Sep 6, 2012 at 19:18
  • welcome :) database queries are something that you should pay more attention to security and performance.that's why I suggested using url() l() and other simple functions when possible. Those functions are well written and in most cases, performance-friendly. Good luck!
    – AKS
    Sep 6, 2012 at 19:23
1

Every theme function/template runs hook_preprocess_HOOK() and hook_process_hook() before executing the function/template. hook_preprocess_HOOK is where you'd want to set up any new variables. The hook is passed an array of variables by reference, and you add to that array. The key of the array becomes the name of the variable in your template. People often try to keep all the logic out of the template file in this way. For example to set up a variable $foo in a page template (ie what's used for theme('page', ...)) you could do this.

/**
 * Implements hook_preprocess_page().
 */
MYMODULE_preprocess_page(&$vars) {
  $vars['foo'] = 'bar';
}
6
  • Hey, I have just made my question more specific with the error it throws. Could you give me advice on how to apply your solution (if it is still valid) to my problem? Thanks so much Sep 6, 2012 at 19:00
  • @slawrence10 The error is because you're trying to print an object. You can see an example in the docs for using db_query(). As things stand your code isn't very Drupally (!) - are you trying to create a block or a page (or something else)?
    – Andy
    Sep 6, 2012 at 19:10
  • I'm going to get the child items of a page to print out in a page template. Sep 6, 2012 at 19:12
  • @slawrence10 what exactly do you want to print out? A link? If so, what should be the text?
    – Andy
    Sep 6, 2012 at 19:18
  • Hey Andy, sorry the function will be more complex I just boiled down the whole query, because such a basic thing as echoing the variable wasn't working...I now see I should have used the format $result->link_path When I've completed my full solution, I will create another Drupal Answers questions asking for improvements and suggestions. I realise I may be doing many things inefficiently as I'm new to Drupal development. Thank you for your time! Sep 6, 2012 at 19:20
0

First I dont suggest put your function in template.php directly, (you can put in in custom module)
I suggest you act more professionaler in function, This code showed your are begginer in drupal developing,
however . this code have to work because template.php function in exist form basic drupal bootstrapping.
Your problem is your function maybe not return anything and $var will be empty. When I was begginer in drupal put my global fuction in themplate.php and use them in every where i want, test something like this

function myfunction() {

      $result = db_query("SELECT field FROM {table} WHERE field = 'query' ");
       if ($result)
       return $result;
       else
       return "sorry,no result";
     }

and in your tpl file

<?php 
      $myvar = mytheme_myfunction();
      print $myvar;
?>

I know this is not principle method but sometimes we just need it just work. :)

1
  • Hey, check the updated edit. The query definitely has results, because if I use foreach to echo a string "hello" for each result then it does, but when I try and print the actual $result it gives me an error. Sep 6, 2012 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.