2012-08-02 70 views
0

我一直在閱讀一些q & a,但仍然沒有設法構建我的算法,所以我在尋求一些關於如何操作的幫助/建議。用php和mysql動態創建一個表格

所以,我有一個MySQL表和我想要在表中顯示它的每個mysql錶行。我希望我的表只有4個cols(意思是根據數據庫中的數據,行數是可變的)。因此,在html表格(第1行,第1列)中將來自數據庫表第1行的數據,來自數據庫表格第2行的數據(行2,列1)第5行等。

因此,該表將是這樣的:

[數據從分貝行#1] [從分貝行#數據2] [從分貝行#數據3]從分貝行#4數據]

[從分貝行#5的數據] [從分貝行#數據6] [從分貝行#數據7] [數據從分貝行#8]

等等...

這是我必須從數據庫中獲取數據:

function get_albums() { 
    $albums = array(); 

    $albums_query = mysql_query("SELECT 'albums'.'album_id', 'albums'.'role_id', 'albums'.'timestamp', 'albums'.'name', LEFT('albums'.'description',50) as 'description', COUNT('images'.'image_id') as 'image_count' 
    FROM 'albums' 
    LEFT JOIN 'images' 
    ON 'albums'.'album_id' = 'images'.'album_id' 
    GROUP BY 'albums'.'album_id'"); 

    // Loop through all images 
    while ($albums_row = mysql_fetch_assoc($albums_query)) { 
     // multidimensional array 
     $albums[] = array(
        // associative array 
        'id' => $albums_row['album_id'], 
        'role' => $albums_row['role_id'], 
        'timestamp' => $albums_row['timestamp'], 
        'name' => $albums_row['name'], 
        'description' => $albums_row['description'], 
        'image_count' => $albums_row['image_count'] 
     ); 
    } 
    return $albums; 
} 

要顯示的信息:

$albums = get_albums(); 

echo '<table>'; 
for ($i=0; $i<(count($albums)/4); $i++) { 
    echo '<tr>'; 

    //HERE IS WHERE I'M LOST 

    foreach ($albums as $album) { 
     echo '<a href="view_album.php?aid=',$album['id'],'">',$album['name'],'</a> (',$album['image_count'],') image(s)<br />',$album['description'],'...'; 

    } 
    echo '</tr>'; 
} 
echo '</table>'; 

因此,任何想法?

謝謝。

+1

你忘了你的''​​標籤和用'串聯,',而不是'.'。 – Matt 2012-08-02 16:59:58

+0

您將要使用[模數運算符](http://php.net/manual/en/language.operators.arithmetic.php),並在for循環中除去4。和馬特所說的+1。 – Josh 2012-08-02 17:00:37

+0

首先,您的查詢中的concatting不正確。其次,您要爲每個數據條目選擇多個列,但是您希望將其全部顯示爲一個單元格?我是否正確理解這一點? – Palladium 2012-08-02 17:00:39

回答

3
// keep calculations outside of the for loop if you can 

// this will display all albums (count($albums)/4) times. 

$limit = count($albums)/4; 
for ($i = 0; $i < $limit; $i++) { 
    echo '<tr>'; 

    foreach ($albums as $album) { 
     echo '<td><a href="view_album.php?aid=' . 
      $album['id'] . '">' . $album['name'] . '</a> (' . 
      $album['image_count'] . ') image(s)<br />' . 
      $album['description'] . '...</td>'; 
    } 
    echo "</tr>"; 
} 

什麼,你可能尋找的是

<?php 
$counter = 0; 

foreach($albums as $album) : 
    // start new row 
    if ($counter == 0): ?> 
     <tr> 
    <?php endif; ?> 
    <td><a href="view_album.php?aid=<?= $album['id'] ?>"><?= $album['name'] ?></a> (<?= $album['image_count'] ?>) image(s)<br />   
     <?= $album['description'] ?>...</td> 
    <?php if ($counter++ == 3) : ?> <!-- 4 records printed in row; end row & reset counter --> 
     </tr> 
    <?php 
     $counter = 0; 
    endif; 
endforeach; 

$lastCount = $counter; 
// print remaining empty cells 
while ($counter ++ < 4) : ?> 
    <td>&nbsp;</td> 
endwhile; 
// close row if necessary 
if ($lastCount != 3) : 
    </tr> 
endif; 
+1

這不太合適。這將輸出儘可能多的'​​',因爲每行都有相冊,並且有25%的專輯有多少行。 – Josh 2012-08-02 17:07:00

+0

@Josh我剛剛注意到這一點,並編輯完整性和清晰度。謝謝! – Matt 2012-08-02 17:08:43

+0

您的更新將爲每行創建1個專輯,而不是OP的目標,即每行4個專輯。 – Josh 2012-08-02 17:11:01