简体   繁体   中英

Joomla plugin for dispay data from database

Hy,

i am trying to make a plugin for joomla to display data from database in article. The problem is that when the data is printed from the database is not showing data where is the tag ({myplugintag}), now display the data above the title of the article.

I put a tag inside of the article to get the data

{myplugintag}gameid{/myplugintag}

<?php


defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.plugin.plugin' );



class plgContentNamePlugin extends JPlugin
{
function PluginNamePlugin( &$subject )
{
    parent::__construct( $subject );
}
function onContentPrepare( $context, &$article, $page = 0)
    {
    global $mainframe;
    if ( JString::strpos( $article->text, '{myplugintag}' ) === false ) {
    return true;
    }
    $article->text = preg_replace_callback('|{myplugintag}(.*){\/myplugintag}|m',function($m) {           return $this->getResults($m[1]); }, $article->text);
    return true;
}

function getResults($gameID)
{
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    $query->select('*')
    ->from('#__component_table')
    ->where($db->quoteName('gameid') . ' = '. $db->quote($gameID));
    $db->setQuery($query);    
    $result = $db->loadObjectList();
    foreach ($result as $var){$playerid=$var->playerid;
    print $playerid;} 
    }
}

Thanks in advance

Instead of printing the result you just return the result.

$playerid = '';
foreach ($result as $var){
        $playerid .= $var->playerid;

} 
return $playerid;   

The reason is when you print the data it prints before component output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM