July 8, 2012

manage module's options approach

Question by mhesabi

I’m about to create let say a custom CMS with php and mysql. each page has a few zones. each zone can output modules. for example we have a news module and we already added news categories and single news items at module admin section.
but here it begin: end user wants to get different output of this module. for example user wants to show news of “sport” category, or user wants to show top 5 last news, or user wants to sort news by something, or, or, or …. many other options that a module can take.

my question is what is your solution to handle and manage these options.
– do you suggest a module_option table for every module like news_options?
– do you suggest a single table that holds every modules options?

hope I could get what i mean 😉 also let me know if db diagram is necessary.

Answer by Starx

  1. First approach could be using different table for each module. Then you can keep the settings of different instance in the fields. In the manageable form.

    Example:

    Table: News
    ----------------------------------------------------------------------
    | id      +     news_title     +     option 1     +    option 2      |
    ----------------------------------------------------------------------
    
  2. Next approach, could me using Global configuration table, which holds the key name and one field and key value in another field. This way you dont have to create different table from different modules.

    Example:

    Table: ModuleOption

    --------+-----------------+-------------------+---------------------+
    |  id   |   module_id     |    setting_name   |   settings_value    |
    --------+-----------------+-------------------+---------------------+
    |   1   |   1             |     option 1      |   option value 1    |
    --------+-----------------+-------------------+---------------------+
    |   2   |   1             |     option 2      |   option value 2    |
    --------+-----------------+-------------------+---------------------+
    

The questions about which one to choose over which, depends on how you code your project. Both have its own advantages and disadvantages. However, some points to consider

  • If the option contains a method to input long text then, second approach is bad as the size of options will vary a lot.
  • If all the module settings can accompany with in fixed length fields, then second option is definitely the best way.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!