Topic Closed
derek
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:

[GetHead]
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.

2 years ago#10585

juergen
1.5K Posts
54.4K Downloads
16 Plugins
design, web development & visual effects

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.ini:

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

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

[Gadget:LinksFromDb]
script = 'LinksFromDb.php'
class = 'LinksFromDb'
method = 'Gadget'

 

LinksFromDb.php:

<?php
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("127.0.0.1","user","pass");
    mysqli_select_db("link_database");
    $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']);
    }
    mysql_close($connection);
    // $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: 2 years ago#10586

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

Topic Closed

 

News

elFinder 2.1.50 in Upcoming Release
12/28/2019

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

Typesetter 5.1
8/12/2017

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

More News

HH-Support

Company located in T├│rshavn, Faroe Islands. * Webpage Design * Consultant & Provider of a wide range of programs for visually impaired and dyslextics.

Find out more about our Provider Spotlight

Log In

  Register