MyBB Community Forums

Full Version: Header/Logo based on Thread ID
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys,

I'm trying to place a logo underneath my header, which depends on the thread id, so with other words: I want a certain logo/image above a selected/certain thread. Wink

I used a simple line underneath the header variable in my showthread template:

{$header}
<center><img src="images/fanclub/{$thread['tid']}.png"></center>

This way works, but it puts an image above EVERY thread, and I cant select it only for a certain thread.

That's why I tried an if-statement in the showthread template:

<script type="text/javascript">
function logo () {var thread = "{$thread['tid']}"; var bild = "0";
    if (thread == 3) {bild = "logo";  }
}
</script>

...

<body onload="logo()">
{$header}
<center><img src="images/fanclub/{$bild}.png"></center>

I wanted to test, if I can make it work for the thread #3, while every other thread gets an ivisible logo ("0.png").

Sadly, it didnt work... ^^ And since my programming skills aren't really good enough to call them "skills", I need your help now.

What did I do wrong in the function logo () ? And how can I do it correctly?

Thx Guys Smile
You can't convert Javascript variable to PHP..

Delete this code and activate Template Conditionals: http://mybbhacks.zingaburga.com/showthread.php?tid=464 (both .txt and .php files required). Then put in showthread template:
<img src="images/fanclub/<if $thread['tid'] == 3 then>logo<else>0</if>.png" alt="" />

Or you can use proper JS. Put before </head> in showthread:
<script type="text/javascript">
document.observe('dom:loaded', function(){
   var l_tid = {$thread['tid']};

   if(l_tid == 3)
   document.getElementById('ImageBasedOnTid').src = "images/fanclub/logo.png";
});
</script>
And this anywhere else in showthread:
<img id="ImageBasedOnTid" src="images/fanclub/0.png" alt="" />
{$header}
<center><img src="images/fanclub/header.php?tid={$thread['tid']}"></center>

Then, create images/fanclub/header.php which looks at the thread id and serves up the appropriate header image (don't forget to send the content-type header!). You can search for php image rotators for sample code.
(2014-03-14, 07:27 PM)Destroy666 Wrote: [ -> ]Or you can use proper JS. Put before </head> in showthread:
<script type="text/javascript">
document.observe('dom:loaded', function(){
   var l_tid = {$thread['tid']};

   if(l_tid == 3)
   document.getElementById('ImageBasedOnTid').src = "images/fanclub/logo.png";
});
</script>
And this anywhere else in showthread:
<img id="ImageBasedOnTid" src="images/fanclub/0.png" alt="" />

Thank you, that is really helpful and it works perfectly.

Now since oyu werent the only one who suggested conditional templates to me, I was wondering: Does the "proper JS" have any risks/disadvantages?
(2014-03-14, 11:00 PM)Gobee129 Wrote: [ -> ]
(2014-03-14, 07:27 PM)Destroy666 Wrote: [ -> ]Or you can use proper JS. Put before </head> in showthread:
<script type="text/javascript">
document.observe('dom:loaded', function(){
   var l_tid = {$thread['tid']};

   if(l_tid == 3)
   document.getElementById('ImageBasedOnTid').src = "images/fanclub/logo.png";
});
</script>
And this anywhere else in showthread:
<img id="ImageBasedOnTid" src="images/fanclub/0.png" alt="" />

Thank you, that is really helpful and it works perfectly.

Now since oyu werent the only one who suggested conditional templates to me, I was wondering: Does the "proper JS" have any risks/disadvantages?

No, it's just pure JS without any libraries like Prototype/jQuery. That's what he meant with "proper JS" I think.
No... pure ≠ proper. Code in 1st post was simply invalid.
Also the code above does use Prototype (first line, document.observe('dom:loaded').

There are no risks and disadvantage may be speed if someone has a slow PC. PHP is handled serverside so it doesn't influence anyone as much as Javascript can.
Oops you're correct, I misread the documentready part.