Setting a default value for a dropdown from the front end

BasilC

Member
I have a membership database which includes a fees payments table with an element recording the year that a payment relates to. The membership year runs from 1st May to 30th April. While payments are usually for the current year or for the year that will commence in a few weeks time, this is not always the case, so the person administering the database has to be able to choose which membership year to assign each payment to.

The field uses the dropdown plugin, which works well except for one point which could be improved. For the sake of convenience, I set a default value for the dropdown corresponding ti the year that most new payments will relate to. As things are, this can only be done from the back end. It would be good if the person using the database was able to set the default from the front end, but I can't find any way of enabling this (they are able to add new years to the dropdown, but not new defaults).

Alternatively, if that isn't possible, is there any way that some php code could be used to automatically generate the values for the dropdown list (presumably quite easy to generate a list of the required years, both before now and in the future) while also generating the default? I suspect not, as the default is stored in the params field in the fabrik_elements table ("sub_initial_selection").

It's obviously no big deal for me to edit the default once a year, I'm really just curious as to whether there's a way to enable the front end or to automate it.
 
There are very few things that cannot be accomplished with Fabrik when extending it with a bit of custom code :).

You can populate dropdown options in Advanced -> Eval populate field like:
Code:
$start_year = 2015;
$end_year = date("Y") + 1;

$options[] = JHTML::_('select.option', 0, "Select year...");
for ($x = $start_year; $x <= $end_year; $x++) {
     $options[] = JHTML::_('select.option', $x, $x);
}

return $options;

And set the default in Options -> Default field (set "Eval" to "Yes"). With a custom query you can use a "dynamic" default year something like:
Code:
$mydb = JFactory::getDBO();
$mydb->setQuery("SELECT MAX(year) FROM table-name WHERE user = '{table-name___user_id_raw}' ");
return $mydb->loadResult();

Of course adjust the code to match your set-up and table/element names.

Please note that the "default" only works for new records. When editing existing one, it takes the value that is saved in database field for that element.
 
Last edited:
Thanks juuser. I'm tied up with other things at the moment but will give this a go in a couple of days time. The default is definitely needed only for new records, so that's not a problem.
 
How foolish of me to think there was something that couldn't be done using Fabrik!

I've now got the dropdown population and default value both working, thanks to your help.

The scenario I described was somewhat simplified, so I had to adapt.

The dropdown has both value (eg 2022-04-30) and label (eg 2021/22). The value is actually stored as a date so that I can calculate whether a member is late with their payments. (I have to go into the database table and change the field from text to date after changing anything in Fabrik).

PHP:
$start_year = 2014;
$end_year = date("Y") + 3;

$options[] = JHTML::_('select.option', 0, "Select year...");
for ($x = $start_year; $x <= $end_year; $x++) {
     $options[] = JHTML::_('select.option', $x . '-04-30', $x - 1 . '/' . substr($x,-2));
}
return $options;

The extra years are there just to reassure the membership admin person that she isn't going to run out of years just yet!

The default doesn't depend on who's paying but on what the current date is. The membership year ends on 30th April and it's assumed that anyone paying during March will be paying towards the next membership year not the current membership year that only has two months to run.

So the code I used was

PHP:
if (date("m") > 2)
{
$expiry = date("Y") + 1 . '-04-30';
}
else
{
$expiry = date("Y") . '-04-30';
}
return $expiry;

Many thanks, I would never have worked it out for myself!
 
Just a heads up. The select options as created above are calculated when the form is created, but they also run when viewing the list data. If you have older records for years prior to the start_year you may not get the result you want in the list view. The value stored in the database will not have a corresponding option when the eval populate runs. I suggest you check in the eval populate if this is an existing record and if so make sure the existing database year is included in the options list.

I have run into this before. It is also problematic in the same case when you trim the options list based on existing chosen records (not your case but just for further info). When you do this the eval populate will never have the value in the database. Again, if you check for an existing record you can handle this.
 
Thanks, I wasn't aware of that, but fortunately I used 2014 as the start_year, as I was pretty sure that was when the U3A was set up. After reading your post, I checked, and I was right, it began in April 2014 and the earliest payment records still show the correct year.

I assume that there's no need to run the additional check you suggest, as all years from 2014/15 onwards are being generated by the php code?
 
As long as your start and end year in the php-code cover the years that might be in database, there's no need to worry.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top