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.
SELECT
countries.name country,
documents.name document,
files.name filename,
DATE_FORMAT(publication_date, '%Y')
FROM
documents
LEFT JOIN
countries
ON
documents.country_iso = countries.iso
LEFT JOIN
files
ON
documents.id = files.document_id
That will result in a followin table from the database:
country | document | filename | year
----------------------------------------------------
Germany | doc_1 | doc_1.pdf | 2009
Germany | doc_2 | doc_2.pdf | 2007
Germany | doc_3 | doc_3.pdf | 2007
Norway | doc_6 | doc_6.pdf | 2008
...
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:
$q = mysql_query("SELECT ...");
$result = array();
$years = array();
while($row = mysql_fetch_assoc($q)) {
$result[$row['country']][$row['year']][] = array('document' => $row['document'], 'filename' => $row['filename']);
$years[] = $row['year'];
}
// Max and min years are used to print the table header
$max_year = max($years);
$min_year = min($years);
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:
<table>
<tr>
<th>Country</th>
<?php for($y = $max_year; $y >= $min_year; $y--): ?>
<th><?php echo $y; ?></th>
<?php endfor; ?>
</tr>
<?php foreach($result as $country => $years): ?>
<tr>
<td><?php echo $country; ?></td>
<?php for($y = $max_year; $y >= $min_year; $y--): ?>
<td>
<?php if(isset($years[$y])): foreach($years[$y] as $document): ?>
<a href="<?php echo $document['filename']; ?>">
<?php echo $document['name']; ?>
</a>
<?php endforeach; endif; ?>
</td>
<?php endfor; ?>
</tr>
<?php endforeach; ?>
</table>
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.
精彩评论