How to query a field in phpbb_topics and display result on viewtopic.php

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
User avatar
teebling
Registered User
Posts: 52
Joined: Sat Apr 14, 2018 7:38 pm
Contact:

How to query a field in phpbb_topics and display result on viewtopic.php

Post by teebling »

Hello all, got a question involving SQL queries and viewtopic.php.

I've done a good deal of homework on this through the search function etc, to try and figure it out myself, but haven't come up with anything yet. Wondering if I can get some help here.

Progress so far:

I have a topic with the topic_id of 2.

In the phpbb_topics table, the topic has its normal row with all the default values.

I used an SQL ALTER TABLE statement to successfully add three extra columns to the phpbb_topics table.

These new columns are fruitname which is varchar(255), fruitcount which is int(1), and finally fruitdescription which is mediumtext.

The row for this topic in the phpbb_topics table therefore now looks a bit like this:

Code: Select all

topic_id    forum_id    ...etc...    fruitname    fruitcount    fruitdescription
   2            1       ...etc...      apple           3        The apple is a delicious, crunchy red frui...

What I want to achieve from here:

This is where I'm stuck. As you can see I have a very basic idea of what needs to be done, but no idea on how to execute it code-wise.
  • I want to make an SQL query, or number of queries, somewhere in viewtopic.php (I guess?) that will fetch these three columns' data for the topic that is currently being viewed.
  • I want to assign these different results to template variables that can then be called in viewtopic_body.html and rendered as HTML.
  • For example if I use { FRUITNAME }, { FRUITCOUNT } and { FRUITDESCRIPTION } in viewtopic_body.html, the data from the relevant fields will render as HTML.
So that when someone visits the topic, if the topic has data in these additional new columns, the data will be fetched and displayed on the topic's page somewhere (I don't really care where, so long as I have a template variable to put somewhere).

Please note:
  • I don't need help with where to put the template variable in the html template, I would be comfortable by this point. I'm just struggling with the backend work.
  • I would not like suggestions for other extensions that may/may not have similar functionality - I am well aware of what is available from the amazing ext community at present and unfortunately it doesn't cover this.
  • I am well aware of the ramifications of editing core files directly, and of the additional work required to maintain it after upgrading etc.
Many thanks for reading and hope someone out there can help :)
User avatar
Brf
Support Team Member
Support Team Member
Posts: 52169
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: How to query a field in phpbb_topics and display result on viewtopic.php

Post by Brf »

The select for $post_data is selecting t.*, so your columns are already being selected and put into the $post_data array. This code is between lines 185-249. The columns of that array are put into the $template starting at line 781. Just add your columns there similar to the way topic_title is done at line 786.
User avatar
teebling
Registered User
Posts: 52
Joined: Sat Apr 14, 2018 7:38 pm
Contact:

Re: How to query a field in phpbb_topics and display result on viewtopic.php

Post by teebling »

Brf wrote:
Fri Oct 23, 2020 2:51 pm
The select for $post_data is selecting t.*, so your columns are already being selected and put into the $post_data array. This code is between lines 185-249. The columns of that array are put into the $template starting at line 781. Just add your columns there similar to the way topic_title is done at line 786.
Thank you so much Brf! This worked. :D

For anyone looking for a step-by step here's what I did:

In /viewtopic.php, find:

Code: Select all

// Send vars to template
$template->assign_vars(array(
After, paste:

Code: Select all

'FRUITNAME' 	=> $topic_data['fruitname'],
'FRUITCOUNT' 	=> $topic_data['fruitcount'],
'FRUITDESCRIPTION' 	=> $topic_data['fruitdescription'],
In /styles/prosilver/template/viewtopic_body.html, find:

Code: Select all

<!-- EVENT viewtopic_body_poll_after -->
(Or wherever you want your data to appear...)

After, paste:

Code: Select all

{FRUITNAME}
{FRUITCOUNT}
{FRUITDESCRIPTION}
And voila it works! Just rename the db columns and template variables to whatever you want.

Thanks again Brf.
Post Reply

Return to “phpBB Custom Coding”