Problems, need help? Have a tip or advice? Post it here.
14 posts Page 1 of 2
There are many products with title='product_title'. Something like
'car'
'car'
'car'
'bike'
'bike'
'plane'

I want to output only unique list of names.
'car'
'bike'
'plane'

I could've probably used SELECT DISTINCT, but is it supported, idk.
I guess, sql is overkill here.
Can it be done with regular Couch?

Thank you.
Join COUCH:TALK channel here https://t.me/couchcms_chat
Ryazania — a framework to boost productivity with Add-ons viewtopic.php?f=2&t=13475
Support my efforts to help the community https://boosty.to/trendo/donate
Assuming you are using <cms:pages> to output the pages onto a template, you could always just add an IF just inside of your pages loop.

Something like this:

Code: Select all
<cms:pages masterpage="example.php">
<cms:if (k_page_title eq 'car') || (k_page_title eq 'bike') || (k_page_title eq 'plane')>

<!--This loop will only output pages with the title "Car" "Bike" or "Plane" -->

</cms:if>
</cms:pages>


It's not the most elegant of solutions, but it's a couch solution.
Image
Bartonsweb wrote: <!--This loop will only output pages with the title "Car" "Bike" or "Plane" -->


I need only unique page titles..
If we have 10 cars, I need only one 'car' in output list.

Thanks anyway, I had to explain more clearly maybe.
Join COUCH:TALK channel here https://t.me/couchcms_chat
Ryazania — a framework to boost productivity with Add-ons viewtopic.php?f=2&t=13475
Support my efforts to help the community https://boosty.to/trendo/donate
trendoman wrote:
Bartonsweb wrote: <!--This loop will only output pages with the title "Car" "Bike" or "Plane" -->


I need only unique page titles..
If we have 10 cars, I need only one 'car' in output list.

Thanks anyway, I had to explain more clearly maybe.


Apologies, I misread the "unique" in the question.
AFAIK this is not possible with regular couch, there's no way to determine whether a page already output had the name 'car' so it would just continue on.
Image
This unique thing is useful for typeahead search suggestions.
A visitor inputs first letters, example 'M' then only unique options are populated:
'Mercedes'
'Mazda'
'Mini'

And on k_success we show only what he likes to see - i.e. all cars with title=Mazda.
The list would include Mazda's with all colors and types.

I have put the output into array like this:
Code: Select all
            <cms:capture into='my_array' scope='global' >
           <cms:pages masterpage="catalog.php" custom_field='ref_title!=0'  >
           '<cms:show ref_title />'<cms:if k_count!=k_total_records>, </cms:if></cms:pages>
            </cms:capture>

This gives a string in my_array like this: 'title1', 'title2', 'title2', 'title3'

And I found this solution for deduplication:
array_unique($my_array);

And some folks suggest a much faster method:
$res2 = array();
foreach($my_array as $key=>$val) {
$res2[$val] = true;
}
$res2 = array_keys($res2);

But anyways I have a problem integrating any of these into <cms:php>.
Join COUCH:TALK channel here https://t.me/couchcms_chat
Ryazania — a framework to boost productivity with Add-ons viewtopic.php?f=2&t=13475
Support my efforts to help the community https://boosty.to/trendo/donate
Almost there...
Thanks to this thread http://www.couchcms.com/forum/viewtopic.php?f=4&t=8528&hilit=array

Code: Select all
<cms:php>
$my_titles = array_unique(array(<cms:pages masterpage="catalog.php" custom_field='ref_title!=0'  >'<cms:show k_page_title />'<cms:if k_count!=k_total_records>, </cms:if> </cms:pages>));
global $CTX;
$CTX->set('custom_titles', implode(",", $my_titles),'global');
</cms:php>   
<cms:show custom_titles />


However, it is stripped of all the single quotes now ' '. Will try to concat them..
Join COUCH:TALK channel here https://t.me/couchcms_chat
Ryazania — a framework to boost productivity with Add-ons viewtopic.php?f=2&t=13475
Support my efforts to help the community https://boosty.to/trendo/donate
Ok, the final code is this:

<cms:php>
$my_titles = array_unique(array(<cms:pages masterpage="catalog.php" custom_field="ref_title=ANY" >"<cms:show k_page_title />",</cms:pages>));
global $CTX;
$CTX->set('custom_titles', "'".implode("','",$my_titles)."'",'global');
</cms:php>
<cms:show custom_titles />

1) First, array is populated with k_page_title, separated with comma (php array understands and recieves separated values).
2) $econd, array_unique is applied to it.
3) Then we set the result back to string again from array. And also adding leading single quote, then also the same in the end. And every single value is also divided by one single quote and a comma, like this: start quote ' title1', title2 ' end quote

Now the output is perfectly ready for use in typeahead https://twitter.github.io/typeahead.js/examples/

Like in their examples:
var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut' ]


Thanks for following. Enjoy.

PS that was a nightmare..

Edit: small changes here and there to improve stability (double quotes). Plus ANY is used to find only non-empty values.
Join COUCH:TALK channel here https://t.me/couchcms_chat
Ryazania — a framework to boost productivity with Add-ons viewtopic.php?f=2&t=13475
Support my efforts to help the community https://boosty.to/trendo/donate
@KK, how to get correctly nonempty names? custom_field="ref_title=ANY" didn't work in my case.
Should I provide full template listing?
Join COUCH:TALK channel here https://t.me/couchcms_chat
Ryazania — a framework to boost productivity with Add-ons viewtopic.php?f=2&t=13475
Support my efforts to help the community https://boosty.to/trendo/donate
I presume 'ref_title' is a 'relation' field pointing to another template. Right?
If so, I think the following should output only unique page titles
Code: Select all
<cms:pages masterpage="catalog.php" aggregate_by='ref_title'>
    <cms:show k_page_title />
</cms:pages>
Please test and let me know.

@KK, how to get correctly nonempty names? custom_field="ref_title=ANY" didn't work in my case.
"ref_title=ANY" would only fetch pages that have atleast one related page with the other template. I'm not sure what you mean by 'nonempty names'. Does this refer to empty k_page_titles of 'catalog.php' or to something in the template on the other end. Please calrify.
I meant empty values of an ordinary editable of catalog.php.
In our case it has a name 'ref_title'.
This naming seem strange, but actual k_page_title is already used for other purpose (if you interested, k_page_title used fo reference number of a product, which is not unique sometimes, which explaines why not used k_page_name for that.)

I tried to not fetch pages, where ref_title is not given any value.
There are no relations yet on this editable.

--
A side question: I'm reading CSV with csv_uploader. Would I be able to db_persist relations? I've read somewhere, that all i need to do is have pages on both templates and then give the relation editable value of page_id's comma-separated. Right? Thank you.
Join COUCH:TALK channel here https://t.me/couchcms_chat
Ryazania — a framework to boost productivity with Add-ons viewtopic.php?f=2&t=13475
Support my efforts to help the community https://boosty.to/trendo/donate
14 posts Page 1 of 2