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.

Getting If/elseif/else statements to work with assigned variables

June 13, 2008 6:28pm

Subscribe [3]
  • #1 / Jun 13, 2008 6:28pm

    I’m having the following problem with code (shown below) where I am trying to run an if/elseif/else statement to load a specific weblog based on what day it currently is. It will currently assign the first variable shown (monday) but will ignore any other content inserted within it.

    {assign_variable:thedate="{current_time format="%D"}"}
       
            {if thedate == "Mon"}
                    {assign_variable:my_weblog="monday"}
            {if:elseif thedate == "Tue"}
                {assign_variable:my_weblog="tuesday"}
            {if:elseif thedate == "Wed"}
            {assign_variable:my_weblog="wednesday"}
            {if:elseif thedate == "Thu"}
            {assign_variable:my_weblog="thursday"}
            {if:elseif thedate == "Fri"}
            {assign_variable:my_weblog="friday"}         
            {if:else}
            {assign_variable:my_weblog="friday"}              
            {/if}
  • #2 / Jun 13, 2008 6:34pm

    Ingmar

    29245 posts

    You cannot use asigned variables dynamically like that. They are only assigned once, and cannot be later changed at run time. In PHP terms, they behave a lot like constants. Makes sense?

  • #3 / Jun 13, 2008 6:47pm

    Ok that makes sense, now is there a dynamic variable or different approach I would want to take to accomplish the intent of the code?

  • #4 / Jun 13, 2008 6:51pm

    Ingmar

    29245 posts

    Skip the assignment of an intermediary variable, like so:

    {assign_variable:my_weblog="{current_time format="%l"}"}

    You get the idea. Or use PHP 😊

  • #5 / Jun 16, 2008 5:49pm

    would it be possible to assign it a value inside php?

    ie:

    <?php
    $date="monday";
    {assign_variable:my_weblog=$date};
    ?>

    I’m aware this specific code will not work, but is there a method to accomplish that that would?

    Thanks again.

  • #6 / Jun 16, 2008 5:55pm

    Ingmar

    29245 posts

    No, that wouldn’t work. What are you trying to accomplish, ultimately?

  • #7 / Jun 16, 2008 6:02pm

    Running conditions that would establish what the global variable would be, and then set it as a constant.

  • #8 / Jun 17, 2008 3:16pm

    just to verify, my hope is that there is some way to have

    it check if it is mon-fri, and assign that blog as the global variable.
    if it is sat-sun. It will load friday.

  • #9 / Jun 17, 2008 7:54pm

    gridonic

    231 posts

    Why not just use PHP in the input stage for this?

  • #10 / Jun 18, 2008 1:59pm

    Would it be possible to get an example of that? I have been trying to do just that, but despite it running and giving the right code, it will not execute. I’ve tried a few variations, but this is the latest just to give you an example of my own:

    <?php
             $date=date("D");
                
            if ($date == "Mon"){
                 $globalvarblog="{assign_variable:my_weblog="monday"}";
                  }
            else if ($date == "Tue"){
                         $globalvarblog="{assign_variable:my_weblog="tuesday"}";
                  }
            else if ($date == "Wed"){
                 $globalvarblog="{assign_variable:my_weblog="wednesday"}";
                }
            else if ($date == "Thu"){ 
                 $globalvarblog="{assign_variable:my_weblog="thursday"}";
                }
            else if ($date == "Fri"){
                 $globalvarblog="{assign_variable:my_weblog="friday"}";
                }
            else{
                 $globalvarblog="{assign_variable:my_weblog="friday"}";        
                }
    
            echo "$globalvarblog";
            ?>
  • #11 / Jun 18, 2008 3:36pm

    gridonic

    231 posts

    Hm, how about

    <?php
    $date = date("D");
    
    if($date == 'Mon')
    {
        $weblog_name = 'monday';
    }
    else
    {
        $weblog_name = 'other-day';
    }
    ?>
    
    {exp:weblog:entries weblog="<?php echo($weblog_name);?>"}
    ...
    {/exp}

    PHP set to input. Shouldn’t this work?

  • #12 / Jun 18, 2008 5:39pm

    Thank you, that did the trick, i was missing setting php parsing to input in preferences.

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

ExpressionEngine News!

#eecms, #events, #releases