PHP variables in templates

Tutorials for <a href="http://www.phpbbstyles.com/viewtopic.php?t=356">eXtreme Styles mod</a> for phpBB 2.0.

Moderator: Moderators

PHP variables in templates

Postby PostBot on Wed Nov 26, 2003 11:42 am

How to use php variables in templates

Templates are included inside function, so that causes some problems for coders who are not experienced with php.


If you want to use global variables like $HTTP_GET_VARS you must declare it global before using global.
Example:
Code: Select all
<?php
global $HTTP_COOKIE_VARS;
if(isset($HTTP_COOKIE_VARS['my_cookie']))
{
echo 'Cookie "my_cookie" is set';
}
?>



Using variables from url ('get' variables).
If script is called like index.php?var1=value1 and you want to use that var1 then you should use this:
Code: Select all
<?php
global $HTTP_GET_VARS;
echo 'var1 = ', $HTTP_GET_VARS['var1'];
?>

or this:
Code: Select all
<?php
echo 'var1 = ', $_GET['var1'];
?>

Variable $HTTP_GET_VARS must be declared global before it is used, but variable $_GET is always global so no need to declare it.

And i suggest not to use $var1 because global variables might be dangerous. Better use array $HTTP_GET_VARS or $_GET.
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby akadis on Mon May 31, 2004 5:48 am

um i have a question that i believe relates to this, if not im sorry please move this post....
im using the following php:
Code: Select all
<?php
$contract =  $phpbb_root_path . 'contract/' . $userdata['user_id'] . '.php';

if ($userdata['user_id'] == ANONYMOUS) 

triggor_error("I'm sorry, you must be logged in to use this feature.");
exit;
}
if (file_exists($contract)) { 
include($contract); 
     } else { 
     echo "I'm sorry, but you do not have a contract yet."; 
     }
?>

it works fine in a regular php file, but doesn't work in a template with extreme styles installed ( it shows "I'm sorry, but you do not have a contract yet." when it should include the file...) as i said its not a problem i know with "$contract = ...." going to the wrong place, it has to be a problem with my syntax, can u help me?
akadis
Registered User
Registered User
 
Posts: 2
Joined: Mon May 31, 2004 5:43 am

Postby PostBot on Mon May 31, 2004 7:56 am

after <?php put this code:
Code: Select all
global $contact, $userdata, $phpbb_root_path;
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby akadis on Mon May 31, 2004 3:41 pm

cool it works perfectly, thank you very much :D
akadis
Registered User
Registered User
 
Posts: 2
Joined: Mon May 31, 2004 5:43 am

Postby EoN on Wed Aug 25, 2004 9:53 am

Hi Everyone,

CyberAlien, thanks for an excellent mod. It's great!

I have a slight problem. I have my displayHeader() and displayFooter() as methods of an $objSite object. When I call $objSite->displayHeader(); in overall_header.tpl it works perfectly, but when i call $objSite->displayFooter(); in overall_footer.tpl I'm getting a "Fatal error: Call to a member function on a non-object in tpl_subSilver.overall_footer.php"

The code I have ADDED is more or less like this:

overall_header.tpl
Code: Select all
<?php

require_once("../include/functions.php"); //Includes line $objSite = new MainSite(); as well as MainSite Class definition

$objSite->displayHeader("home");
?>


overall_footer.tpl
Code: Select all
$objSite->displayFooter(); //Throws Error


Even when I added global $objSite; in the overall_footer.tpl (before the $objSite->displayFooter();) it still throws the same error. Do you have any ideas how I can make this work? Is it something to do with the scope of the object/variable?

Kind Regards,
EoN
EoN
Registered User
Registered User
 
Posts: 2
Joined: Wed Aug 25, 2004 9:45 am

Postby PostBot on Wed Aug 25, 2004 9:55 am

Those 2 tpl files are included inside function, so to use same variables across different tpl files you need to declare those variables as global. Add this in overall_header.tpl after include() and in overall_footer.tpl after <?php:
Code: Select all
global $objSite;
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby EoN on Wed Aug 25, 2004 10:14 am

Thanks CyberAlien. I had added it to the overall_footer.tpl, and it still didnt work, but when I added it to the overall_header.tpl as well it worked fine! (Out of interest, I had to add global $objSite; BEFORE the include(); in the header for it to work)

Thanks again for a gread Mod, and thanks for your very quick response. Much appreciated :)

EoN.
EoN
Registered User
Registered User
 
Posts: 2
Joined: Wed Aug 25, 2004 9:45 am

Postby JAS4Yeshua on Fri Sep 03, 2004 7:58 pm

I am trying to include php variables in bbcode.tpl. Is this possible?

Running phpBB 2.0.10
eXtreme Mod 2.0.1
Jason
Servant of the Lord
JAS4Yeshua
Registered User
Registered User
 
Posts: 4
Joined: Fri Sep 03, 2004 7:49 pm

Postby PostBot on Fri Sep 03, 2004 8:24 pm

