vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   PHP function in a template (https://vborg.vbsupport.ru/showthread.php?t=158129)

T_Richardson 09-18-2007 10:53 AM

PHP function in a template
 
I have this users.php function code to list online users. I would like to include this on a non-vb page that uses templates.

PHP Code:

<?
chdir('./');
require_once('./global.php');

// ---------------------------------------------------
    // Start Globalized Function - LoadTemplate
    // ---------------------------------------------------

    function LoadTemplate($template = ""){

    $template = "vBExternal/{$template}";

        if(!file_exists($template)){
        RunError("System was unable to find the template '{$template}'");
        }

        if(!$Handler = fopen($template,'r')){
        RunError("System was unable to open the template '{$template}'");
        }

    $template = fread($Handler,filesize($template));
    fclose($Handler);

    return $template;
    }
    
    // ---------------------------------------------------
    // Start Globalized Function - RunError
    // ---------------------------------------------------

    function RunError($message = ""){
    echo "<font size='1' face='verdana'>There was an error while processing vBExternal:<br />{$message}</font>";
    exit;
    }

    // ---------------------------------------------------
    // End Globalized Function - RunError
    // ---------------------------------------------------
    
    // ---------------------------------------------------
    // Start Globalized Function - ParseTemplate
    // ---------------------------------------------------

    function ParseTemplate($template, $parser = array(), $doGlobals = 0){
    global $vbulletin;

        if(is_array($parser)){
            foreach($parser as $find => $replace){
            $template = str_replace("{".$find."}", $replace, $template);
            }
        } else if($doGlobals){
        $RepGlobals = array(
                    'url' => $vbulletin->options['bburl'],
                );

            foreach($RepGlobals as $find => $replace){
            $template = str_replace("{".$find."}", $replace, $template);
            }

        }

    return $template;
    }

    // ---------------------------------------------------
    // End Globalized Function - ParseTemplate
    // ---------------------------------------------------

function output_UsersOnline(){
    global $db, $Data, $vbulletin;

    // Load Template
    $Template = LoadTemplate("users_online.html");

    $cache = array();

    // Collect Data
    $datecut      = TIMENOW - $vbulletin->options['cookietimeout'];
    $UsersOnline = $db->query("
        SELECT
            user.username, (user.options) AS invisible, user.usergroupid,
            session.userid,
            IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid
        FROM " . TABLE_PREFIX . "session AS session
        LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = session.userid)
        WHERE session.lastactivity > $datecut and user.userid > 0
        " . iif($vbulletin->options['displayloggedin'] == 1, "ORDER BY username ASC") . "
    ");

        while($User = $db->fetch_array($UsersOnline)){
            if(!$cache[$User['userid']]){
            $cache[$User['userid']] = $User['userid'];

            $Data .= ParseTemplate($Template,
                                array(
                                    'userid'     => $User['userid'],
                                    'username'   => $User['username'],
                                )
                );
            }
        }

    unset($cache);
    doOutput();
    }

    function doOutput(){
    global $Data, $vbulletin;

    $Data = str_replace('images/',"{$vbulletin->options[bburl]}/images/",$Data);

    echo ParseTemplate($Data,"",1);
    $Data = "";
    }
   
    ?>



That php file works if you include output_UsersOnline() in the file itself.

What would be the proper way construct this code in a plugin so I can call it in a template with a variable ie; $onlineusers

Guest190829 09-18-2007 11:14 AM

If you are including global.php already, why making custom functions for already built in functionality? (Fetching templates and parsing them.)

Have you looked at this tutorial? :

https://vborg.vbsupport.ru/showthread.php?t=98009

It should point you in the right direction.

T_Richardson 09-18-2007 11:16 AM

The template that it's parsing is an html page that puts the users online in clickable link to they're profile.

T_Richardson 09-19-2007 08:38 PM

Anyone have any idea. Surely there's some guru out there that can point me in the right direction :)


All times are GMT. The time now is 10:21 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01091 seconds
  • Memory Usage 1,732KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (4)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete