0

私はいくつかのQ&Aを読んでいますが、それでもアルゴリズムを構築することができていないので、その方法についていくつかのヘルプ/提案を求めています。

したがって、MySQLテーブルがあり、mysqlテーブルの行ごとにテーブルに表示したいと思います。テーブルに4列のみを含める必要があります(つまり、行数はデータベース内のデータに応じて変化します)。したがって、htmlテーブル(行1、列1)には、dbテーブル行1からのデータ、html(行1、列2)、dbテーブル行2からのデータ...(行2、列1)、dbテーブルからのデータが含まれます。行5など。

したがって、テーブルは次のようになります。

[db行#1からのデータ] [db行#2からのデータ] [db行#3からのデータ] [db行#4からのデータ]

[db行#5からのデータ] [db行#6からのデータ] [db行#7からのデータ] [db行#8からのデータ]

等々...

これは私がdbからデータを取得しなければならないものです:

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

それで、何かアイデアはありますか?

4

1 に答える 1

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;
于 2012-08-02T17:03:37.463 に答える