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.

Snippet in channel entries search tag

August 31, 2011 2:35pm

Subscribe [3]
  • #1 / Aug 31, 2011 2:35pm

    I have a snippet named no_mag_sum, it return an edition number for a magazine based on the current date. I need to search one custom field with that number :

    {exp:channel:entries channel="chroniques" limit="18" orderby="random" search:mag-chronique="{no_mag_sum}"  status="open|Cover|Vedette"}
    
    {/exp:channel:entries}

    But nothing i have tried work…

    EE Version 2.1.3 Build 20101220

  • #2 / Aug 31, 2011 4:16pm

    this is the snippet code :

    {if {current_time} >= "1322715600"}90{if:elseif {current_time} >= "1319688000"}89{if:elseif {current_time} >= "1316664000"}88{if:elseif {current_time} >= "1314244800"}87{if:elseif {current_time} >= "1311307200"}86{if:elseif {current_time} >= "1308196800"}85{if:elseif {current_time} >= "1305777600"}84{if:elseif {current_time} >= "1303358400"}83{if:elseif {current_time} >= "1300334400"}82{if:elseif {current_time} >= "1297918800"}81{if:elseif {current_time} >= "1294894800"}80{if:elseif {current_time} >= "1291266000"}79{/if}

    and this is the debugging output :

    {exp:channel:entries channel="chroniques" limit="18" orderby="random" search:mag-chronique="{if 1314816720 >= "1322715600"}90{if:elseif 1314816720 >= "1319688000"}89{if:elseif 1314816720 >= "1316664000"}88{if:elseif 1314816720 >= "1314244800"}87{if:elseif 1314816720 >= "1311307200"}86{if:elseif 1314816720 >= "1308196800"}85{if:elseif 1314816720 >= "1305777600"}84{if:elseif 1314816720 >= "1303358400"}83{if:elseif 1314816720 >= "1300334400"}82{if:elseif 1314816720 >= "1297918800"}81{if:elseif 1314816720 >= "1294894800"}80{if:elseif 1314816720 >= "1291266000"}79{/if}" status="open|Cover|Vedette" }

    i was thinking snippet were process first, what can i use instead?

    (sorry for the bad english, im a french-canadian)

  • #3 / Sep 01, 2011 12:53am

    Dan Decker

    7338 posts

    Alexandre,

    Let’s see if we can get an understanding as to why this isn’t working the way you expect. Snippets are the first thing parsed after a template is called. They are then “expanded” into (essentially becoming a part of) the template that calls them. In your example, you are expanding a snippet out to be part of a parameter. The reason this is failing is because *parameters* can’t have conditionals. The same thing would happen if you tried to run your debugging output directly, it wouldn’t work either. To test this, you could use the snippet in a plain template and you would see the conditionals operated. For your reference, here is a nice PDF that explains EE’s parsing order: ExpressionEngine Parsing Order.

    I hope this helps clarify the issue for you and let us know if you have any further questions.

    Cheers,

  • #4 / Sep 01, 2011 10:17am

    So the better way to do what i need is to call a php function im my parameter?

    like this ? :

    {exp:channel:entries channel="chroniques" limit="18" orderby="random" search:mag-chronique="<?php outputNoMag(); ?>"  status="open|Cover|Vedette"}
    
    {/exp:channel:entries}

    Where outputNoMag() is a function who output the edition number of the magazine.

  • #5 / Sep 01, 2011 11:51am

    Dan Decker

    7338 posts

    Alexandre,

    That may work, but I have never written and called a PHP function inside of an EE template before. My suggestion would be to assign a variable using PHP if/else and then echo out that variable in your parameter.

    <?php
    if (my condition) {
    $noMag = ‘my_value’;
    } else {
    $noMag = ‘other_value’;
    }
    ?>
    ...
    
    {exp:channel:entries channel="chroniques" limit="18" orderby="random" search:mag-chronique=“<?= $noMag; ?>"  status="open|Cover|Vedette"}

    Hope this helps.

    Cheers,

  • #6 / Sep 01, 2011 12:24pm

    Tanx for your help Dan,

    I think i have found a good solution :

    template code (parsing php on input):

    {exp:channel:entries channel="chroniques" limit="18" orderby="random" search:mag-chronique="{no_mag_sum}"  status="open|Cover|Vedette"  }
            
    {/exp:channel:entries}

    Snipet {no_mag_sum} :

    <?php
    $arr_magno = array( 
            array( 'no' => 79, 'kiosque' => "2010-12-02"),
                       array( 'no' => 80, 'kiosque' => "2011-01-13"),
                       array( 'no' => 81, 'kiosque' => "2011-02-17"),
                       array( 'no' => 82, 'kiosque' => "2011-03-17"),
                       array( 'no' => 83, 'kiosque' => "2011-04-21"),
                       array( 'no' => 84, 'kiosque' => "2011-05-19"),
                       array( 'no' => 85, 'kiosque' => "2011-06-16"),
                       array( 'no' => 86, 'kiosque' => "2011-07-21"),
                       array( 'no' => 87, 'kiosque' => "2011-08-25"),
                       array( 'no' => 88, 'kiosque' => "2011-09-23"),
                       array( 'no' => 89, 'kiosque' => "2011-10-27"),
                       array( 'no' => 90, 'kiosque' => "2011-12-01")
    );
    $arr_magno = array_reverse($arr_magno);
    foreach ($arr_magno as &$value) {
        if (strtotime($value['kiosque']) <= strtotime("now")){
            echo $value['no'];
            break;
        }
    }
    ?>

    So the snippet is process firt (expand), then the PHP is parsed and finaly the channel module.

    So solution found, you can close this topic if you want

  • #7 / Sep 02, 2011 12:41am

    Dan Decker

    7338 posts

    Alexandre,

    Excellent! And thank you for sharing your solution. Let us know if we can be of assistance in the future.

    Cheers!

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

ExpressionEngine News!

#eecms, #events, #releases