forum_display_thread, randomly display something every n threads
#1
Hi,

In the list of threads on the forum display page. I want to have some adverts displayed instead of the thread randomly once within the first 5 thread positions.

So it would look like this

- Thread 1
- Thread 2
- Advert
- Thread 3
- Thread 4
- Thread 5

In the past I directly edited the template but want this to survive upgrades and am not sold on using PHP in the templates because of the extra overheads that will cause.

Can someone give me some guidance on this?

Cheers
Dan
I am guessing my plugin will need to run with this hook
809 forumdisplay_thread
Hmmm so this is proving far more difficult that it should.

Is there a global counter for the thread display. I can get it to do what I want, but it does it for every thread or runs the function for every thread.

I was hoping forumdisplay_thread would run once and I could perform the actions on that, but it appears it runs for all the threads so will run the function each time. Basically meaning that the action is preformed infinitely. I will need to see if there is a way to do it outside of it.

Any suggestions?
Reply
#2
Just set a global variable recording the count. For example you could do something like this in the hook.

global $mybb, $db, $count;

$count++;

if($count >= 3) {
$count = 0;
//do what you want here
}
Reply
#3
hmm that is what I had

But I can't get it to work.

It seems that altering the template at that stage doesn't seem to work and causes a few variant issues.

I don't want to have to end up installing PHP directly in templates so this is killing me.
Just cant think of another way to besides editing the template on the fly to get this to insert.
Reply
#4
Something like this works for me:

global $mybb, $db, $count, $threads;

++$count;

if($count >= 3) {
$count = 0;
$threads .= '<tr><td colspan="8">Whatever you want here.</td></tr>';
}
Reply
#5
oh wow. that simple.... let me try it out
Didn't ealise I could just add to the thread variable. That has fixed it up..Cheers
Reply
#6
(2011-04-14, 03:22 AM)Jammerx2 Wrote: Something like this works for me:

global $mybb, $db, $count, $threads;

++$count;

if($count >= 3) {
$count = 0;
$threads .= '<tr><td colspan="8">Whatever you want here.</td></tr>';
}

Works great, thanks Jammerx2. Smile

#
MyBB is the best forum software! Exclamation

Reply
#7
(2011-04-14, 03:22 AM)Jammerx2 Wrote: Something like this works for me:

global $mybb, $db, $count, $threads;

++$count;

if($count >= 3) {
$count = 0;
$threads .= '<tr><td colspan="8">Whatever you want here.</td></tr>';
}

One further stumbling block i am coming up against is getting a further global variable to be recognized.

With the count variable I am also declaring

$ran_num_ad = rand(1,6);


I then put this in the global as follows

function random_thread_advert(){
	global $mybb, $db, $ad_count, $threads, $ad_ran_num;

	++$ad_count;
	echo "NUM: ".$ad_ran_num; //For Debug
	if($ad_count == $ad_ran_num) {
		$threads .= '<tr><td colspan="7">Whatever you want here.</td></tr>';
	} 
}

However it looks like $ad_ran_num;, is still coming out as blank
echoing it out before the function it comes out with a value.

Am I missing something?

Dan

Ok the only dirty and somewhat scary way to get it to read was to declare it as $globals['var'] = rand(1,9);
Reply
#8
The random number isn't even generated in that function.
Reply
#9
Yes that's correct. If I called it inside that function it would generate for every thread.

I want a random number, say 6 to be called once and then fed into that function so for every call it will be 6 then after the 6th post it displays the ad.

Reply
#10
Do something like this:

if(!$ran_num_ad) {
$ran_num_ad = rand(1,6);
}

At the start of the function, that way it will only generate it once.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)