-1

I have a javascript that formats an HTML table that takes some parameters to configure it. I also have a reports table in mysql that contains definitions of my reports: a field for the report name, description, sql, footer, permissions, report status and some report specific parameters. I build up the parameters as an array so that I can set defaults that can then be overwritten by other processes before being passed to a process that turns that array into the javascript parameters.

We use PHP to build reports based on the information in the table and it merges an array of "default parameters" with an array of "report specific parameters" . Before we put the report definitions into a database there was a function for each report that used array_merge that merged the default parameters array and the report specific parameter array.

eg

$defaultparameters = array (
                    'base_path' => "'/inc/plugin/'",
                    'btn_reset' => "'true'",
                    'enable_default_theme' => "'true'");
$thisReportParameters  = array (
                    'extensions' => "[{ name: 'sort', types: ['String', 'String', 'String','String', 'String','String', 'Number']},'col_2'=> "'multiple'",'col_3'=> "'select'");
$parameters = array_merge ($defaultparameters,$thisReportParameters);

This array is then passed to a function that builds the javascript for the page. Now we've put all the reports into a table I'm having trouble pulling out the report specific parameters into an associative array. The default parameters are now set in the report class as a property of the report object. I want to pull the additional report specific parameters from the database into an array to combine the two. When I added 'col_2'=> "'multiple'",'col_3'=> "'select'" to the database and brought it back as an array it put the whole string in as element [0] in the array.
ie

Array ([0] => "'col_2'=> "'multiple'"'col_3'=> "'select'"") 

as opposed to

Array([col2] =>"'multiple'",[col_3]=> "'select'")

I considered just adding the string from the DB to the end of the javascript configuration but this would not overwrite any settings that were already defined in the default settings array so I need to add it to an array first so that when it merges it overwrites the any defaults where the keys are the same.

The report specific options are many and varied in their format so I can't feasibly create a field for each.

I thought about exploding the string on , then parsing the pairs to build the array but the value can have "," in it as well so that might mess up the exploding.

There's probably a really neat way of achieving this but at the moment it escapes me...

Any suggestions welcome

C

Curious User
  • 39
  • 1
  • 6

1 Answers1

0

You could serialize your arrays.

There's also an option to encode them in JSON which may be better if your DB is postgres - it has some built-in support for things like that and it would be easier to work with your data using some other language if need ever arises.

In any case, be sure to properly sanitize your data before trying to put it into the DB.

Daerdemandt
  • 2,281
  • 18
  • 19