Page 1 of 1

Passing phpBB variable to PHP/MySQL code in template

Posted: Wed Nov 26, 2014 4:15 am
by keepingitodd
Hi everyone. I'm struggling with something, have been for a couple of days, and no amount of searches have helped me. I'm adding a bit of PHP in one of my phpBB templates that will perform actions in the viewtopic.html file based on the poster of each individual post. Basically, it will check query a, if it has data, it will display an icon along with the PM, website, etc icons. Basically, my MySQL query would be something like this:

Code: Select all

SELECT count(ID) FROM theTable WHERE usrID = 53
Now, "53" needs to be the poster ID, and for the life of me I can't seem to get the poster ID into the SQL query. When I hardcode a number in the query, it works fine. But when I try to pass the variable, I get nothing. I've tried it many different ways:

If tried these with and without quotes. I've also tried with with and without quotes and with and without <!-- -->

Code: Select all

mysql_query("SELECT count(ID) FROM theTable WHERE usrID = '{postrow.POSTER_ID}'")

Code: Select all

mysql_query("SELECT count(ID) FROM theTable WHERE usrID = ' . $row['poster_id'] . '")

Code: Select all

mysql_query("SELECT count(ID) FROM theTable WHERE usrID = ' . $user_cache[$poster_id] . '")

Code: Select all

mysql_query("SELECT count(ID) FROM theTable WHERE usrID = ' . $topic_data['topic_poster'] . '")
This is all within the <!--PHP--><!--ENDPHP--> tags. I even tried an ENDPHP tag, then the variable, then PHP tag, and that didn't work.

I'm at a total loss here. Can anyone help me?

Thank you!

John

Re: Passing phpBB variable to PHP/MySQL code in template

Posted: Wed Nov 26, 2014 2:50 pm
by RaythXC
Firstly, it's not very secure to add PHP code to the template html files. Your best bet is modifying the viewtopic.php file and using the $template->set_var or $template->set_vars option to send it to the HTML file, then load that into the template using the correct variable in the template.

Example:
In the php

Code: Select all

$result = $db->sql_query("QUERY TEXT HERE");
//Some other code to do what you want
$template->assign_var('THIS_VAR', $data_to_send);
In the html you would call it by putting in {THIS_VAR} where you want it to appear.

Re: Passing phpBB variable to PHP/MySQL code in template

Posted: Thu Nov 27, 2014 2:40 am
by keepingitodd
Thank you, RaythXC. This is *almost* working for me....I can't seem to find the right place to put my PHP/SQL code to get the poster id variable, but if I hardcode an ID it passes it on to the template, so I'm getting close. This has been a big help!

John