Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jpeterson579's avatar

Help structuring DB table for grid that uses 1 DB table for rows and 1 for columns

Hi all,

So I am trying to create a table grid for product testing where each cell in the table has a dropdown for pass or fail. Each row is a specific test-question. And each column is a single product.

My db structure is like this: test hasMany questions question hasMany products

The issue I can't wrap my head around is that each test-question may only relate to 3 out of the 5+ products/columns, therefore that cell should exist but be blank (without the pass/fail dropdown).

How would you structure your db tables and data loops to accomplish this?

0 likes
3 replies
lbecket's avatar
lbecket
Best Answer
Level 39

You can achieve this in a single table with fields for product, question, test result, etc.

As for representing this in a grid, just query the table with the necessary sort order and then when you iterate through the dataset you can conditionally create a new row/column as you encounter new products/questions. If you need to force an intersection that doesn't exist in the data, you should be able to make this determination by first establishing a count of questions and products and looping accordingly.

1 like
jpeterson579's avatar

@lbecket Thanks for your input. That makes sense but then how would I avoid my columns not lining up properly?

For example: If row 1 (test questions) relates to only 3of5 products and then row 2 relates to 4of5, wont the columns be mismatched?

How would I keep product 3 in column 3 regardless if it is in the test row or not.

lbecket's avatar

I'm envisioning a nested loop where you're iterating over the maximum number of questions and the maximum number of products. Then, within your loop you use a conditional statement to evaluate whether that question/product combination exists. If it does, you return the test result value. If it doesn't, you return null or some other value that creates a blank cell in the grid.

1 like

Please or to participate in this conversation.