Populate distinct values in select drop-down from MySQL database
Populate distinct values in select drop-down from MySQL database

I have a custom form template for datatables editor, with a field called region, for which I would like to populate a select drop-down using values from the database, and not predefined using options array. The problem is that even though it does populate the values, there are duplicates as it pulls the values of the entire table and not only distinct values, as it should in terms of a select field. My PHP is included in excerpt A and the js in excerpt B:
Excerpt A:
Field::inst( 'Region' )
->validator( Validate::notEmpty() )
->options( Options::inst()
->table( 'tblusers' )
->value( 'MemId' )
->label( 'Region' )
),
Excerpt B:
{
"label": "Streek:",
"name": "Region",
'className': 'full block',
"type": "select",
"def": "Kies n Streek",
/*"options": [
"Gauteng",
"KwaZulu-Natal",
"Limpopo Provinsie",
"Mpumalanga",
"Noord-Kaap",
"Noordwes",
"Oos-Kaap",
"Vrystaat",
"Wes-Kaap"
]*/
},
I don't seem to find any documentation on SQL Distinct for datatables editor? Any advise for this use case please?
This question has accepted answers - jump to:
Answers
Hi @avrrr ,
This thread should help, it's asking the same thing.
Cheers,
Colin
Hi Collin
Thanks for the info, I will give it a try and let you know the outcome, I am however not using joins, all the data is from the same table in the database.
Thanks
HI Colin,
Unfortunately it does not work.
Thanks
Hi @avrrr ,
Could you give more information, please, on what you've tried and why it doesn't work?
Cheers,
Colin
Hi Colin
As per the thread you provided, I made the changes to the code to use distinct SQL queries, it however still gave me multiple values in the select dropdown. I have used this exact approach on custom PHP and SQL code (outside editor) with no issues, but for some reason when I am struggling to achieve this with editor.
Thanks
Hi avrrr,
Could you show me your full PHP code for this Editor instance please? Also can you use the debugger to give me a trace please - click the Upload button and then let me know what the debug code is.
Thanks,
Allan
Hi Allan
The debugger is stuck on "gathering data". We have about 10k members in the database loaded via server-side ajax.
Thanks for the code. Could you try changing the last two lines to enable debugging - e.g.:
The Options class does a distinct by default, so I'm guessing that
MemId
andRegion
are unique for every option that it shown, even if it looks likeRegion
is not unique.That would make sense since
MemId
appears to be your primary key fortblusers
(from the initialisation line).What I'm not clear on is why you would want to set
Region
to be the primary key from thetblusers
table (which in this case is your host table). Should the value actually beRegion
here? That would mean that existing regions would be shown in the list.Allan
Hi Allan
The MemId field is my primary key for this table and Region has multiple values, but even though a certain value might appear numerous times, only distinct values must be present in the drop-down options, hence the distinct option is required. If you could advise how to fix this it would be appreciated, but let me just test and get back to you as I think that might just the mistake I made - the primary key...
Hi Allan
It is working now, it was in fact due to the mistake I made in using the primary key as opposed to the field value in the options declaration.
Thanks for your assistance!