Quantcast
Viewing all articles
Browse latest Browse all 2325

phpBB Custom Coding • External connection to a second db

Hello everyone!
Thanks for programming PHPBB!!
I've been using phpBB for few years, making some mod sometimes, and I'm looking to improve my PHP skills over time.

I have a 3.3.10 installation now and would like to make some changes by approaching the creation of extensions.
I would like not to directly modify phpBB's PHP files but rather make integrations so that they can be maintained over time, even with future updates.

I have a forum that interacts with a website. On the site, I've recreated and assigned the same login as the forum (IN PHPBB TRUE), so the navigation is closely linked to the forum. but it uses another db.
So far, I've modified some functions in the template to integrate the forum and the site.

like...
SQL phpbb-DB
SQL website-db
same host, different name, user and psw

However, I'd like to go further. I have some text lines in a secondary database (simply containing links) that I would like to display in the forum, particularly on the posting.php page (or specifically when someone wants to reply to a topic). I would like that, beneath the text area, there is a list of the last 15 lines from a secondary database. All of this to avoid affecting the main phpBB database.

Creating it statically in HTML is simple. You just need to modify and insert an "event" in the posting_editor_message_after.html position. The problem arises when I want to insert these 15 links as the latest ones with an SQL query:

Code:

SELECT * FROM `foto` ORDER BY `foto`.`id` DESC LIMIT 15;
So, I tried to find a method to connect to this second database, but without success. I attempted to use the wiki, but I have a "basic" knowledge of PHP. I've always copied and adapted code, gradually learning its structure, but I'm still far from proficient in PHP. Image may be NSFW.
Clik here to view.
:roll:


Nevertheless, I tried to create this PHP event (thank www and gpt for helping Image may be NSFW.
Clik here to view.
:D
), but the result is only causing the forum to crash with errors on the posting page.

Code:

page listener.php<?php/** * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 */namespace sebo\galleria_posting\event;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class listener implements EventSubscriberInterface{    /** @var \phpbb\db\driver\driver_interface */    protected $db;    /** @var \phpbb\template\twig\twig */    protected $template;    /**     * Constructor for listener     *     * @param \phpbb\db\driver\driver_interface $db phpBB database connection     * @param \phpbb\template\twig\twig $template phpBB template     * @access public     */public function __construct(\phpbb\db\driver\mysqli\mysqli $db, \phpbb\template\twig\twig $template){$this->db = $db;$this->template = $template;}    /**     * Assign functions defined in this class to event listeners in the core     *     * @return array     * @static     * @access public     */    static public function getSubscribedEvents()    {        return array(            'core.posting_modify_message_text' => 'galleria_posting',        );    }    /**     * Update the template variables with data from the external database     *     * @param object $event The event object     * @return null     * @access public     */    public function galleria_posting($event)    {        // Establish a connection to the external database (replace with your connection details)        $externalDb = new \phpbb\db\driver\mysqli\mysqli();        $externalDb->sql_connect('db_host', 'db_user', 'db_passw', 'db_name');// Query the "foto" table in the external database$sql = 'SELECT * FROM ' . $externalDb->sql_table('foto') . ' ORDER BY ' . $externalDb->sql_escape('foto.id') . ' DESC LIMIT 15';$result = $externalDb->sql_query($sql);// Fetch the data and assign it to the template$links = array();while ($row = $externalDb->sql_fetchrow($result)){// Assumi che la colonna nel risultato della query si chiami 'link'// Se la colonna ha un nome diverso, sostituisci 'link' con il nome corretto$links[] = $row['link'];}// Chiudi il risultato della query$externalDb->sql_freeresult($result);// Assegna i dati al template$this->template->assign_vars(array('EXTERNAL_DB_LINKS' => $links,));    }}

Code:

page services.ymlservices:    sebo.galleria_posting.listener:        class: sebo\galleria_posting\event\listener        arguments:            - '@template'        tags:            - { name: event.listener }

Code:

page posting_editor_message_after.html{EXTERNAL_DB_LINKS}
Could someone help me build this function?

Thank you very much!

Statistics: Posted by sebo — Mon Jan 22, 2024 10:55 pm



Viewing all articles
Browse latest Browse all 2325

Trending Articles