Template assign block vars in a function...

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
Slechtvalk
Registered User
Posts: 34
Joined: Tue Jan 15, 2008 12:56 pm

Template assign block vars in a function...

Post by Slechtvalk » Sat May 02, 2015 3:52 pm

Hello,

i need some help.

i outsourced database selects for parts of custom pages into a separate mechfunctions.php

the function i now added is

Code: Select all

function get_hangar() {
  $mliste = mysql_query("SELECT ucase(mname_long) as gmname_long, mname, mtons, mech_id, smurfy FROM phpbb3_mechs ORDER BY mtons, mname_long, mname");
  while ($mrow = mysql_fetch_array($mliste, MYSQL_ASSOC)) {
    $mid=$mrow["mech_id"];
    $basicliste = mysql_query("SELECT GROUP_CONCAT(ucase(u.username) ORDER BY ucase(u.username) ASC SEPARATOR '<br>') AS basic, mech_id FROM phpbb3_pilot_mech_skill b LEFT JOIN phpbb3_users u ON b.user_id = u.user_id LEFT JOIN phpbb3_user_group g ON u.user_id = g.user_id WHERE (b.mech_id='$mid' AND b.skill='basic' AND g.group_id='145' AND g.user_pending='0')");
    $brow = mysql_fetch_array($basicliste);
    $eliteliste = mysql_query("SELECT GROUP_CONCAT(ucase(u.username) ORDER BY ucase(u.username) ASC SEPARATOR '<br>') AS elite, mech_id FROM phpbb3_pilot_mech_skill e LEFT JOIN phpbb3_users u ON e.user_id = u.user_id LEFT JOIN phpbb3_user_group g ON u.user_id = g.user_id WHERE (e.mech_id='$mid' AND e.skill='elite' AND g.group_id='145' AND g.user_pending='0')");
    $erow = mysql_fetch_array($eliteliste);
    $masteryliste = mysql_query("SELECT GROUP_CONCAT(ucase(u.username) ORDER BY ucase(u.username) ASC SEPARATOR '<br>') AS mastery, mech_id FROM phpbb3_pilot_mech_skill m LEFT JOIN phpbb3_users u ON m.user_id = u.user_id LEFT JOIN phpbb3_user_group g ON u.user_id = g.user_id WHERE (m.mech_id='$mid' AND m.skill='mastery' AND g.group_id='145' AND g.user_pending='0')");
    $masrow = mysql_fetch_array($masteryliste);
    $besitzliste = mysql_query("SELECT GROUP_CONCAT(ucase(u.username) ORDER BY ucase(u.username) ASC SEPARATOR '<br>') AS owned, mech_id FROM phpbb3_pilot_mech_skill o LEFT JOIN phpbb3_users u ON o.user_id = u.user_id LEFT JOIN phpbb3_user_group g ON u.user_id = g.user_id WHERE (o.mech_id='$mid' AND o.owned='1' AND g.group_id='145' AND g.user_pending='0')");
    $besitzrow = mysql_fetch_array($besitzliste);
    $favoritliste = mysql_query("SELECT GROUP_CONCAT(ucase(u.username) ORDER BY ucase(u.username) ASC SEPARATOR '<br>') AS fav, mech_id FROM phpbb3_pilot_mech_skill f LEFT JOIN phpbb3_users u ON f.user_id = u.user_id LEFT JOIN phpbb3_user_group g ON u.user_id = g.user_id WHERE (f.mech_id='$mid' AND f.fav='1' AND g.group_id='145' AND g.user_pending='0')");
    $favoritrow = mysql_fetch_array($favoritliste);
      
    $template->assign_block_vars('hangar_liste', array(
        'MECH_NAME'        => $mrow["gmname_long"],
        'MECH_MODELL'        => $mrow["mname"],
        'SMURFY'        => $mrow["smurfy"],
        'MECH_TONS'            => $mrow["mtons"],
        'OWNED_LIST'            => $besitzrow["owned"],
        'BASIC_LIST'            => $brow["basic"],
        'ELITE_LIST'            => $erow["elite"],
        'MASTERY_LIST'            => $masrow["mastery"],
        'FAV_LIST'              => $favoritrow["fav"],
    ));
    }
}
The mechfuntions.php is included into the custom page, now when using the function i get a blank page with error
Fatal error: Call to a member function assign_block_vars() on a non-object in ......./mechfunctions.php on line 18
How can i use assign block vars in functions? or do i need a temporary array?

Thank you!

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 2408
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Template assign block vars in a function...

