ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to output the same random value using PHP across 2 separate templates?

July 11, 2007 10:02am

Subscribe [4]
  • #1 / Jul 11, 2007 10:02am

    Hamish Brown

    12 posts

    Hi

    We have four wallpaper-style images that we need to theme a site with. Each image is carved into three (eg Wallpaper1A.jpg, Wallpaper1B.jpg, Wallpaper1C.jpg, Wallpaper2A.jpg, Wallpaper2B.jpg, Wallpaper2C.jpg) to display within three templates (Header, LeftSidebar, RightSidebar) which are includes on each page.

    Is there a way in EE to use PHP to output a random value between 1 and 4 and have this value remain the same across all 3 templates? I’ve tried a few solutions including assigning the value of a Variable B to Variable A, with Variable A value set to random, but can’t get this to work across separate templates.

    I’m guessing that path.php might be where the solution goes but that’s as far is i’ve got.

    Thanks for any help

  • #2 / Jul 11, 2007 10:25am

    Ingmar

    29245 posts

    Simply generate the random value, and pass it on to the sub-template.

    <?php srand((double)microtime()*1000000); $theme = rand(1,4); ?>
    
    {embed="/includes/header" my_theme_id="<?php echo $theme;?>"}
    {embed="/includes/left_sidebar" my_theme_id="<?php echo $theme;?>"}
    {embed="/includes/right_sidebar" my_theme_id="<?php echo $theme;?>"}

    In your sub_templates you can then use something like:

    Wallpaper{embed:my_theme_id}A.jpg

    Makes sense? Untested, but should get you started.

  • #3 / Jul 11, 2007 10:28am

    silenz

    1651 posts

    to display within three templates (Header, LeftSidebar, RightSidebar) which are includes on each page.

    If that means they are embedded, perhaps try the following:
    Enable PHP for the template and set it to Input Parsing stage.

    <?php $somenumber = rand(1,4); ?>
    
    {embed="site/embed_one" random="<?php echo $somenumber ; ?>"}
    
    {embed="site/embed_two" random="<?php echo $somenumber ; ?>"}
    
    {embed="site/embed_three" random="<?php echo $somenumber ; ?>"}

    Inside the embedded templates you can then refer to the value with {embed:random}.

  • #4 / Jul 11, 2007 10:29am

    silenz

    1651 posts

    LOL. Agreed.

  • #5 / Jul 11, 2007 10:30am

    Ingmar

    29245 posts

    Great minds think alike, right? 😊

  • #6 / Jul 11, 2007 3:30pm

    Hamish Brown

    12 posts

    That worked perfectly.

    Thank you people.

    -Hamish

  • #7 / Feb 13, 2008 6:00pm

    yolise

    59 posts

    I’m trying desperately to get my head around this. Which bit goes in the containing template and which bit goes in the embedded template?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases