ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Tip: How to use a Global Variable in your Forums

March 05, 2009 2:42pm

Subscribe [3]
  • #1 / Mar 05, 2009 2:42pm

    sfgolfer

    41 posts

    Update: Code has been updated to read the database file from EE’s config file.


    Task at Hand:

    You want to use a template Global Variable in your Forums. 

    You discovered the hard way that the Forums has its own templating system and you are now tweaking the Forum templates.  Instead of recreating the content of a Global Variable into your Forum templates, you simply want to use the actual site Global Variable inside the Forums templates. 

    Good idea.  This way you only have to edit the Global Variable in one place. 

    Then you discover that you cannot use a Global Variable outside the Site’s Templates and in your Forums.


    Background:

    Global Variables are meant to be used only in the Site Templates because only the site template parsing can parse the Global Variables.  Remember that Global Variables can contain only static content (per EE’s guidelines at http://expressionengine.com/docs/templates/globals/user_defined.html) which may fool you into thinking that they can be used in the Forums.  However, the Forums template parsing cannot “read” (aka parse) the site template Global Variables.


    Possible Solution:

    Since we cannot access a Global Variable via EE from the Forums, lets retrieve the contents of the Global Variable by going directly to the database.


    Example Scenario:

    You have a template Global Variable for a menu which you would also like to use in the Forums templates.


    Steps:

    1) Identify which Global Variables you want to use.  In my example scenario, I want to use two variables called mainmenu and footermenu.  Each has static content.


    2) In the Forum templates, edit the desired template to where you want to use your Global Variable. 

    In my case, I edited Global Templates > HTML Header and Global Templates > HTML Footer.

    Place this in the desired spot or spots if you want to use more than one Global Variable.  Just change $globalvariable in each instance:

    <?php 
    $globalvariable="mainmenu";  // template Global Variable to be passed to the php script
    include("themes/forum_themes/themename/scripts/menu.php"); 
    ?>


    3) Create menu.php in the above directory using the code below.  Obviously, you will need either direct access to the server or FTP to your web host.

    <?php 
    
    global $PREFS; 
    
    // pull database info from config file
    $dbname = $PREFS->core_ini['db_name'];
    $hostname = $PREFS->core_ini['db_hostname'];
    $dbuser = $PREFS->core_ini['db_username'];
    $dbpass = $PREFS->core_ini['db_password'];
    
    // query the database
    $dbhandle = mysql_connect($hostname, $dbuser, $dbpass) or die("Connection Failure to Database");
    mysql_select_db($dbname, $dbhandle) or die ($dbname . " Database not found. " . $dbuser);
    
    $query = "SELECT variable_data FROM exp_global_variables WHERE variable_name='". $global_variable . "'"; 
    
    $result = mysql_db_query($dbname, $query) or die("Failed Query of: " . $query);
    
    $thisrow = mysql_fetch_row($result);
    if ($thisrow)  //if the results of the query are not null
    {
      // output the results
      echo $thisrow[0];  // there should only be one row returned
    }
    ?>

    Recommended placement of menu.php is in your themes directory in a separate folder off the root; otherwise, you may notice some odd behavior in the Control Panel. 

    You can optionally ignore the initial code in Step 2 and instead put the code above directly in your Forum templates.  But in the example scenario, I have a Main Menu and a Footer Menu and did not want to duplicate the code.


    Explanation:

    The result should be that the Global Variable’s contents will be parsed out as static content in the Forum templates.

    Since we cannot retrieve the desired Global Variable via EE, we are going directly to the database and reading the contents of the variable.

    This method can probably be fine tuned but is my attempt to solve a situation I ran into.

    There are other scenarios on why you would want to use a Global Variable but at least this should allow you to access the Global Variable’s content.

  • #2 / Mar 05, 2009 11:42pm

    Hoosteeno

    109 posts

    Cool!

    Another option might be to run the forum through the standard template engine.  I haven’t tested, but don’t see why this wouldn’t work.

    We’ve done this on several sites where the forum header and the site header are shared, and we don’t want to maintain them in two places.

    Justin

  • #3 / Mar 06, 2009 2:27am

    Ingmar

    29245 posts

    <?php
    $dbuser = "";  // fill in
    $dbpass = "";  // fill in
    $dbname = "";  // fill in
    // $globalvariable="";  // use only if needed

    If you don’t want to use the database class (and you should), at least use something like:

    <?php global $PREFS; 
    
    $db_name  = $PREFS->core_ini['db_name'];
    $hostname = $PREFS->core_ini['db_hostname'];
    $username = $PREFS->core_ini['db_username'];
    $password = $PREFS->core_ini['db_password'];
    
    .... ?>

    to pull that data automatically from config.php.

  • #4 / Mar 06, 2009 10:51am

    sfgolfer

    41 posts

    <?php global $PREFS;

    $db_name = $PREFS->core_ini[‘db_name’]);
    $hostname = $PREFS->core_ini[‘db_hostname’];
    $username = $PREFS->core_ini[‘db_username’];
    $password = $PREFS->core_ini[‘db_password’]);

    .... ?>

    Small syntax errors ... parentheticals ‘)’ at the end produces parsing errors.

  • #5 / Mar 06, 2009 10:58am

    sfgolfer

    41 posts

    I like the change to pull the database info from the config file. 

    I updated the code in Step 3 of the first post…

  • #6 / Mar 06, 2009 11:00am

    Ingmar

    29245 posts

    <?php global $PREFS;

    $db_name = $PREFS->core_ini[‘db_name’]);
    $hostname = $PREFS->core_ini[‘db_hostname’];
    $username = $PREFS->core_ini[‘db_username’];
    $password = $PREFS->core_ini[‘db_password’]);

    .... ?>

    Small syntax errors ... parentheticals ‘)’ at the end produces parsing errors.

    Thanks, that’s what I get for copy & pasting without checking 😊 I corrected my code, thanks for pointing it out.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases