Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#181 2008-11-20 18:04:54

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,243
Website GitHub

Re: smd_if: Generic multiple if condition tests

nabrown78 wrote:

if that person’s name is searched on, I’d like to return only the most recent of those articles.

*head scratch* OK, let’s see if I have this straight:

  • your visitor searches for “nora”
  • your results might return 5 articles called Nora, plus a load of other results where Nora has been mentioned in the body text or something
  • you want all the other search results left alone, but if you see more than one article with a Title (not url-only-title, but article Title) that matches the exact search term AND that search term is someone’s name, you only want to display one of them; the most recent

?? Eek!

EDIT: ok, our posts crossed. So you’re matching part of the article bio title with the search term. This gets even more fun… are all your bios in one section?

EDIT2: Why is Joe Schmoe not updating his original bio? Why is he writing a fresh one? Is this some kind of freaky version control system? :-)

Last edited by Bloke (2008-11-20 18:09:33)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#182 2008-11-20 18:18:49

nabrown78
Member
From: Northampton, MA, USA
Registered: 2006-10-04
Posts: 294
Website

Re: smd_if: Generic multiple if condition tests

Yeah…it’s kind of weird I know. You have it right though. Separating the bios out from other articles is no problem – different sections (all the bios are in a section called “contributors”.
This is a website for an academic journal, so we want to replicate the sort of coherency of a print journal. Each issue has a list of contributors, each with a short bio. But the bio for Joe Schmoe in 2009 might list a book just published, that shouldn’t be included in his bio from 2004.
I guess it will have to match part of the title, because I can certainly imagine someone searching on just a last name, and having multiple contributors with the same last name.

You think it’s possible to achieve what I want? I thought as a last resort, I could use a custom field or the keywords field to somehow “tag” which is the most current article.

Offline

#183 2008-11-20 18:51:44

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,243
Website GitHub

Re: smd_if: Generic multiple if condition tests

nabrown78 wrote:

You think it’s possible to achieve what I want?

Probably. Anything’s possible :-)

One question while I think about it: is the search limited to the contributors section? i.e. do they go to a specific search box that is labelled “search authors here” or something? In that case it’s probably a heck of a site easier to achieve, because we know there won’t be any other articles polluting the search results.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#184 2008-11-20 19:09:11

nabrown78
Member
From: Northampton, MA, USA
Registered: 2006-10-04
Posts: 294
Website

Re: smd_if: Generic multiple if condition tests

Hm. Interesting. No, not at the moment. It is just one universal search box. The only articles included in search are from the sections “Contributors” and “Articles”, and I currently use sorting by section and <txp:if_different> on the results page to separate them.

How would you set up one search box to search one section, and separate one to search a second section? That could come in handy for sure.

Thanks for taking the time to ponder this!

Last edited by nabrown78 (2008-11-20 19:09:30)

Offline

#185 2008-11-20 19:17:13

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: smd_if: Generic multiple if condition tests

Hi,
I seem to have a problem with the plugin.
I want to test if a custom_field value is empty OR if it contains the character “|”.
My code is as follows:

<txp:smd_if field="my_custom_field_name" operator="contains, isempty" value="|" logic="or">
do this
<txp:else />
do that
</txp:smd_if> 

The problem is only the first condition appears to be checked (the field contains “|”).
I’ve tried changing the order of the “operator” attribute (operator=“isempty,contains”) and I’ve the same problem: only the firsrt condition is checked (in this second case if the field is empty).
Am I overlooking something?
Thanks in advance

Offline

#186 2008-11-20 19:24:42

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,243
Website GitHub

Re: smd_if: Generic multiple if condition tests

redbot wrote:

Am I overlooking something?

Sadly, you have to duplicate the field you are looking for; that is what the plugin iterates over.

I did consider (may still implement, if I can make it work) iterating over the operator so you can use your much shorter syntax if you are checking the same variable. But for now you’ll need:

<txp:smd_if field="my_custom_field_name, my_custom_field_name" operator="contains, isempty" value="|" logic="or">
...

Sorry about that. Bit of a stupid feature really, I’ll try and fix it to work more intuitively next release.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#187 2008-11-20 19:29:52

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: smd_if: Generic multiple if condition tests

Thank you Bloke, it works now!!
And I suppose it was a stupid error from my side not yours.
Thank you again

Offline

#188 2008-11-20 19:50:11

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,243
Website GitHub

Re: smd_if: Generic multiple if condition tests

nabrown78 wrote:

How would you set up one search box to search one section, and separate one to search a second section?

Ah, my blunder. You can’t, as I forgot it searches all the articles listed as searchable from the Sections tab. For some reason I thought you could specify the sections you want to search in the tag.

But since you are sorting by section anyway that may be to our advantage. If you sort by section and descending date we might be in business. It’s not pretty but how about using <txp:variable /> to keep track of what you’ve found? This technique revolves around being able to use conventional tags inside a search form which I think you can do, but I’m not sure. Untested, but for each article in your search results form:

<txp:if_article_section name="Contributors">
  <txp:smd_if field="txpvar:found_users" operator="contains" value='<txp:title />'>
    // Do nothing
  <txp:else />
    <txp:variable name="found_users" value='<txp:variable name="found_users" /> : <txp:title />' />
    // .. And output your search result info here...
  </txp:smd_if>
</txp:if_article_section>

So what that is doing:

  • only worrying about articles in the Contributors section
  • if we’re there, it looks at the found_users variable and asks if it contains the current article title
  • if not (it won’t first time) it adds the title to the found_users array (separated by a colon for the sake of argument, it doesn’t matter what you use), and outputs the article info as part of the search results
  • in subsequent articles, if it finds a title it has already seen it does nothing, i.e. skips that result

