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.

Display entries in one channel based on category id of another channel

October 07, 2010 3:54am

Subscribe [3]
  • #1 / Oct 07, 2010 3:54am

    fourlincoln10

    18 posts

    I’m working on a site that sells fish. They have individual pages for each type of fish they sell. They also have about 15 or so fish recipes. The fish are in one channel. The recipes are in another channel. I assigned the same ‘fish’ category group to both channels, and each fish/recipe has a fish category assigned to it. For example, the ‘blackcod’ page has the ‘blackcod’ category assigned to it and all of the recipes that have to do with ‘blackcod’ have the ‘blackcod’ category id assigned to them too.

    What I want to do is display teasers for three recipes in the sidebar on the fish page. I want all three to be randomly selected from recipes that have the same category id as the fish page, but if there aren’t three then I want enough recipes with other category ids to be randomly selected until there are a total of three. I then want to output the recipes that match the category id first followed by the other recipes. I hope that makes sense. Here’s a link to a fish page:

    http://posustainableseafood.com/index.php/lingcod

    Right now the recipes in the sidebar are just randomly selected. Can anyone give me some advice on how to proceed? I enabled PHP on output and I’m playing with that, but it seems a little complicated.

    Thanks,

    Troy

  • #2 / Oct 07, 2010 10:55am

    Sue Crocker

    26054 posts

    For ultimate control, I’d recommend using Playa or perhaps Selectatron. Would either of these third party add-ons work for you?

  • #3 / Oct 07, 2010 3:18pm

    fourlincoln10

    18 posts

    I was able to do it with PHP. In case anyone has a similar problem, here’s what I ended up with. If there’s a better way to do this, please let me know. This is a hack.

    <?php
    $entries = array();
    
    /*******************************************************************************
        Store the recipes in a 2-dimensional array with category_id as the first key
    ********************************************************************************/
    {exp:channel:entries channel="recipes" dynamic="no" disable="member_data|pagination|category_fields"}
        $entry =<<<EOE
            <h2>{title}</h2>
    <p>        /images/site/our_fish/plate_icon.jpg{description}<br />
    <a href="/index.php/{url_title}">» Continue Reading</a><br />
    EOE;<br />
        {categories}<br />
            $entries[{category_id}][] = $entry;<br />
        {/categories}<br />
    {/exp:channel:entries}</p>
    
    <p>/*******************************************************************************<br />
        Get the category_id of for the current page<br />
    ********************************************************************************/  <br />
    $cid = "{exp:channel:entries channel="pages"}{categories}{category_id}{/categories}{/exp:channel:entries}";</p>
    
    <p>/*******************************************************************************<br />
        Print up to 3 recipes in the current category<br />
    ********************************************************************************/<br />
    $cnt = 0;    <br />
    if(isset($entries[$cid])) {<br />
        shuffle($entries[$cid]);<br />
        foreach($entries[$cid] as $val) {<br />
            echo $val;<br />
            if(++$cnt >= 3)break;<br />
        }<br />
    }</p>
    
    <p>/*******************************************************************************<br />
        If we haven't printed 3 recipes yet, select 3 at random and print them.<br />
    ********************************************************************************/<br />
    $prev_keys = array(); // Store previous keys to avoid duplicates<br />
    while($cnt < 3) {<br />
            $rand_key = mt_rand(0, (count($entries) - 1));<br />
            while($rand_key == $cid || !array_key_exists($rand_key, $entries)) $rand_key = mt_rand(0, (count($entries) - 1));<br />
            $rand_entry = array_rand($entries[$rand_key]);<br />
            $pk = $rand_key + $rand_entry;<br />
            if(in_array($pk, $prev_keys)) continue; // Don't print duplicates<br />
            $prev_keys[] = $pk;<br />
            echo $entries[$rand_key][$rand_entry];<br />
            ++$cnt;<br />
    }<br />
    ?>

    Best,

    Troy

  • #4 / Oct 08, 2010 3:14am

    John Henry Donovan

    12339 posts

    Troy,

    Thanks for sharing your solution.and I’m glad you are up and running again. Feel free to start a new thread if you have any more questions.

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

ExpressionEngine News!

#eecms, #events, #releases