Post by kinerity » Sat May 02, 2015 11:39 pm

Can you post the code for the custom page? Your function looks OK at first glance.
Kailey Truscott - Community Team

User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: Template assign block vars in a function...

Post by javiexin » Sun May 03, 2015 1:11 am

Have you defined or imported $template?

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51890
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Template assign block vars in a function...

Post by Brf » Sun May 03, 2015 2:58 am

javiexin wrote:Have you defined or imported $template?
Indeed. You cannot use $template if it is not globalized in the function.

Slechtvalk
Registered User
Posts: 34
Joined: Tue Jan 15, 2008 12:56 pm

Re: Template assign block vars in a function...

Post by Slechtvalk » Sun May 03, 2015 9:23 am

ok added
function get_hangar() {
global $template;
....
}

and the error is gone. Thank you!

funnily, the select does not give any result, well thats another problem....

html template:

Code: Select all

<!-- INCLUDE overall_header.html -->

<!-- IF S_GROUP_145 -->

{S_DB_INFO}

Anzahl bekannter Mechs in comstar_military_unclassified: {S_MECH_COUNT}<br><br>

<table class="table1" cellspacing="1">
    <tr>
        <th>Mech</th>
        <th>Modell</th>
        <th>Tonnage</th>
        <th>im Besitz von</th>
        <th>Basis Skill</th>
        <th>Elite Skill</th>
        <th>Mastery Skill</th>
        <th>Favorisiert von</th>
    </tr>

<!-- BEGIN hangar_liste -->
    <tr class="<!-- IF hangar_liste.S_ROW_COUNT is even -->bg1<!-- ELSE -->bg2<!-- ENDIF -->">
        <td>{hangar_liste.MECH_NAME}</td>
        <td>{hangar_liste.MECH_MODELL}</td>
        <td>{hangar_liste.MECH_TONS}</td>
        <td>{hangar_liste.OWNED_LIST}</td>
        <td>{hangar_liste.BASIC_LIST}</td>
        <td>{hangar_liste.ELITE_LIST}</td>
        <td>{hangar_liste.MASTERY_LIST}</td>
        <td>{hangar_liste.FAV_LIST}</td>
    </tr>
    <!-- END block_name -->
</table>

<!-- ELSE -->
Sie haben keine Berechtigung diesen Bereich zu betreten. Sie m&uuml;ssen Mitglied der Gruppe "Aktive MWO Piloten" sein!

<!-- ENDIF -->
 
<!-- INCLUDE overall_footer.html -->

custom site:

Code: Select all

<?php

define('IN_PHPBB', true);
// Specify the path to you phpBB3 installation directory.
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
// The common.php file is required.
include($phpbb_root_path . 'common.' . $phpEx);
include('./mechfunctions.php');
// Start session management
$user->session_begin();
$auth->acl($user->data);

if ($user->data['user_id'] == ANONYMOUS)
{
	login_box();
}

$user->setup();


$user_id=$user->data['user_id'];
$username=$user->data['username'];


//your PHP and/or HTML code goes here
include '../config.php';
$link = mysql_connect($dbhost, $dbuser, $dbpasswd);
if (!$link) 
  {
  die('Verbindung schlug fehl: ' . mysql_error());
  }
else
  {
  $db_info = "Erfolgreich mit Comstar verbunden";
  $db_selected = mysql_select_db($dbname, $link);
  if (!$db_selected) 
    {
    die ('Kann Datenbank nicht benutzen : ' . mysql_error());
    }
  else
    {
    $db_info .= " - Daten Download .....100% - ";
    }
}

$anzahl = "SELECT mech_id FROM phpbb3_mechs";
$ergebnis = mysql_query($anzahl);
$mech_count = mysql_num_rows($ergebnis);

get_hangar();
mysql_close($link);



// Page title, this language variable should be defined in the language file you setup at the top of this page.
page_header('Hangar');

$template->set_filenames(array(
    'body' => 'mech_neu.html',
));

$template->assign_vars(array(
	'S_MECH_COUNT'		=> $mech_count,
  'S_DB_INFO' => $db_info
));

// Completing the script and displaying the page.
page_footer();
?>

Slechtvalk
Registered User
Posts: 34
Joined: Tue Jan 15, 2008 12:56 pm

Re: Template assign block vars in a function...

Post by Slechtvalk » Sun May 03, 2015 9:40 am

<!-- END block_name -->

there was the error.

Thank you!

Post Reply

Return to “Extension Writers Discussion”