Php bir html tablo oluşturmak için verimli php / mysql sorgu

1 Cevap php

Benim MySQL veritabanında 3 tablolar var.

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

Şimdiden teşekkürler

1 Cevap

Her bir belge başına sadece bir dosya olması durumunda belgeleri tablo bir dosya alanı var, böylece belgeleri ve dosyaları tablo birleştirmek gerekir.

Bu örnek, dosya ve belgeler arasında bire-bir ilişki varsayar. Bunun yerine egzotik SQL sorguları oluşturma, sadece DB düz verileri seçin ve PHP kullanarak dönüştürmek için basittir.

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

Bu veritabanından bir izlemeye tabloda neden olur:

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

Sonraki bu tablo uygun form PHP ile dönüştürülmüş olmalıdır. Bu durumda uygun form tüm belgeleri sırayla müşteri tarafından endeksli yıl endeksli olurdu:

$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);

Eğer 2009 yılı için Almanya'nın belgeleri bulmak istiyorsanız Şimdi, sadece kullanabilirsiniz $result['Germany'][2009];. Ama otomatik olarak tabloyu yazdırmak için şık bir döngü kullanabilirsiniz:

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

Buyurun. Ben bu test değil unutmayın, orada ayrıştırma hataları ve diğer bazı mantıksızlıklarla olabilir ama ben bu gelen fikri olsun sanırım olabilir.