Forum for discussing general topics related to Couch.
11 posts Page 1 of 2
Okay so this sounds a bit weird in my head so hopefully I can explain this well :)

I'll use an example, let's say I'm using cloned pages to keep a database of products. I want a search box that runs some custom code ONLY IF the search term matches a specific field EXACTLY, if not then the search should fail.

Is this something I can do? It should resemble something like this:

Code: Select all
search for term "AmazingProductName".

if "AmazingProductName" exists as k_page_title
    Do this code
if not EXACT match
    Do this code


Thanks!
Hi,

Please try the following code (make sure to set the 'masterpage' parameter to your actual template) -
Code: Select all
<cms:set my_search_term="AmazingProductName" />

<cms:if my_search_term != '' >
    <cms:set my_page_exists="<cms:pages masterpage='whatever.php' page_title=my_search_term limit='1' count_only='1' />" />
</cms:if>

<cms:if my_page_exists >
    Page found. Do this code.
<cms:else />
    Page not found. Do this code.
</cms:if>

It should search for a page titled 'AmazingProductName' and then execute code depending on the result (of course, you should use an actual title here).

Once you find that it works the way you want, instead of using hardcoded titles, use the value submitted through your form by making the following change to the first line -
Code: Select all
<cms:set my_search_term="<cms:gpc 's' method='get' />" />

The modified code assumes the textbox used for the search is named 's'. Change it to match that you use.

Hope this helps.
PERFECT! You're a genius KK! Thanks for the code and Couch in general!
You are welcome :)
I am glad it helped.
I'm trying to use this approach and it's not working! To try to troubleshoot it, I've pared my actual code down to this:

Code: Select all
<cms:set my_search_term="2020-08-20_10:00_S. Paul, Lenton" />
<cms:if my_search_term != '' >
    <cms:set my_page_exists="<cms:pages masterpage='masses.php' page_title=my_search_term limit='1' count_only='1' />" />
</cms:if>

<cms:if my_page_exists >
    Page found. Do this code.
<cms:else />
    Page not found. Do this code.
</cms:if>

<br />==========================<br />
S:|<cms:show my_search_term />|<br />
<cms:pages masterpage='masses.php' id= '2745'>
    M:|<cms:show k_page_title />|
</cms:pages>


Down to the line of = characters, it's the exact code from KK's example, with only the search term and the template name changed.

Below the line, I've output the search term and the name of a page which I know matches the search, having surrounded each with | characters to make sure there are no trailing spaces or anything.

The output is this:
Code: Select all
Page not found. Do this code.
==========================
S:|2020-08-20_10:00_S. Paul, Lenton|
M:|2020-08-20_10:00_S. Paul, Lenton|


So the cms:pages expression is returning 'false' (i.e.0), although the page title does match!

What's going on here? Is it to do with the fact that the page name includes punctuation, and if so what can be done about it (I can't change the elements which got to make up the page title - here a date, time and church name).
@daldred, searching for the 'page_name' (instead of the title) is likely to give you more consistent results.

If in the admin-panel, as is the default, the page names have been auto-generated by Couch using the titles, you may do the same in your code and use something like the following to convert the title to name -
Code: Select all
$name = $FUNCS->get_clean_url( $title );

Hope this helps.
A function is handy -
Code: Select all
<cms:func 'title2name' title=''>
    <cms:php>
        global $FUNCS, $CTX;
        echo $FUNCS->get_clean_url( $CTX->get('title') );
    </cms:php>
</cms:func>

Usage -
Code: Select all
<cms:call 'title2name' title=k_page_title />
Thanks, @KK and @Trendoman. For clarity for anyone who later finds this - the changes needed were
  • to set the search term to the page name - using Trendoman's function
  • then limit the cms:pages expression by the page_name instead of page_ title.

So my code ended up as:
Code: Select all
<cms:func 'title2name' title=''>
    <cms:php>
        global $FUNCS, $CTX;
        echo $FUNCS->get_clean_url( $CTX->get('title') );
    </cms:php>
</cms:func>

<cms:set my_search_term="<cms:call 'title2name' title="2020-08-20_10:00_S. Paul, Lenton" />" />

<cms:if my_search_term != '' >
    <cms:set my_page_exists="<cms:pages masterpage='masses.php' page_name=my_search_term limit='1' count_only='1' />" />
</cms:if>

<cms:if my_page_exists >
    Page found. Do this code.
<cms:else />
    Page not found. Do this code.
</cms:if>

<br />==========================<br />
S:|<cms:show my_search_term />|<br />
<cms:pages masterpage='masses.php' id= '2745'>
    M:|<cms:show k_page_title />|
</cms:pages>


...which now returns

Code: Select all
Page found. Do this code.
==========================
S:|2020-08-20_1000_s-paul-lenton|
M:|2020-08-20_10:00_S. Paul, Lenton|


(I've left the final output of the page title to demonstrate the difference).
Hello,

What is wrong here in this code

Code: Select all
<cms:if k_logged_in >

         <cms:set my_registration_exists="<cms:pages masterpage='registrations.php' custom_field='reg_competition=<cms:show competition_title /> | reg_driver=<cms:show k_user_name' />" />
                               
                                <cms:if my_registration_exists >
                                    Page found.
                                   
                                    <cms:else />
                                    Page not found.

                                </cms:if>

                            <cms:else />
                            <button>Logi sisse!</button>
                       
</cms:if>


It only shows page not found when logged in but physically that cloned page exists with user and competition confirmed.
rFPlanet wrote: Hello,

What is wrong here in this code

Hi,

If something as a whole is not working as expected, then very probably some part of that whole is not right. Obviously you should work harder :)
11 posts Page 1 of 2
cron