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.

Using Matrix to display certain results among all results

September 11, 2010 1:56am

Subscribe [4]
  • #1 / Sep 11, 2010 1:56am

    Hi,
    I keep coming up with half of the solution and decided to drop in here to see if someone could help me out.

    What I have set up is a blog in which only one field group exists. That field group is a matrix that is used to display audio tracks. I use the title of each entry of this blog to be the header of my list of tracks for that entry. This works fine when I want to show everything without any constraints.

    However, if I add a checkbox to my matrix and want to display only tracks that have been checked off I run into problems. It displays only those tracks just fine, but as you can see from my code below, I’m also returning every single entry {title} in the blog since it sits outside of the matrix.

    {exp:weblog:entries weblog="{my_weblog}" sort="desc" orderby="date" limit="10" }
        <div>
            <h3>{title}</h3>
    <p>         {matrix}<br />
                {if checkbox}<br />
                <a href="http://{url}">{name}{if artist}{artist}{/if}, {time}</a><br />
                {/if}<br />
            {/matrix}<br />
        </div> <br />
    {/exp:weblog:entries}

    I’m aiming for results like this:
    Album 1
    Track 01
    Track 04
    Track 07

    Album 3
    Track 05

    But, instead I’m getting results like this:
    Album 1
    Track 01
    Track 04
    Track 07

    Album 2

    Album 3
    Track 05

    Album 4

    Album 5

    Album 6
    ...

    I can’t move the {title} inside the {matrix} tags, because then it will be repeated above every single track. Is there something I’m not thinking of? Or, is there a better way to do this? I’m all ears.

    And preferably, I’d like to use FF Select instead of FF Checkbox to determine which tracks will be displayed so I don’t have a bunch of checkboxes on every entry form. But I was having trouble as far as the markup is concerned for FF Select. Maybe that’s another topic to create…

  • #2 / Sep 11, 2010 9:43am

    Boyink!

    5011 posts

    Hmm…

    The first thing I’d suggest is looking at the search parameter for the matrix tag - so rather than a conditional inside the loop you can pre-filter the results to just matrix rows with the checkbox checked.

    Then maybe a conditional that looks at the {total_rows} variable and only lists {title} once.

  • #3 / Sep 11, 2010 4:12pm

    smartpill

    456 posts

    If “checkbox” is the name of the checkbox column field in matrix, try something like:

    {matrix search:checkbox="y"}
                            <a href="http://{url}">{name}{if artist}{artist}{/if}, {time}</a>
                     {/matrix}

    See if that helps.

  • #4 / Sep 12, 2010 12:02am

    Hmm…

    The first thing I’d suggest is looking at the search parameter for the matrix tag - so rather than a conditional inside the loop you can pre-filter the results to just matrix rows with the checkbox checked.

    Then maybe a conditional that looks at the {total_rows} variable and only lists {title} once.

    Thanks, Boyink!
    Setting the search parameter allowed me to figure out how to display results based on FF Select as well as Checkbox.

    However I’m still having trouble displaying only the title of the matched criteria since {title} is dependent on the blog and not the matrix. I either get every single title regardless of if my criteria is met, or—if I move {title} inside my {matrix} tags—I get a duplicated title above each track.

    Is there a way I can set a search parameter based on a column in a matrix in the {exp:weblog:entries} tag, and thus only display {title} and {matrix} if that parameter is met? I’m not sure if that would work… just throwing that out there.

  • #5 / Sep 12, 2010 1:00am

    smartpill

    456 posts

    Hmm…
    Is there a way I can set a search parameter based on a column in a matrix in the {exp:weblog:entries} tag, and thus only display {title} and {matrix} if that parameter is met? I’m not sure if that would work… just throwing that out there.

    For the title issue, try:

    {if "{matrix:total_rows search:checkbox="y"}"}<h3>{title}</h3>{/if}
  • #6 / Sep 12, 2010 1:38am

    For the title issue, try:

    {if "{matrix:total_rows search:checkbox="y"}"}<h3>{title}</h3>{/if}

    Thank you, that seems like it is going to work for what I am trying to do.

    I actually just reached a solution of sorts myself, albeit in a different manner. I went about setting the parameter first in the entry tag and then whittled down my results using the search parameter on the matrix tag, like this:

    {exp:weblog:entries weblog="{my_weblog}" search:matrix="Term"}
            <div>
                <h3>{title}</h3>
    <p>            {matrix search:tags="Term"}<br />
                <a href="http://audiopath/{file}">{name}{if artist}{artist}{/if}, {time}</a><br />
                {/matrix}<br />
            </div> <br />
    {/exp:weblog:entries}

    Anyway, thanks for all your help guys. Much appreciated.

  • #7 / Oct 28, 2010 8:01pm

    Chris Arnold

    201 posts

    Pretty similar to this Matrix searching question, is it possible to search for, and display, just ONE result from a Matrix? Not limiting it to the first row, but limiting it to one of many results that may exist.

    I’m working with a Products weblog and am using a Matrix to add product colors, sale price (if present), and inventory for each option.

    But.. I want to indicate in a few spots on this site *if* a sale exists for that product by displaying a ‘sale’ image. The code I have so far just sorts and limits to the first row. So if my sale is on a color that isn’t in the first row of that Matrix, nothing shows up.

    Possible to take what it finds, know there is a sale within the loop, and display that sale image just once? (my sale modifiers are negative numbers, hence the <=0 in place)

    {product_options search:price="<=0" limit="1"}
        {if "{price}" != ""}
        <a href="http://{title_permalink={segment_1}/{segment_2}}">{site_url}/i/on-sale.png</a>
        {/if}
    {/product_options}
  • #8 / Oct 28, 2010 8:19pm

    smartpill

    456 posts

    I think you have to take the check out of the loop or it will repeat. Try something like:

    {if "{product_options:total_rows search:price="not IS_EMPTY"}"}
    <a href="http://{title_permalink={segment_1}/{segment_2}}">{site_url}/i/on-sale.png</a>
    {/if}
     {product_options}
        do ther stuff
    {/product_options}
  • #9 / Oct 29, 2010 12:45pm

    Chris Arnold

    201 posts

    Yep, looks like that does it. Was over thinking it. Thanks for the outside look!

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

ExpressionEngine News!

#eecms, #events, #releases