Skip to content
Advertisement

Build a multidimensional PHP array out of MySQL table data linked by values in one column

I am using a WordPress plugin (Advanced CF7 DB). It puts the results of the contact form into the MySQL database so it can be displayed on the front end of the website.

There are two tables with data like this.

Table 1:

JavaScript

Table 2:

JavaScript

The id in table 1 corresponds to the data_id in table 2.

I need to display the comment on the website in two separate lists (for people above and below 18), and only display comments where there is a row with ‘name = display’ and ‘value = yes’ for the corresponding data_id.

Example:

Comments from people under 15:

JavaScript

Comments from people over 18

JavaScript

Would the best approach to be to put the information into a multidimensional PHP array where I can manipulate it, or should I try to use MySQL to get the information in the format I need it?

Advertisement

Answer

I’m not sure why your table is formatted that way in the first place, but you could devise your row values into columns first, then using HAVING clause to filter by your criteria:

Here’s a sample:

JavaScript

Right after that it’s just a matter of fetching it. Sidenote: I’m not a wordpress guru, but it’s fairly straightforward, execute the query -> fetch the results, loop them in your markup.

JavaScript

It’s just a sample, you can squeeze in your markup html whichever you like.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement