Topic Closed
16 Posts

Hi everyone.

I'm creating a plugin that I want to interact with a MySQL database that I already have established. The database is used by another app, and I want to pull some of the info out into my Typesetter CMS pages.

I have a file called initiate.php which is added to the head of my site via the following line in Addon.ini:

script = 'initiate.php'

In this file a connection to the database is established and stored in $LINK.

Then I have a separate php script which is a gadget.  I want the gadget to be able to access the $LINK established in the initiate script, but when I do it fails.

To remove the complexity of the database I have tested by setting a test variable in initiate.php then echoing that variable from the gadget. The variable is empty from the gadget code.

So, my question is - how can I set a variable in one script of my addon, then access it from another?

Thanks in advance for any assistance.

6 years ago#10585

1.5K Posts
16 Plugins

Plugin hooks and gadget functions aren't called in the same order as their output appears on a page. In fact, gadgets are 'prepared' relatively early, while the GetHead hook is called late as part of the page rendering process. Therefore you might not even need the GetHead hook at all – it's really only an 'action' hook in order to load assets like css, javascript or meta data.

If you only want to output some values from your database in the gadget, you can do everything in the gadget script/function/method.

But of course gadget(s) and other hook(s) may share data. With a global variable, which works basically, you'll always adhere to the internal calling order.
If you don't want to care/keep track of this order, put everything into a class and query your database in the the class constructor method. Feed your global variable(s) here, use class variables or even write stuff to the global $page object - I'd call it merely a matter of taste. (Plugin methods may also be called statically, but this would require some sort of static init function for your DB query, therefore I'd gor for an object instantiation in your case). So it could be sth. like that…


Addon_Name = 'Links from Database'
# Addon_Unique_ID = XXX
Addon_Version = 1
min_gpeasy_version = 4.3
About = 'Get link(s) from database'

script = 'LinksFromDb.php'
class = 'LinksFromDb'
method = 'GetHead'

script = 'LinksFromDb.php'
class = 'LinksFromDb'
method = 'Gadget'



defined('is_running') or die('Not an entry point...');

class LinksFromDb{

  private $links = array();
  private $errors = array();

  // constructor
  function __construct(){
    // global $LINKS_FROM_DB;
    $connection = mysqli_connect("","user","pass");
    $query = "SELECT url,text FROM link_table";
    $result = mysqli_query($query);
    $error = mysqli_error($connection);
    if( $error != '' ){
      $this->errors[] = $error;
    while( $row = mysqli_fetch_assoc($result) ){
      $this->links[] = array('url' => $row['url'], 'text' => $row['text']);
    // $LINKS_FROM_DB = $this->links;

  public function GetHead(){
    // only exemplary - it could also be done in the gadget method
    // global $LINKS_FROM_DB;
    if( \gp\tool::LoggedIn() ){
      msg('Links from Database fetched: ' . count($this->links) );
      if( !empty($this->errors) ){
        msg('Database Error(s): ' . pre($this->errors) );

  public function Gadget(){ 
    // global $LINKS_FROM_DB;
    echo '<h2>Links</h2>';
    if( !empty($this->links) ){
      echo '<ul class="db-links">';
      foreach( $this->links as $link ){
        echo '<li><a href="' . $link['url'] . '">' . $link['text'] . '</a></li>';
      echo '</ul>';

} /* end of class LinksFromDb */

In addition to $this->links you could also use the currently commented global $LINKS_FROM_DB array, e.g. if you want to output them directly in tempate.php.

Hope it's clear and works (not tested)

Edited: 6 years ago#10586

16 Posts
Great, thanks Juergen. Very helpful!
6 years ago#10587

Topic Closed



elFinder 2.1.50 in Upcoming Release

A new release for Typesetter is in the works with a lot of improvements including the ... Read More

Typesetter 5.1

Typesetter 5.1 is now available for download. 5.1 includes bug fixes, UI/UX improvements, ... Read More

More News

Log In