Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中生成html表的高效php/mysql查询_Php_Mysql - Fatal编程技术网

在php中生成html表的高效php/mysql查询

在php中生成html表的高效php/mysql查询,php,mysql,Php,Mysql,我的MySql数据库中有3个表 ------------- | countries | ------------- | iso | | name | ------------- -------------------- | documents | -------------------- | id | | country_iso | | publication_date | -------------------- -

我的MySql数据库中有3个表

-------------
| countries |
-------------
| iso       |
| name      |
-------------

--------------------
| documents        |
--------------------
| id               |
| country_iso      |
| publication_date |
--------------------

---------------
| files       |
---------------
| document_id |
| name        |
---------------
您可以建议使用mysql查询和php函数将其打印到如下表中吗。 其中d=documents->files->name,一个特定国家/地区每年可以有许多文档

-----------------------------------------
|           | 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  |      |
-----------------------------------------

提前感谢

如果每个文档只能有一个文件,则应将“文档和文件”表合并,以便“文档”表具有“文件名”字段

本例假设文件和文档之间存在一对一的关系。不必创建异国情调的SQL查询,只需从DB中选择普通数据并使用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
这将从数据库中生成以下表格:

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
...
接下来,必须使用PHP将该表转换为正确的形式。在这种情况下,正确的形式是所有文档按年份索引,然后按客户索引:

$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);
现在,如果您想查找2009年德国的文档,您可以使用
$result['Germany'][2009]。但您可以使用漂亮的循环自动打印表格:

<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>

国家
好了。请注意,我还没有对此进行测试,可能存在解析错误和一些其他不合逻辑的地方,但我猜您从中了解到了这一点