Long answer:
Can be done. The code that I have below should not be used in production. It's just a demo. There might be many ways to achieve similar functionality but this is one – albeit, buggy – way.
let's say the settings in the DB are:
---------------------------+----------------