vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=111)
-   -   random or flipping of section execution within template (https://vborg.vbsupport.ru/showthread.php?t=117447)

libertate 06-02-2006 05:16 PM

random or flipping of section execution within template
 
Currently I have two sections within a template which gets executed depending on the pageid.
Code:

<if condition="($pages['pageid'] ==...
... code A ...
<else />
... code B ...
</if>

How can I make code A and code B alternate, or randomly displayed, using only template script?

Thanks!

libertate 06-04-2006 07:43 PM

bump, any ideas?

Adrian Schneider 06-04-2006 08:35 PM

What are you trying to accomplish exactly? Will your template condition be executed more than once (in a loop), or just once? What would Code A and Code B contain?

If you want it random, have it generate a random number (say between 0 and 1), then in your template just check it.
PHP Code:

$num rand(01); 

Code:

<if condition="$num">Code A<else />Code B</if>
With a little more information I'll show you how to do what you want...

libertate 06-05-2006 01:11 AM

I want the code to be kept within the template and not use external code.

I simply one to display two javascripts. It would not loop. Depending on a random state, it should display one or the other. It doesn't even have to be random, it could be ABABABABA i.e. flipping

Is there a template variable that changes at each load time? Time or or something else? Maybe then I can do a simple var div 2 != var / 2 to decide which section to execute.

Adrian Schneider 06-05-2006 01:25 AM

If it is a one time per script thing, then I would do this:
Code:

<if condition="TIMENOW % 2">Code A<else />Code B</if>
Otherwise I would do this:
Code:

<if condition="exec_switch_bg() == 'alt1'">Code A<else />Code B</if>
Every time my second example executes, it will alternate.

libertate 06-05-2006 04:35 PM

Thank you!

Is there a round(), trunc(), ceiling(), floor() or similar function which I can use like this:

Code:

<if round("TIMENOW % 2")="TIMENOW % 2">Code A<else />Code B</if>
Or DIV (integer division)?
Code:

<if "TIMENOW DIV 2"="TIMENOW % 2">Code A<else />Code B</if>

Hellcat 06-05-2006 05:01 PM

Try this:

Code:

<if condition="$myalternate == 1">
  ... code A ...
  <if condition="$myalternate = 0"></if>
<else />
  ... code B ...
  <if condition="$myalternate = 1"></if>
</if>

Note that there are only SINGLE equal signs ("... = ...") in the secondary IFs and the usual double ones ("... == ...") in the first one.


[EDIT]
This may not work on the first try, depending on how the single bits are assembled (all in one function call, or each in seperate function call), so may want to find a variable with global scope you can use/hijack.

For posts inside a thread, for example, you could use the $thread array ($thread[myalternate]) to keep your value between posts....

Adrian Schneider 06-06-2006 02:16 AM

Why not just use plugins to accomplish most of the processing, so you can keep the templates nice and simple?

@Hellcat: You really shouldn't use PHP like that in templates. Though it works, it defeats the whole purpose of the template system. Someone editing it will most likely remove the empty conditions and break the script without knowing what happened to it.

@libertate: your syntax is a little off:
Code:

<if condition="YOUR_CONDITION">
    show this if YOUR_CONDITION is true
<else />
    Show this if YOUR_CONDITION is false.
</if>

Note that the <else /> is optional.

Anyway, both of our examples do the same thing (alternate between true/false of the condition), so what exactly are you trying to accomplish here?

libertate 06-07-2006 12:17 AM

Hah! It worked. Thank you all!

Code:

<if condition="TIMENOW % 2">Code A<else />Code B</if>
The above works.

Coming from old school programming, I didn't realize that the "condition" was not a pseudonym, but the actual command, and the comparison was inside the quotes. :-/

So what does TIMENOW generate, and what does % mean? Is it MOD or something? Why is there no reference for this stuff (or so complicated to find / I looked on vB.com and couldn't find it)?

If you wanted to know...Code A is Google Adsense, Code B is Yahoo Publisher Network.

And it is showing positive impact to flip the advertisers periodically. :-D

Adrian Schneider 06-07-2006 12:23 AM

TIMENOW is a Unix timestamp. % is mod, yes. The condition can basically be any PHP code, with the exception of only being able to run a few functions.

For more conditions you can use, you can just learn more PHP, but I think there are some basics in the vBulletin manual.


All times are GMT. The time now is 08:02 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01123 seconds
  • Memory Usage 1,735KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (9)bbcode_code_printable
  • (1)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete