Forum for discussing general topics related to Couch.
12 posts Page 1 of 2
Hi,
im sorry for asking about this, but believe me i did all i can after reading docs and forum topics but still didn't get any progress.

I haven't experience in PHP and im trying to create the search part and when i type "s" for example the search page is blank without any markup.
could you please help me in simple steps
Thanks.

my index page have this code
Code: Select all
<cms:form method="get" action="<cms:link 'search.php' />" anchor='0'>
                <cms:input type="text" value="" name="s" />
                <cms:input type="submit" value="Go" name='submit' /> </cms:form>


search page have only and nothing else this code
Code: Select all
<?php require_once( 'couch/cms.php' );    ?>
    <cms:search limit='10'>
        <cms:if k_paginated_top>
            <div>
                <cms:if k_paginator_required> Page
                    <cms:show k_current_page /> of
                    <cms:show k_total_pages />
                    <br> </cms:if>
                <cms:show k_total_records /> Pages Found - displaying:
                <cms:show k_record_from />-
                <cms:show k_record_to /> </div>
        </cms:if>
        <h3><a href="<cms:show k_page_link />"><cms:show k_search_title /></a></h3>
        <cms:show k_search_excerpt />
        <hr>
        <cms:paginator /> </cms:search>
    <?php COUCH::invoke(); ?>
Please modify your code, adding this inside the <cms:search /> block:
Code: Select all
    <cms:no_results>
      Search query <pre><p><cms:show k_search_query /></p></pre> produced 0 results.
    </cms:no_results>

It will display some markup if nothing is found.

A side note, stock CouchCMS is able to produce results with at least 4 symbols in a query, based on MySQL default search capabilities.
trendoman wrote: Please modify your code, adding this inside the <cms:search /> block:
Code: Select all
    <cms:no_results>
      Search query <pre><p><cms:show k_search_query /></p></pre> produced 0 results.
    </cms:no_results>

It will display some markup if nothing is found.

A side note, stock CouchCMS is able to produce results with at least 4 symbols in a query, based on MySQL default search capabilities.


you are awesome man you saved my day its working fine :D now!
Thanks a lot

One more question how can i modify queries to accept less than 4 symbols?
One more question how can i modify queries to accept less than 4 symbols?

It's tricky. Search forum, @nsy had this sorted out for 3 symbols with the help of @KK.

However, if you intend to search only within certain text fields, like title etc, then it is possible to make up a custom script with a bit of php to fetch all pages and compare your search query to the content of fields and prepare search results page.

I can add some 'weight' to results, so text appearing in page title would be ranked higher( similar to Couch ). Custom solution would of course allow to search within words, not only queries starting at beginning of words (as in default MySQL).
Hi, I had a question about searching.

My search needs to specifically search 2 fields only - "page_title (or name)" and a custom field "book_responsibility". I don't think that the search can be that specific out of the box.

Is there a way to do this? Trendoman mentions building a query which could do this and produce search results. I've tried this, using return_sql to get the sql query. But it doesn't work, it returns an unspecific error (Could not successfully run query: You have an error in your SQL syntax). I haven't used sql in years (thanks to couch!), and it was never a strong point, so I'm struggling with this. Ideally there would be a way to force the search to only look in the two fields, but I doubt it.

The code I tried (just to try and start building the query I would need - 226 is book_responsibility):
Code: Select all
<cms:set my_sql="SELECT p.id, p.template_id FROM couch_pages p inner join couch_data_text t0 on t0.page_id = p.id WHERE p.template_id='30' AND p.publish_date < '2017-01-27 08:55:14' AND NOT p.publish_date = '0000-00-00 00:00:00' AND p.parent_id=0 AND ( t0.field_id=226 ) AND ( t0.search_value LIKE '%searchquery%' ) ORDER BY p.publish_date desc LIMIT 0, 1"    />
<cms:query sql=my_sql paginate='0' fetch_pages='1' >
    <cms:show k_page_title /><br>
</cms:query>

Thanks in advance!
OK, I guess no-one could help with this. I managed to piece something together.

Code: Select all
<cms:set parameter1="<cms:gpc 's' />" scope="global"/>
<cms:query
    sql="SELECT
        page_title AS k_page_title,
        page_name AS k_page_name
        FROM couch_pages p
        inner join couch_data_text t0 on t0.page_id = p.id
        WHERE template_id='30'
        AND ( t0.field_id=226 )
        AND ( t0.search_value LIKE '%<cms:show parameter1 />%' OR page_title LIKE '%<cms:show parameter1 />%' )
        AND publish_date < NOW()
        AND NOT publish_date = '0000-00-00 00:00:00'
        "
>
    <cms:pages masterpage="book.php" page_name="<cms:show k_page_name />" custom_field="book_collection_belong=yes">
<cms:show k_page_title /><br>
</cms:pages>
</cms:query>

I can't find any documentation on the <cms:query> tag, so I have some questions if anyone can help.
There was another field I needed to check (book_collection_belong=yes), but I couldn't figure out how to add it in the original query. This is why I have a pages tag to show the results. If anyone has any idea how to add this to the sql query that would be great.
Another question, how would I go about paginating these results? Obviously the usual pagination doesn't work on the pages tag, can I add it to the query tag in some way? ( I tried adding paginate="1" but it didn't do anything.)
Finally, how would I override the couch limit of 1000 in this case? I tried adding a LIMIT 0,10000 to the query, but it breaks. I assume that couch is applying a limit to the sql directly?
Thanks.
I can't find any documentation on the <cms:query> tag

1. Documentation exists here viewtopic.php?f=5&t=7377
It can be found via googling "cms:query site:couchcms.com/forum"
Maybe scarce, but it's enough because of next point:

2. <cms:query /> is an advanced tool for very advanced users of CouchCMS. Normally, such people know sql or can educate themselves on the matter.
I guess no-one could help with this
This brings us to your above statement, which is simply not true. There are some advanced users in forum who can spend time to figure it out, provide code and/or educate you, which brings the situation to next point

3. Hire someone or become advanced by educating yourself through experiments and sharp questions if nothing helps.
Thank you for pointing me to the documentation. I could not find it using the in-site search facility (believe me I tried).

I thought that what I was doing was trying to educate myself through experimentation - the original query did not work, so I searched the forums and pieced together a query which did work. I do have some sql experience, but it has been a couple of years since I had to use it, mainly because of couch. I managed to build a query which does most of what I want, and I think that the documentation fills in the blanks for the rest of it.

I also thought that the questions I asked were sharp enough, but obviously they were not up to standard. My statement that "I guess no-one can help me with this" is indeed untrue, I suppose I was probably frustrated that no-one had answered me after 3 days. For which I apologise.
Accepted :)
If any sharp question left (apart from "Is there a way to do this?"), I'd be happy to answer with a piece of code.
Thanks trendoman,

I think I've managed to achieve what I needed to for now. Basically I am building a mail order book site, and I was wanting to specifically search just the page_title, a field called book_responsibility, and to check whether the book had a dropdown (book_collection_belong) selected as "yes" - return_sql="1" really helped me! It was mostly figuring out I had to user inner join twice to get two fields that was holding me back.
Just for reference, the following code seems to be working:

Code: Select all
<cms:query
      sql="SELECT
        p.id, p.template_id,
        page_title AS k_page_title,
        page_name AS k_page_name
        FROM couch_pages p
        inner join couch_data_text t0 on t0.page_id = p.id
        inner join couch_data_text t1 on t1.page_id = p.id
        WHERE template_id='30'
        AND ( t0.field_id=226 )
        AND ( t1.field_id=243 )
        AND ( t0.search_value LIKE '%<cms:show parameter1 />%' OR page_title LIKE '%<cms:show parameter1 />%' )
        AND ( t1.search_value LIKE '%yes%' )
        AND publish_date < NOW()
        AND NOT publish_date = '0000-00-00 00:00:00'
        "
    limit='10'
    paginate='1'
    fetch_pages='1'
>
12 posts Page 1 of 2