Well, using the “schema” can be a pain. I never use it.
After some thought, I was thinking… Earlier you mentioned:
idnummer | productnaam | v1a2 | v2a1 | v3a2
1 | test 1 | 1 | 2 | 1
2 | test 2 | 2 | 2 | 1
3 | test 3 | 1 | 2 | 2
4 | test 4 | 1 | 2 | 1
So I want to count columns together (not all columns but based on the user selection so I can’t do it hardcoded in the database, it’s dynamic) so the total is
1 = 4
2 = 5
3 = 5
4 = 4
You have an ID, Product and arguments. All quite normal. But, you want totals from columns.
Normally, if you do a query, you are grabbing a group of data from your table. If you need a count from
the entire table, you would use the SUM function inside the query to create a, well, “Grand” total of one
column. If you need a total from columns across one row, you would either create that total when you
create the row of data and store it inside a field. (another column) OR, you grab all of your data from
the query and then as you parse thru the data, you create a count of the columns you want totaled.
So, as I see it, you have two choices.
-
Add a “total” column to the table and create the total when the line is created. This would be done
by using a simple “foreach” loop to parse each column and total it up in PHP before writing the data and
saving the newly totaled amount.
-
After running your query, use a simple “foreach” loop to total up the values and use it however.
I lean to the #1 version as then the total is always there and it is easier to display the total as it is
already there. This depends on the use of this data. If the data is created often and displayed seldom,
them #2 would be best. If the data is seldom created and viewed often, then, #1 is better.
When being logical about database tables, you mostly need to think outside the box and think about
how the data is going to be used. How often it is created, updated, displayed and even deleted are
all important things to think about.
Now, as far as your code goes, I can try to sort out getting it to work with the schema version if you
want to. It is possible. I suspect it is the way it is worded. It is a complicated query, I think…
So, just some more thoughts. Let us know where you would head next…