It’s totally hatstand but as long as the results are ordered in descending order it finds the most recent first and flags it to say “found it”, thus ignoring any others it may encounter with the same name later.

I might be way off base as I don’t know if you can use normal article tags inside search results, but it’s wacky as sin if you can!


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#189 2008-11-20 20:09:14

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: smd_if: Generic multiple if condition tests

nabrown78 wrote:

How would you set up one search box to search one section, and separate one to search a second section?

Bloke wrote:

Ah, my blunder. You can’t, as I forgot it searches all the articles listed as searchable from the Sections tab. For some reason I thought you could specify the sections you want to search in the tag.

I think you can:
how-do-i-search-in-the-current-section-only
;)

@Bloke, this line:
bc. <txp:variable name=“found_users” value=’<txp:variable name=“found_users” /> : <txp:title />’ />

made my brain explode. Could you explain it, please?
Also, you are using the variable “txpvar:found_users” on the first “branch” of smd_if. But… can you use it if it ain’t already created/defined? (or is it defined somewhere else?)

BTW, using txp:if_article_section to filter out articles on a search is a nice trick, but will probably lead to an overhead in the search, and also, a wrong number of search results.

I will stay tuned to this thread to keep learning more bloke’s shamanistic tricks.

Last edited by maniqui (2008-11-20 20:09:30)


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#190 2008-11-21 00:58:42

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,243
Website GitHub

Re: smd_if: Generic multiple if condition tests

maniqui wrote:

how-do-i-search-in-the-current-section-only

Cool, thanks for the tip. That makes it even easier because we can essentially ignore all other sections, thus remove the if_article_section call. But either way, you are right: filtering search results after the event will corrupt the result counts, so they cannot be used.

this line: <txp:variable name=“found_users” value=’<txp:variable name=“found_users” /> : <txp:title />’ /> made my brain explode. Could you explain it, please?

Certainly. Let’s step through it like the computer would:

  1. The txp:variable named “found_users” does not exist. As far as I know, (I’ve not tried it) you can use variables without “declaring” them and they’ll just return nothing. But if you can’t do that, then somewhere ahead of time — outside your search results form — you can set up an empty variable of that name with <txp:variable name="found_users" value="" />
  2. To parse a statement, the parser always goes as far “in” as it can first, then works “out”. So it finds the deepest nested statement in that line and comes up with <txp:variable name="found_users" /> : <txp:title />
  3. It evaluates each tag in turn, returning "" (empty) for the txp:variable and, say, Joe Schmoe as the current article title. Thus at this point the parser has made an internal note of the following text:   : Joe Schmoe (note the space at the start)
  4. It then moves “out” one level and discovers <txp:variable name="found_users" value='{result of the thing I just made a note of}' />
  5. It performs the replacement and thus the statement becomes: <txp:variable name="found_users" value=' : Joe Schmoe' />
  6. The result is that the variable is set with that value

Then it looks at the next search result, let’s say it’s an article about Joe Plumber: here’s how it goes down:

  1. The txp:variable named “found_users” contains the value   : Joe Schmoe
  2. The smd_if checks the contents of this variable and sees that Joe Plumber is not contained in its value
  3. It looks at the brain explody line, goes “in” as far as it can and sees <txp:variable name="found_users" /> : <txp:title /> again
  4. Evaluating each tag returns 1)   : Joe Schmoe, 2) a space-colon-space, 3) Joe Plumber. The result of all that lot together is   : Joe Schmoe : Joe Plumber, which it makes a note of
  5. Moving “out” a level, it yet again sees <txp:variable name="found_users" value='{result of the thing I just made a note of}' />
  6. Performing the replacement makes: <txp:variable name="found_users" value=' : Joe Schmoe : Joe Plumber' />
  7. The value of the variable is thus altered: it is now the current article title appended to the old contents of the variable (each separated by a colon — as I said earlier, you can use any separator you like here, it’s not even needed but it keeps things tidier in my brain)

This carries on for each article, building up the variable as it goes, appending new article titles to it. The fact you have an ‘empty’ entry at the start of the list is (luckily) irrelevant here.

If at any time, the smd_if finds a title it has “seen before” (i.e. one that has been stored in found_users) it skips it and does not append it to found_users and does not output its search results.

The net effect is that the very first time an article title is encountered it is displayed and has its title appended to whatever was in found_users. Subsequently, any duplicate titles are ignored. So if the articles are arranged in descendiong date order, the only ones you see are the most recent unique titles.

I’m sure with smd_query this would be one line of SQL, but hey; that’s cheating, right?!

It’d be great — as I mentioned in another thread — if txpvars could be appended to / removed from, without having to do stuff manually yourself like this example but, as wet pointed out, it’s a slippery slope.

Does that lead to the path of enlightenment, or have I pushed you even further into a programmers’ abyss :-)

Last edited by Bloke (2008-11-21 01:08:56)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#191 2008-11-21 01:17:58

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: smd_if: Generic multiple if condition tests

Very very clever. I liked it. :)


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#192 2008-11-21 02:00:28

nabrown78
Member
From: Northampton, MA, USA
Registered: 2006-10-04
Posts: 294
Website

Re: smd_if: Generic multiple if condition tests

Wow. How the hell did you figure that out? I’m going to have to think on that code some more…
I just got wise to txp:variable today – it’s only in the soon-to-be-released 4.0.7, right? Are there any docs anywhere about how this guy works? I read through the orig. forum thread. Can you set it in the admin interface as well? Or only in page templates, forms, etc?

Bloke thanks for your help on this…I guess I should look at the smd_query you recommend too!

Offline

Board footer

Powered by FluxBB