I have 3 tables in my MySql database.
-------------
| countries |
-------------
| iso |
| name |
-------------
--------------------
| documents |
--------------------
| id |
| country_iso |
| publication_date |
--------------------
---------------
| files |
---------------
| document_id |
| name |
---------------
Could you suggest a mysql query and php function to print this to a table as below.
Where d = documents->files->name, each year can have many document for a particular country.
-----------------------------------------
| | 2009 | 2008 | 2007 | 2006 |
-----------------------------------------
| country a | d d | | d | d |
| country b | | d | d | |
| country c | d | d d | | d |
| country d | d | | d d | |
-----------------------------------------
Thanks in advance
If there can be only one file per each document, you should combine the documents and files table so that the documents table has a filename field.
This example assumes one-to-one relation between files and documents. Instead of creating exotic SQL-queries, it’s simpler to just select plain data from DB and transform it using PHP.
That will result in a followin table from the database:
Next that table has to be transformed with PHP to proper form. The proper form in this case would be that all documents are indexed by year which are in turn indexed by customer:
Now, if you would like to find Germany’s documents for year 2009, you could just use
$result['Germany'][2009];. But you can use a nifty looping to print the table automatically:There you go. Note that I haven’t tested this, there might be parse errors and some other illogicalities but I guess you get the idea from this.