No, bbcode.tpl is parsed differently than other tpl files and you can't use php code in it
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby Hyperion on Sun Nov 21, 2004 12:55 am

I was wondering what I have to do to bypass page_header.php in installed the Advanced IP Tools Pack.

I do not have access to that file because I have free hosting and the google banner is on the top. I could not get paid hosting at this time.

First, here was the post by which the suggestion to use eXtreme Styles was given:

pentapenguin wrote:If you can't modify it, I think you would be able to acheive the same effect if you wrapped the code in PHP if/else tags if you have CyberAlien's eXtreme Styles MOD installed which allows PHP in templates.


I do have that MOD installed. I'll check with the author.

Thanks.


Here's the original thread starting from where I asked the question: http://www.phpbb.com/phpBB/viewtopic.ph ... 03#1322103

The instructions for the MOD are on the top.
User avatar
Hyperion
Registered User
Registered User
 
Posts: 114
Joined: Tue Nov 09, 2004 7:29 am

Postby pentapenguin on Sun Nov 21, 2004 6:57 am

Try this and let me know.
Sorry but I can't test it just yet. :(
(This is customized parts of it-still do the other parts.)
Code: Select all
#
#-----[ OPEN ]------------------------------------------
#
includes/usercp_viewprofile.php


#
#-----[ FIND ]------------------------------------------
#
$template->pparse('body');

#
#-----[ BEFORE, ADD ]------------------------------------------
#
//start Advanced IP Tools Pack MOD
//Let's see if the user viewing this page is an admin or mod, if not, we can save two database queries! :P
if ( ($userdata['user_level'] == ADMIN) || ($userdata['user_level'] == MOD) )
{
   if ($decoded_ip != '')
   {
   //Get everyone else who has registered under this IP address
   $sql = "SELECT user_id, username, user_registered_ip, user_registered_hostname FROM " . USERS_TABLE . " WHERE user_registered_ip = '" . $encoded_ip . "' AND user_id != '" . $profiledata['user_id'] . "'";
   
      if ( ! ($result = $db->sql_query($sql) ) )
      {
         message_die(GENERAL_ERROR, 'Error looking up other IP addresses.', '', __LINE__, __FILE__, $sql);
      }

      while ( $row = $db->sql_fetchrow($result) )
      {
         $user_id = $row['user_id'];
         $username = $row['username'];
         $user_registered_hostname = $row['user_registered_hostname'];

         $template->assign_block_vars("OTHER_REGISTERED_IPS",array(
            'USER_NAME' => $username,
            'U_PROFILE' => append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . "=$user_id"),
            'USER_HOSTNAME' => $user_registered_hostname,
              ));
      }
   }

   //Get a list of all the IPs this user has posted from
   $sql = "SELECT poster_ip, COUNT(*) AS postings FROM " . POSTS_TABLE . " WHERE poster_id = '" . $profiledata['user_id'] . "' GROUP BY poster_ip ORDER BY " . (( SQL_LAYER == 'msaccess' ) ? 'COUNT(*)' : 'postings' ) . " DESC";

      if ( ! ($result = $db->sql_query($sql) ) )
      {
         message_die(GENERAL_ERROR, 'Error looking up all IP addresses this user has posted from.', '', __LINE__, __FILE__, $sql);
      }

      while ( $row = $db->sql_fetchrow($result) )
      {
         $poster_ip = $row['poster_ip'];

         $template->assign_block_vars("ALL_IPS_POSTED_FROM",array(
            'POSTER_IP' => decode_ip($poster_ip),
            'POSTS' => $row['postings'] . ' ' . ( ( $row['postings'] == 1 ) ? $lang['Post'] : $lang['Posts'] ),
            'U_POSTS_LINK' => append_sid("search.$phpEx?mode=results&amp;search_author=" . urlencode($profiledata['username']) . "&amp;search_ip=" . decode_ip($poster_ip) ),
              ));
      }
}
//end Advanced IP Tools Pack MOD


#
#-----[ OPEN ]------------------------------------------
#
templates/subSilver/profile_view_body.tpl

#
#-----[ FIND ]------------------------------------------
#

//--></script><noscript>{ICQ_IMG}</noscript></td>
      </tr>
     </table>
   </td>
  </tr>

#
#-----[ AFTER, ADD ]------------------------------------------
#

