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.

orderby second name?

March 10, 2008 4:56pm

Subscribe [7]
  • #1 / Mar 10, 2008 4:56pm

    Simon Clayson

    38 posts

    I’m sure I either saw how to do this or dream it as I’m having deja vu. I want to be able to say orderby=“secondname”

    I have a list of names which are the titles for the entries, and then displayed via a standard entries tag. I want to list these names like this:

    - Robyn Banks
    - Tom Bowler
    - Horace Cope
    - Eileen Dover

    Not this:

    - Eileen Dover
    - Horace Cope
    - Robyn Banks
    - Tom Bowler

    The obvious way is to create custom fields for first and second names but would like to avoid this for simplicity. Am I missing a trick or a plugin?

  • #2 / Mar 10, 2008 5:08pm

    Mark Bowen

    12637 posts

    Hi Simon,

    Unless I have missed something also then I think you would need to have two fields, one for the first name and one for the last and then orderby the last name field.

    I don’t know if there would be some clever way to get the last name using PHP or something where you would look for the break between the two (first and last name) and then use the Dynamic Parameters using the last name you now have but I have never tried that so not sure if it would work or not.

    Myself personally I always go with having two fields, one for first name and one for last just in case anything like this ever comes up. Also you have more control over how to show peoples names on sites when you need to.

    I think I found the post that you may have been referring to here but I think there may have been one other at some point but I can’t find that for the life of me unfortunately. If there is another post then maybe someone else will remember it and put our minds at ease 😉

    Hope some of this helps.

    Best wishes,

    Mark

  • #3 / Mar 10, 2008 5:14pm

    Low

    407 posts

    Go with 2 fields, or (in short): PHP on output, collect the titles in an array (you could explode() the title, reverse it, and implode again to create the array key, which would result in ‘Banks Robyn’ => ‘Robyn Banks’), ksort() the array, and parse it.

    Does that help? 😊

  • #4 / Mar 10, 2008 5:20pm

    Mark Bowen

    12637 posts

    That would be a way but I still think the two fields are better as you have a lot more control over things and you also don’t have to use PHP on the template then.

    A good way of doing it never-the-less.

    Thanks for the heads up Lodewijk! 😉

    Best wishes,

    Mark

  • #5 / Mar 10, 2008 5:22pm

    Simon Clayson

    38 posts

    Thanks for all the help. I think I’m going to sleep on it. From a client perspective, two custom fields is an annoyance:

    Title? Robyn Banks. Easy
    First Name: Robyn
    Second: Banks

    “I’ve already typed this once! FFS! What is this, Vista!?”

    The PHP option isn’t going to be produced by me in a few minutes so I need to mull it over!

  • #6 / Mar 10, 2008 5:26pm

    Mark Bowen

    12637 posts

    From a client perspective, two custom fields is an annoyance.

    Agggh clients!! 😊

    I sometimes think that some of the clients I work with say not to something just to see how good you are at figuring out another method 😉

    They are still typing the same amount of data so I never understand it when they say that kind of thing. Oh well, each to their own I suppose.

    Hope you get it all sorted the way you need.

    Let us know how you get on.

    Best wishes,

    Mark

  • #7 / Mar 10, 2008 5:34pm

    Simon Clayson

    38 posts

    Thanks Mark, (and all you Twitter peoples).

    They would be typing the name twice because what else to call the title? I’m going to see first if we need to be alpha ordered (we probably will), and go from there. Something usually comes along when I’m walking down the road or something, but I was hoping for the plugin route!

  • #8 / Mar 10, 2008 5:38pm

    Jared Farrish

    575 posts

    Well, it’s seems like you could use the AS keyword to build a subquery that populates a col_last from the col_name, but only returns the SUBSTRING_INDEX to the first space character from the right:

    SELECT member_id AS id, col_name, col_title, col_other, col_last AS (
        SELECT SUBSTRING_INDEX(col_name, ' ', -1)
        FROM members
        WHERE member_id=id
        LIMIT 0, 1
    )
    FROM members
    WHERE col_title='exp'
    ORDER BY col_last

    That’s essentially pseudo-code, and it also may need MySQL 5.

    Something like that…

    Right? I have never done that before, but I believe it is possible… 😊

    EDIT: I changed it a little.

    EDIT: AARRGGGH! I need to read before I hit post. Changed some more!

  • #9 / Mar 10, 2008 5:40pm

    Mark Bowen

    12637 posts

    Hmm,

    One other thought comes to mind on this 😉

    If you got the client to use an SAEF instead of the back-end to enter the entries then you could feasibly use either some Javascript on the form to break up whatever they write into the title field and make that auto-populate two custom fields or you could use some clever code once the SAEF gets submitted to do the same sort of thing with PHP.

    Just a couple more ideas that might be useful or work for you?

    Best wishes,

    Mark

  • #10 / Mar 10, 2008 5:50pm

    Low

    407 posts

    Well, as long as we’re open to options, and with a little inspiration from Jared, you could also use the Query module:

    {exp:query sql="SELECT title, SUBSTRING_INDEX(title, ' ', -1) AS surname FROM exp_weblog_titles WHERE weblog_id = 1 ORDER BY surname ASC"}
      {title}
    {/exp:query}

    Voila, no php needed.

  • #11 / Mar 10, 2008 5:56pm

    Mark Bowen

    12637 posts

    Nice code Lodewijk and Jared!

    Can that be used within a weblog tag as the orderby=”{title}” easily enough though?

    Best wishes,

    Mark

  • #12 / Mar 10, 2008 5:58pm

    Ingmar

    29245 posts

    Not as such, the

    SUBSTRING_INDEX(title, ' ', -1)

    makes all the difference.

  • #13 / Mar 10, 2008 5:58pm

    Sue Crocker

    26054 posts

    Low, ooohhhh.. I like it! I have been needing code like this for a client.

    Thanks to you too, Jared.

  • #14 / Mar 10, 2008 6:01pm

    Mark Bowen

    12637 posts

    Not as such, the

    SUBSTRING_INDEX(title, ' ', -1)
    makes all the difference.

    So how would you use the query to enable the {surname} variable returned by the sql query to be used as the orderby parameter in the weblog tag then?

    Best wishes,

    Mark

  • #15 / Mar 10, 2008 6:08pm

    Low

    407 posts

    You’re not using the weblog module to retrieve the entries, you’re using the query module, thus the orderby parameter isn’t needed. The query module will create the list in the right order. You don’t even need the {surname} variable, because that’s only needed for sorting within MySQL.

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

ExpressionEngine News!

#eecms, #events, #releases