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.

php variables inside template get auto-reassigned within {exp:channel:entries} loop?!

July 09, 2012 1:05am

Subscribe [3]
  • #1 / Jul 09, 2012 1:05am

    chickwithbob

    1 posts

    Hi all,

    I am trying to assign some varaibles via php inside a template, yet they seem to get reassigned when I echo them:

    {exp:channel:entries channel="staticpage" entry_id="{embed:current_id}" dynamic="off"}
                
    <?php 
    $page_title = "{title}"; 
    ?>
    
    <div class="panel">
        <div class="titlebig"><?php echo $page_title; ?></div>
    </div>
                  
    {/exp:channel:entries}
                    
    {exp:channel:entries entry_id="{segment_3}" dynamic="off"}
    
    <div class="panel">
        <div class="titlebig"><?php echo $page_title; ?></div>
    </div>
    
    {/exp:channel:entries}

    The first time I echo $page_title it displays the correct title from channel “staticpage”, but the 2nd time $page_title suddenly becomes the title for entry_id {segment_3}

    ie. $page_title has become reassigned in the next {exp:channel:entries} loop even though the php variable was never reassigned?!

    Can anyone offer a clue why this is happening?

     

  • #2 / Jul 10, 2012 4:05pm

    Kevin Smith

    4784 posts

    Hi chickwithbob,

    I’m not quite sure what’s going on here since I’m able to get this to work on my own installation of EE here. Could you provide a bit more context? Are you enabling PHP on your template, set to parse on Output? Is there anything else in your template? Perhaps if you could post the entire template to http://pastie.org and post the link here, that would help give better context.

    Also, could you go into some detail about why and how you’re using {embed:current_id}? Does that just contain an entry ID? Could you also post that template here?

  • #3 / Jul 10, 2012 9:18pm

    MadWebDesigns

    147 posts

    Hi chickwithbob,

    Try declaring your php $page_title variable before you use it in the channel entries tag like so:

    <?php 
    $page_title = ""; //Declare the variable first
    ?>
    
    {exp:channel:entries channel="staticpage" entry_id="{embed:current_id}" dynamic="off"}
                
    <?php 
    $page_title = "{title}"; 
    ?>
    
    <div class="panel">
        <div class="titlebig"><?php echo $page_title; ?></div>
    </div>
                  
    {/exp:channel:entries}
                    
    {exp:channel:entries entry_id="{segment_3}" dynamic="off"}
    
    <div class="panel">
        <div class="titlebig"><?php echo $page_title; ?></div>
    </div>
    
    {/exp:channel:entries}

    See if that makes a difference.

    Mike

  • #4 / Jul 13, 2012 10:26am

    Shane Eckert

    7174 posts

    Hey chickwithbob,

    Looks like you have some great help here!

    Can you let us know how you are doing?

    Cheers!

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

ExpressionEngine News!

#eecms, #events, #releases