MyBB Community Forums

Full Version: PHP in Templates and Template Conditionals
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
(2010-03-26, 05:27 AM)Yumi Wrote: [ -> ]
(2010-03-25, 10:31 AM)gokufg Wrote: [ -> ]and the "first post in forum id 36" showed in that forum... But I'd like show "first post in forum id 36" in all threads of forum 36 and i all subforums. Do you understand?
Read up on PHP and how it evaluates conditionals. I'm only going to help with simple requirements, but for more complex things, you really should spend the few minutes to learn how it really works under the hood.

Sure,

can you show me where I can find the variable like "$GLOBALS['forum']['fid'] == 36" ?
The page you're viewing, eg showthread.php if you're in a thread.
I have encountered a problem when pulling data from a language file.

<if $GLOBALS['mybb']->user['usergroup'] == 1 then>{$lang->online_note}<else>{$lang->online_note2}</if>

In the example above, all "asterix" types of code (such as {1} for a number) is shown as it's shown in the language file rather than it's practical counter-part (a number from the database in the case of {1}), but only for the first hook. The hook after <else> converts {1} etc as it should when viewing the page in a browser. I suspect this has something to do with language files already using PHP, but why is then only the hook before <else> affected and not the one after?

Edit: I'm not sure, but could this problem be due to the fact that i simply copied the online_note hook and made a second one just below it in the language file in question? Do i have to do something more than copy and change the name of a hook in a language file in MyBB for it to properly convert asterisk code?

Edit 2: As i feared, this seems to be an issue with MyBB itself. Apparently, you cannot simply copy an entry in a language file and expect the copy to convert asterisks. I found this code in index.php:

$lang->online_note = $lang->sprintf($lang->online_note, my_number_format($onlinecount), $onlinebit, $mybb->settings['wolcutoffmins'], my_number_format($membercount), $memberbit, my_number_format($anoncount), $anonbit, my_number_format($guestcount), $guestbit);
eval("\$whosonline = \"".$templates->get("index_whosonline")."\";");

