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 data only Once within the loop

March 28, 2011 7:31am

Subscribe [3]
  • #1 / Mar 28, 2011 7:31am

    JusTyMe

    76 posts

    Obiously EE is making a magic looping with the use of tags but sometimes we still want to get the previous data.

    To make it clear here is an example code which is not working where I want to display the group_title ONLY once. It will display ONLY on its first occurence, Is this possible to achieve with EE?

    {exp:query sql="
    
                SELECT exp_members.member_id, exp_member_groups.group_title, exp_members.username, exp_members.screen_name, exp_members.email
                FROM exp_members
                INNER JOIN exp_member_groups
                ON exp_members.group_id = exp_member_groups.group_id
                ORDER BY exp_members.group_id
    
            "}
            
           {if previous_group_title != group_title} //previous group title is within the array() it is like $i-1
                <tr><td colspan="3">{preload_group} - {group_title}</td></tr>
            {/if}
            
            <tr>
                
                <td>{member_id}</td>
                <td>{screen_name}</td>
                <td>{email}</td>
                
            </tr>
            
            {/exp:query}

    {if previous_group_title != group_title} //previous group title is within the array() it is like $i-1
            <tr><td colspan=“3”>{preload_group} - {group_title}</td></tr>
          {/if}


    best regards,
    Jay

    Moved to CodeShare Corner by Moderator

  • #2 / Mar 28, 2011 3:32pm

    Sue Crocker

    26054 posts

    Hi, Jay, have you tried adding limit 1 to your sql query? Give that a try and see if it does what you’re looking for.

  • #3 / Mar 28, 2011 3:59pm

    JusTyMe

    76 posts

    Sorry but that won’t solve the problem. I am sorry to but it is really hard to explain.

    My case is I sorted the query by group. Let us assume I have 3 groups. In every first occurrence of a group, I want to display the group name but for the succeeding occurrence won’t display it anymore. It will again display when a new group occurs.

    For example I have these data. Let as assume this are the result from the query

    grp_1,  Jay
    grp_1, Roy
    grp_1, Earl
    grp_5, Simon
    grp_5, Ted
    grp_9, Jim
    grp_9, Joy
    grp_9, Max
    grp_9, Mark

    I want to get a result like this .

    grp_1
      Jay
      Roy
      Earl
    grp_5
      Simon
      Ted
    grp_9
      Jim
      Joy
      Max
      Mark

  • #4 / Mar 29, 2011 10:31am

    Sue Crocker

    26054 posts

    You’re going to need a bit of CodeShare Corner  assist with this one. Should be doable with some SQL, but let’s have the community help out.

  • #5 / Mar 29, 2011 10:48am

    Mark Bowen

    12637 posts

    Untested but give this a go :

    <?php
        $previous_group_title = '';
    ?>
    <table>
    {exp:query sql="
                SELECT exp_members.member_id, exp_member_groups.group_title, exp_members.username, exp_members.screen_name, exp_members.email
                FROM exp_members
                INNER JOIN exp_member_groups
                ON exp_members.group_id = exp_member_groups.group_id
                ORDER BY exp_members.group_id
            "}
    
    <?php
            // If the current group title is not equal
            // then the title needs to be printed out 
            if ("{group_title}" != $previous_group_title)
            { 
                $previous_group_title = '{group_title}'; 
    ?>
                <tr><td colspan="3">{group_title}</td></tr>
    <?php
            }
    ?>
            
            <tr>
                <td>{member_id}</td>
                <td>{screen_name}</td>
                <td>{email}</td>
            </tr>        
            {/exp:query}
    </table>

    You’ll need PHP turned on for the template of course.

    Best wishes,

    Mark

  • #6 / Mar 31, 2011 7:55am

    JusTyMe

    76 posts

    Thanks Prof. Mark. You are always there scratching our backs.

    Best regards,
    Jay

  • #7 / Mar 31, 2011 8:26am

    Mark Bowen

    12637 posts

    Thanks Prof. Mark. You are always there scratching our backs.

    Best regards,
    Jay

    No problem at all. Glad it helped a bit.

    Best wishes,

    Mark

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

ExpressionEngine News!

#eecms, #events, #releases