Dropdown Advanced Eval Populate - 0 shows as 1

hominid4

Member
I have a dropdown element, with the below query under the Eval Populate of the Advanced tab to build the dropdown. It works as needed except the 0 in the `label` column shows as "1" within the form, so the dropdown shows as:

1
1
2

I've been messing with this a good while and not sure how to prevent that, would anyone have a suggestion where the 0 label comes over as 0?

Thanks!

my_table
id | label (TEXT)
1 | 0
2 | 1
3 | 2

PHP:
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);

$myQuery = 'SELECT id, label FROM my_table';
$myDb->setQuery($myQuery);
$items = $myDb->loadObjectList();

$options[] = JHTML::_('select.option', '', 'Please select');

foreach ($items as $item) {
  $options[] = JHTML::_('select.option', $item->id, $item->label);
}

return $options;
 
Last edited:
Make sure $item->id is a string. Cast it if you need to. Also, you should not use an empty string for the default value, choose something that won't be in your table, a string zero would work in your case.
 
Thanks for the reply. Makes sense about the id being a string, I tried CASTing and CONVERTing but still same result. Was hoping to use the index ID, I went ahead and created a 3rd `value` column, that contains the same value as my index ID and now using 'SELECT value, label FROM my_table' and it's showing correctly now. Thanks again.
 
I don't know your exact workflow.
But if you are only selecting the table values - why don't you use a dbjoin element?
 
Thanks for the follow-up. Mainly because I'm being lazy and trying to spend the minimum time possible on this form - HA! This is a form that will be visible for only one day, which is a multi-page assessment form that has around 80 questions, with most of the elements linking to relationship tables as DbJoins dropdowns and I was maxing out the 64 Db table joins limit that my SQL Db allows so I was doing the remaining as dropdown elements using the Eval Populate. I could of course split the form apart and get around that, but with this being a one-time purpose I, again, am being lazy - HA. Thanks again.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top