Copying this code and changing the first part from online_note to online_note2 did however cause another problem. They both convert asterisks properly, but the copy looks exactly like the original; as if you they were using the same text in the language file. I have no idea how to fix this problem or if the author of this plugin does either.
I just got this plugin installed (or so I assume since it doesn't have ANY installation instructions) and I still can't seem to even get an echo statement working from a template.

It only comes with one file, I uploaded that to inc/plugins and activated the plugin from Admin CP.

The PHP and conditionals are actually being output to the browser. The code is not even being parsed... that's a pretty sure shot sign that this plugin is not working for me.

If I put the following code in a template with 'x' replaced with '1' (Guests) and view my forum logged in as Admin (shouldn't display) it still displays.

<if $mybb->user['usergroup'] == x then>
Your link here
</if>

If you were to view source you will see this in the source:

<if Array['usergroup'] == 1 then>
Your link here
</if>

As a second example, the following code:

<?php echo "Hi from PHP"; ?>

would be seen in the source as:

<?php echo "Hi from PHP"; ?>

The plugin seems to be installed wrong or not functional - for whatever reason.

Any advice?



P.S.
(2008-05-31, 07:14 PM)TStarGermany Wrote: [ -> ]you're beating one of the biggest weaknesses in mybb with this...

If I could actually get this to work for me that would be VERY true. The lack of control over logic flow in MyBB's template system is the very reason most of my friends still use SMF.
Created a new custom template file named it as "header_basic"

Then on member_register page called it using this code

<template header_basic>

Doesn't show anything...

What am I doing wrong?
Is this still working with 1.6? (If I change compatibility from "1.4*" to "1.6*" in php file)

EDIT: Yes, it works. "MyBB Compatibility: 1.2.x, 1.4.x, 1.6.x"
OMG, Why I didn't saw this before?
Thanks a LOT for this I was searching for something like this a LOT time ago Smile
An absolute great mod!!!

But I've got a problem with one of the pages I've created at the moment.

It's an interview page (just a form with a lot of text fields), but the script on the send page doesn't work like it does when I build the page manually (not as a template). It checks the field for entries and gives an error when there are empty fields. But instead of opening it in the tabel which I stated in the template it opens it in a black page ...

Like the php part isn't integrated in the page ?!

But the weird thing is that it's working ... because if all the fields are filled, it sends out the mail and gives the 'ok message' inside the tabel. So why doesn't it show the error message inside that same table? (that code is also placed inside the table)....
What's the code you're using? You're probably using a function like die() which stops executing the current script and displays a single message.
This is a part of the send template:

<table border="0" cellspacing="0" cellpadding="5" class="tborder">
<thead>
<tr>
<td class="thead" colspan="5">

<div><strong><a href="caresheet.php">Invul lijst</a></strong><br />
  <div class="smalltext"></div></div>
</td>
</tr>
<tr>
<td class="trow2" valign="top">
<strong><div class="smalltext">

<?php

$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitornick = $_POST['visitornick'];
$visitormail = $_POST['visitormail'];
$vraag1 = $_POST['vraag1'];
$vraag2 = $_POST['vraag2'];
$vraag3 = $_POST['vraag3'];
$vraag4 = $_POST['vraag4'];
$vraag5 = $_POST['vraag5'];
$vraag6 = $_POST['vraag6'];
$vraag7 = $_POST['vraag7'];
$vraag8 = $_POST['vraag8'];
$vraag9 = $_POST['vraag9'];
$vraag10 = $_POST['vraag10'];
$vraag11 = $_POST['vraag11'];
$vraag12 = $_POST['vraag12'];
$vraag13 = $_POST['vraag13'];
$vraag14 = $_POST['vraag14'];
$vraag15 = $_POST['vraag15'];
$vraag16 = $_POST['vraag16'];
$vraag17 = $_POST['vraag17'];
$attn = $_POST['attn'];


if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
echo "<h2>Voer een juist email adres in, gebruik de 'vorige pagina' knop hiervoor</h2>\n";
$badinput = "<h2>De vragenlijst is niet correct ingevult, gebruik de 'vorige pagina' knop hiervoor</h2>\n";
echo $badinput;
die ("Ga terug, gebruik de 'vorige pagina' knop hiervoor");
}

if(empty($visitor) || empty($visitormail) || empty($visitornick) || empty($vraag1) || 
empty($vraag2) || empty($vraag3) || empty($vraag4) || empty($vraag5) || 
empty($vraag6) || empty($vraag7) || empty($vraag8) || empty($vraag9) || 
empty($vraag10) || empty($vraag11) || empty($vraag12) || empty($vraag13) || 
empty($vraag14) || empty($vraag15) || empty($vraag16) || empty($vraag17)) {
echo "<h2>Vul overal iets in, gebruik de 'vorige pagina' knop hiervoor</h2>\n";
die ("Ga terug, gebruik de 'vorige pagina' knop hiervoor");
}


$todayis = date("l, F j, Y, g:i a") ;

$attn = $attn ;
$subject = $attn;

$notes = stripcslashes($notes);

$message = " $todayis [EST] \n

Van: $visitor ($visitormail)\n
Gebruikersnaam: $visitornick\n
Wetenschappelijke naam: $vraag1 \n
Nederlandse Naam: $vraag2 \n
Oorsprong: $vraag3 \n
Habitat: $vraag4 \n
Temperatuur: $vraag5 \n
Luchtvochtigheid: $vraag6 \n
Volwassen grootte: $vraag7 \n
Voeding: $vraag8 \n
Lichturen: $vraag9 \n
Aanbevolen grootte van het verblijf: $vraag10 \n
Gedrag: $vraag11 \n
Karakter: $vraag12 \n
Voortplanting: $vraag13 \n
Opkweek: $vraag14 \n
Extra informatie: $vraag15 \n
Naamsvermelding: $vraag16 \n
Fotos: $vraag17 \n
";

$from = "From: $visitormail\r\n";


mail("[email protected]", $subject, $message, $from);

?>

<p align="center">
<br /><br />
<a href="http://www.hetslangenforum.nl"> Bedankt voor het invullen van de invul lijst, 
klik hier om terug te gaan naar Het Slangenforum </a>
</p>

  <p>
 </td>

</tr>

</table>

If the languare (Dutch) is a problem to read around let me know.

Edit: I've placed some enters in it so it doensn't break out of of the forum here.
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22