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.

next-previous based on custom field

June 13, 2009 2:33pm

Subscribe [2]
  • #1 / Jun 13, 2009 2:33pm

    Jan_Huygens

    106 posts

    Next-Prev is based upon date
    I was wondering if there was a simple way to do something similar but based upon the contents of a custom field?

    bit of background - I’m preparing informative entries according to a numbered scheme, but am completing them in a moderately random order.  In each entry I have a custom field to which I’ve enter a sequence number.

    So initially I will have a few entries whose sequence numbers are dotted round all over the place, e.g.
    entry 1   #0020
    entry 2   #0055
    entry 3   #0010
    entry 4   #0001
    entry 5   #0100

    now if I’m looking at entry3[#0010], then I don’t want my prev/next to be entry2/entry4
    BUT entry4/entry1 because their sequence numbers are #0001 & #0020

    So, for the ‘next’ [as an example] I’m sort to thinking about using the weblog tag, ordering it by the sequence number field, and seeing if the sequence number of each entry is more than that of the one I’m looking at.

    However, where I get stuck is that I can’t see how just to find the first entry that passes the test, i.e. I want just one ‘next, and not 10 or 20 nexts!

    have I missed some clever EE way of solving this? 

    thanks in advance

  • #2 / Jul 15, 2009 8:52am

    Jan_Huygens

    106 posts

    still not cracked this one :-(

    I had wondered about using the ‘search:field_name=’ parameter in the weblog, but sadly it only offers ‘equals’ or ‘not equals’, and does not seem to offer ‘greater than’ or ‘less than’...

    any thoughts?

  • #3 / Jul 23, 2009 1:17pm

    Jan_Huygens

    106 posts

    I keep on coming back to this and am now trying my hand at using php [first time], and I could really do with some help. 

    Here is my first hack at a ‘next’ entry link - just trying to get a custom counter working as the first step.

    <?php $mycount=0; ?>
    {exp:weblog:entries weblog="w_entries" orderby="f_seq_nos" sort="desc" dynamic="off" }
     {if "{f_seq_nos}" < "{curr_seq_nos}"}
       <?php $mycount = $mycount + 1; print $mycount."  "."{count}"; ?>
     {/if}
    {/exp:weblog:entries}

    This is in an embed which is called from a single entry page.
    The entry sequence numbers are stored in the field [f_clip_seq_nos]
    The sequence number for the current entry is passed to the variable [curr_seq_nos]

    I’ve sorted the entries by the sequence number, and then use a conditional to reject those whose sequence number is greater than that of the current entry - so far so good - thats pure EE.

    I just need to get the first entry that passes the conditional

    So what I’m trying to with php do is create an new counter, $mycount, that only increments inside the conditional.  I think it should then be a simple matter to print out a link to that entry - so far I have not written that - for the simple reason that my counter seems to completely ignore the conditional and always has the same value as the EE {Count} :-(

    so, if I have 23 entries, and the current entry is 20, then I actually see:
    21 21
    22 22
    23 23

    when I should be seeing:
    1 21
    2 22
    3 23

    PHP is enabled on output.

    any thoughts on what I’m doing wrong?

  • #4 / Jul 23, 2009 2:52pm

    ender

    1644 posts

    ok I managed to get something to work along these lines by embedding a template that runs a query and passing a few variables over to it.

    first the template code (we’ll say it’s the includes/prev_next_links template:

    {exp:query sql="SELECT
                        t.entry_id,
                        t.title,
                        t.url_title,
                        d.field_id_{embed:field_id} AS number
                    FROM
                        exp_weblog_titles AS t NATURAL JOIN
                        exp_weblog_data AS d
                    WHERE
                        d.field_id_{embed:field_id} = (
                            SELECT field_id_{embed:field_id}
                            FROM exp_weblog_data 
                            WHERE field_id_{embed:field_id} > {embed:current} AND field_id_{embed:field_id} <> ''
                            ORDER BY field_id_{embed:field_id}
                            LIMIT 1
                        ) OR
                        d.field_id_{embed:field_id} = (
                            SELECT field_id_{embed:field_id}
                            FROM exp_weblog_data 
                            WHERE field_id_{embed:field_id} < {embed:current} AND field_id_{embed:field_id} <> '' 
                            ORDER BY field_id_{embed:field_id} 
                            DESC LIMIT 1
                        )
                    ORDER BY d.field_id_{embed:field_id} ASC"}
        {if number < embed:current}
            <a href="{embed:path}/{url_title}">< prev</a>
        {if:else}
            <a href="http://{embed:path}/{url_title}">next ></a>
        {/if}
    {/exp:query}

    now how you’d call it:

    {exp:weblog:entries weblog="your_weblog"}
    {embed="includes/prev_next_links" current="{field_name}" field_id="107" path="/template_group/template"}
    {/exp:weblog:entries}

    where {field_name} is the name of the field holding the numbers you’re ‘sorting’ by, field_id=“107” is the number on that field in the database, and path=“template_group/template” is the url to the template that you want to display the entries with.

    brief testing shows this working like a champ on my test system, let me know how it goes for you

  • #5 / Jul 23, 2009 3:09pm

    Jan_Huygens

    106 posts

    Hi Ender

    thanks for that 😊

    I’ve got to go out now, but as soon as I get back I’ll give it a go

  • #6 / Jul 24, 2009 6:25am

    Jan_Huygens

    106 posts

    Does what it says on the tin 😊
    Thank you very much - that is deeply appreciated!

    BTW, for anyone interested, the field_id can be found in the MySQL database, in the exp_weblog_data table, in the column labelled ‘field_id’ [suprisingly enough!]

    I mention this because, you might be tempted to use the field label that is seen when you setup or edit a field in the CP
        [CP Home ›  Admin ›  Weblog Administration ›  Field Groups ›  Custom Fields ›  Field Order]
    By default they are the same [i think], but if you alter the field label manually, then field_id is unchanged.

    One slight tweak to the embed code, you can replace this:

    WHERE
                        d.field_id_107 = (
    :
    :
                        ) OR
                        d.field_id_107 = (

    with

    WHERE
                        d.field_id_{embed:field_id}  = (
    :
    :
                        ) OR
                        d.field_id_{embed:field_id}  = (

    at least that works for me, and means you dont have to re-edit the embed if you use it else where.

    Once again - thanks 😊

  • #7 / Jul 24, 2009 8:32am

    Jan_Huygens

    106 posts

    Just out of interest
      - can anyone explain what was wrong with my EE/PHP approach?

  • #8 / Jul 24, 2009 9:01am

    ender

    1644 posts

    One slight tweak to the embed code, you can replace this:

    WHERE
                        d.field_id_107 = (
    :
    :
                        ) OR
                        d.field_id_107 = (

    with

    WHERE
                        d.field_id_{embed:field_id}  = (
    :
    :
                        ) OR
                        d.field_id_{embed:field_id}  = (

    at least that works for me, and means you dont have to re-edit the embed if you use it else where.

    oops, I thought I caught all of those… was trying to make it as generic as possible and I guess I missed a couple :p

    glad it worked for you though, and yeah you’d think the field id would be the number that EE shows you, but it isn’t.  glad you found the real number to use.

  • #9 / Feb 25, 2010 6:13pm

    RezwanR

    126 posts

    This looks promising.

    I’m not sure how to apply it inside the gallery.  As you see from this page - that’s what I want with the previous next links - a thumbnail to show up on either side.  http://focusfusion.org/index.php/gallery/image_med/43/

    This is a gallery page, so the weblog:entries tag seems out of place.  Also, in lieu of field_id=“107”, what do I put?

    {exp:weblog:entries weblog="your_weblog"}
    {embed="includes/prev_next_links" current="{field_name}" field_id="107" path="/template_group/template"}
    {/exp:weblog:entries}
  • #10 / Feb 26, 2010 9:53am

    ender

    1644 posts

    my query’s general approach still more or less applies, but you’d need to change it to use the exp_galleries/exp_gallery_entries tables and fields instead of exp_weblog_xxx tables/fields.

    the 107 got turned into field_id_107 in my query which allowed the developer to select which custom field to look at for next/previous values.  in a gallery you’d probably just use the entry_id of the image unless you wanted to use custom_field_one|two|...|six for whatever reason.

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

ExpressionEngine News!

#eecms, #events, #releases