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.

total_results divided by 2?

February 22, 2011 9:28am

Subscribe [4]
  • #1 / Feb 22, 2011 9:28am

    heaversm

    197 posts

    Hi - I need to create a template in which the total results are split into two columns, so that the first column displays half the results, and the second displays the others. Any idea how to do this with just EE tags? I can’t use the css3 columns options as it is not supported by older browsers.

  • #2 / Feb 22, 2011 9:34am

    Mark Bowen

    12637 posts

    Hiya,

    Take a look at Splitter I’m pretty sure that should do what you need here.

    Hope that helps a bit.

    Best wishes,

    Mark

  • #3 / Feb 22, 2011 9:57am

    heaversm

    197 posts

    Wow - thanks Mark. That would be a great solution. I’m having a little trouble trying to get it to work though. My code seems pretty straightforward, but nothing is being split.

    {exp:splitter blocks="2" delimiter="<br/>" class="blogroll"}
        {exp:channel:entries channel="links"}
        <a href="http://{link_url}" target="_blank" rel="noopener">{title}</a>
        {/exp:channel:entries}
    {/exp:splitter}

    I’ve also tried the PHP approach, but EE doesn’t seem to recognize PHP tags within the limit parameter:

    {exp:channel:entries channel="links" dynamic="off" {preview} orderby="link_order" category="2"}
    {if count == total_results}
    <?php 
    $total_results = '{total_results}';
    $half_results = round($total_results/2);
    echo "$half_results";
    ?>
    {/if}
    {/exp:channel:entries}
    
    
    <div class="blogroll">
        {exp:channel:entries limit='<?php echo "$half_results"?>' channel="links" dynamic="off" {preview} orderby="link_order" category="2"}
        <a href="http://{link_url}" target="_blank" rel="noopener">{title}</a>
        {/exp:channel:entries}
    </div>
    
    <div class="blogroll blogroll_two">
        {exp:channel:entries offset='<?php echo "$half_results"?>' channel="links" dynamic="off" {preview} orderby="link_order" category="2"}
        <a href="http://{link_url}" target="_blank" rel="noopener">{title}</a>
        {/exp:channel:entries}
    </div>
  • #4 / Feb 22, 2011 4:50pm

    Ingmar

    29245 posts

    I’ll just move this thread to the CodeShare Corner. Thanks.

  • #5 / Feb 22, 2011 5:30pm

    heaversm

    197 posts

    Nooooo! Codeshare corner is where forum questions go to die.

  • #6 / Feb 22, 2011 5:36pm

    Ingmar

    29245 posts

    I’m very sorry, but neither using the third-party “Splitter” plugin nor raw PHP is really a matter of EE Tech Support…

  • #7 / Feb 22, 2011 5:41pm

    heaversm

    197 posts

    Hah - I know. It was just a joke. I’ll figure it out eventually. Thanks though.

  • #8 / Feb 22, 2011 11:27pm

    heaversm

    197 posts

    I’m sure once splitter gets the kinks worked out, it will be far more elegant than this, but if anyone’s curious, this is what I did:

    Get the count for the halfway point, rounded, with PHP.

    {exp:channel:entries channel="links" dynamic="off" {preview} orderby="link_order" category="2"}
    {if count == total_results}
    <?php 
    $total_results = '{total_results}';
    $half_results = round($total_results/2);
    ?>
    {/if}
    {/exp:channel:entries}

    Then set up an embed for each column, and pass it the halfway point. In the first embed, this is used as the limit, and in the second it’s the offset (you could really do this one template and an if statement, but oh well).

    <div id="blogroll_container">
        <div class="blogroll">
            {embed="embeds/blogroll1" limit='<?php echo "$half_results" ?>'}
        </div>
    
        <div class="blogroll blogroll_two">
            {embed="embeds/blogroll2" offset='<?php echo "$half_results" ?>'}
        </div> 
        <br class="clear_both">
    </div>

    and the embed would look like:

    {exp:channel:entries limit="{embed:limit}" channel="links" dynamic="off" {preview} orderby="link_order" category="2"}
    <a href="http://{link_url}" target="_blank" rel="noopener">{title}</a>
    {/exp:channel:entries}

    and

    {exp:channel:entries offset='{embed:offset}' channel="links" dynamic="off" {preview} orderby="link_order" category="2"}
        <a href="http://{link_url}" target="_blank" rel="noopener">{title}</a>
    {/exp:channel:entries}
  • #9 / Apr 06, 2012 6:56pm

    5BYFIVE Creative

    159 posts

    Will this work without the embeds? Is this a parsing order issue?

  • #10 / Apr 06, 2012 8:00pm

    the3mus1can

    426 posts

    Something like this should give you the same result with only onc entries loop and no embeds (not tested so it might need to be tweaked):

    Make sure PHP is parsed on output.

    <?php
    
    $links = array();
    
    function array_split($array)
    {            
        $end = count($array);
        
        $half = ($end % 2 )?  ceil($end/2): $end/2;
        
        return array(array_slice($array, 0, $half), array_slice($array, $half));
    }
    
    ?>
    
    {exp:channel:entries channel="links" dynamic="off" {preview} orderby="link_order" category="2"}
     <?php $links[] = '<a href="http://{link_url}" target="_blank" rel="noopener">{title}</a>'; ?>
    {/exp:channel:entries}
    
    <?php
    
    $links = array_split($links);
    
    ?>
    
    <div id="blogroll_container">
        <div class="blogroll">
            <?php foreach($links[0] as $link): ?>
         <?php echo $link; ?>
        <?php endforeach; ?>
        </div>
    
        <div class="blogroll blogroll_two">
            <?php foreach($links[1] as $link): ?>
         <?php echo $link; ?>
        <?php endforeach; ?>
        </div> 
        <br class="clear_both">
    </div>
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases