0

わかりましたので、私のSQLクエリは次のとおりです。

$image_results = $wpdb->get_results('SELECT * FROM uploaded_images GROUP BY album_title') ;

私が今やりたいことは、アルバム タイトルをタグで出力し、そのアルバム タイトルを持つアイテムのリストを表示する、異なる *album_title* ごとに異なる結果のセットを用意することです。

これが私がこれまでに持っているものです:

<?php foreach ($image_results as $result => $coll):?>
    <h4><?php echo $coll->album_title;?></h4>
    <?php foreach ( $coll->file_name as $i => $row ) : ?>
         <p><img src="http://example.com/<?php echo  $row->file_name; ?>"/></p>
    <?php endforeach; ?>
<?php endforeach; ?>
4

2 に答える 2

1

質問の更新後、これがあなたが探しているものだと思います。ORDER BYの代わりに使用したことに注意してくださいGROUP BY。グループ化はSUM、 、AVG、またはその他の集計関数が使用されるときに使用されます。これはすべて 1 つのループで処理できます。

$image_results = $wpdb->get_results('SELECT * FROM uploaded_images ORDER BY album_title');
$curTitle = '';
foreach($image_results as $result => $col) {
    if($curTitle !== $col['album_title']) {
        $curTitle = $col['album_title'];
        echo "<h4>$curTitle</h4>";
    }
    echo '<p><img src="' . $col['file_name'] . '" /></p>';
}
于 2012-12-10T21:49:48.860 に答える
0

簡単な方法:

$album_titles = array();
$image_results = $wpdb->get_results('SELECT * FROM uploaded_images GROUP BY album_title') ; 
foreach ($image_results as $result){
   $album_titles[ $result['album_title'] ][] = $result;
}

foreach( $album_titles as $title -> $images ){
 echo "<h4>".$title."</h4>";
   foreach( $images as $image ){
      echo "<br/>".$image['file_name'];
   }
}

すべてを選択し、タイトルの配列を作成し、同じタイトルですべてを出力します。

于 2012-12-10T21:41:45.570 に答える