<?php
global $userdata;
if ( ($userdata['user_level'] == ADMIN) || ($userdata['user_level'] == MOD) )
{
?>

<tr>
  <th class="thHead" colspan="2" align="center">{L_MODERATOR_IP_INFORMATION}</th>
</tr>

<tr>
  <td class="row1" colspan="2"><span class="gen">{L_EMAIL_ADDRESS}: <a href="mailto:{USER_EMAIL_ADDRESS}">{USER_EMAIL_ADDRESS}</a></span></td>
</tr>

<tr>
  <td class="row1" colspan="2"><span class="gen">{L_REGISTERED_IP_ADDRESS}: <a href="http://whois.sc/{USER_IP_ADDRESS}" target="_blank">{USER_IP_ADDRESS}</a></span></td>
</tr>

<tr>
  <td class="row1" colspan="2"><span class="gen">{L_REGISTERED_HOSTNAME}: {USER_REGISTERED_HOSTNAME}</span></td>
</tr>

<tr>
  <th class="thHead" colspan="2" align="center">{L_OTHER_REGISTERED_IPS}</th>
</tr>

<!-- BEGIN OTHER_REGISTERED_IPS -->
<tr>
  <td class="row1"><span class="gen"><a href="{OTHER_REGISTERED_IPS.U_PROFILE}">{OTHER_REGISTERED_IPS.USER_NAME}</a></span></td>
  <td class="row1"><span class="gen">{OTHER_REGISTERED_IPS.USER_HOSTNAME}</span></td>         
</tr>
<!-- END OTHER_REGISTERED_IPS -->

<tr>
  <th class="thHead" colspan="2" align="center">{L_OTHER_IPS}</th>
</tr>

<!-- BEGIN ALL_IPS_POSTED_FROM -->
<tr>
  <td class="row1" colspan="2"><span class="gen"><a href="http://whois.sc/{ALL_IPS_POSTED_FROM.POSTER_IP}" target="_blank">{ALL_IPS_POSTED_FROM.POSTER_IP}</a> [ <a href="{ALL_IPS_POSTED_FROM.U_POSTS_LINK}">{ALL_IPS_POSTED_FROM.POSTS}</a> ] </span></td>
</tr>
<!-- END ALL_IPS_POSTED_FROM -->

<?php
}
?>


#
#-----[ OPEN ]------------------------------------------
#
templates/subSilver/search_body.tpl

#
#-----[ FIND ]------------------------------------------
#
<tr>
   <td class="row1" colspan="2"><span class="gen">{L_SEARCH_AUTHOR}:</span><br /><span class="gensmall">{L_SEARCH_AUTHOR_EXPLAIN}</span></td>
   <td class="row2" colspan="2" valign="middle"><span class="genmed"><input type="text" style="width: 300px" class="post" name="search_author" size="30" /></span></td>
</tr>

#
#-----[ AFTER, ADD ]------------------------------------------
#
<?php
global $userdata;
if ( ($userdata['user_level'] == ADMIN) || ($userdata['user_level'] == MOD) )
{
?>

<tr>
   <td class="row1" colspan="2"><span class="gen">{L_SEARCH_IP}:</span><br /><span class="gensmall">{L_SEARCH_IP_EXPLAIN}</span></td>
   <td class="row2" colspan="2" valign="middle"><span class="genmed"><input type="text" style="width: 300px" class="post" name="search_ip" size="30" /></span></td>
</tr>

<?php
}
?>
User avatar
pentapenguin
Moderator
Moderator
 
Posts: 1466
Joined: Tue Sep 14, 2004 4:15 pm
Location: Georgia, USA

Postby Hyperion on Sun Nov 21, 2004 7:34 pm

Wait... so with this, I can skip page_header.php, but I'm to edit the rest of the files for Advanced IP Tools Pack, right?
User avatar
Hyperion
Registered User
Registered User
 
Posts: 114
Joined: Tue Nov 09, 2004 7:29 am

Postby pentapenguin on Sun Nov 21, 2004 7:58 pm

[quote user="Hyperion" post="7861"]Wait... so with this, I can skip page_header.php, but I'm to edit the rest of the files for Advanced IP Tools Pack, right?[/quote]
Yes, install it using the directions on the thread over at phpbb.com
Just skip pages
includes/usercp_viewprofile.php
templates/subSilver/search_body.tpl
templates/subSilver/profile_view_body.tpl

Then go back and MOD those pages as described immediately above. :)
User avatar
pentapenguin
Moderator
Moderator
 
Posts: 1466
Joined: Tue Sep 14, 2004 4:15 pm
Location: Georgia, USA

Postby superruzafa on Mon Feb 28, 2005 2:07 pm

I have this code in a .php file included in "overall_header.tpl".

Code: Select all
...

$tags=array();

function xmlStartTag($xmlParser, $name, $attrs) {
    global $tags;
    array_push($tags, $name);

    ...


However it says Warning: array_push(): First argument should be an array...

Anyone knows why happens this.
...and so I become a webmaster
superruzafa
Registered User
Registered User
 
Posts: 1
Joined: Mon Feb 28, 2005 1:54 pm

Postby nix on Mon Apr 04, 2005 5:08 am

Hi

I need to access the phpbb variable $forum_name in overall_header.tpl
I added in overall_header.tpl

<?php
global $forum_name;
echo $forum_name;
?>

It is ok with view_topic.php but not with view_forum.php
I have added to view_forum.php

$forum_name = $forum_row['forum_name'];

but doesnt work.

any advice is appreciated thanks!
nix
Registered User
Registered User
 
Posts: 2
Joined: Mon Apr 04, 2005 5:01 am

Next

Return to eXtreme Styles mod tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron