带有JOINED表的2D数组

时间:2012-02-18 02:42:27

标签: php mysql greatest-n-per-group

我正在使用join来查询两个表中的数据。对于每个a_id,我需要获取关联的image_id,过滤那些image_id结果,只保留第一个结果,最后将结果输出到li中。我认为我的查询很好,但是我在如何获取每个a_id的image_id并将其输出到我的li时遇到了一些麻烦。这段代码返回了一些结果,但它们并不是我想要的。

<?php
echo '<ul>';
$result = mysql_query ("SELECT * from artists left join images on artists.a_id = images.image_id where artists.display_works = '1' and artists.active = '1' order by artists.project_year desc, artists.fullname desc");

while ($row = mysql_fetch_array($result)){
    $data['a_id']['image_id']=$row->a_id;

    foreach($data as $id=>$images) {
            $totalimages=1;
            $addstyle = "";
            $art_id = $data['a_id'];
            $img_id = $data['image_id'];

            foreach($images as $val){

                    if($totalimages > 1){ $addstyle = 'style="display:none;"'; }
                    else {
                    $myimagename = "http://artists/$art_id/images/$img_id" .  "_large.jpg";
                    list($width, $height, $type, $attr) = getimagesize("$myimagename");
                    $myimagename = "http://artists/resize.php/$art_id/images/$img_id" . "_large.jpg?resize(157x2000)";

                    if($row["layout"] == "vert"){$pl = "_vertical";}else if($row["layout"] == "website"){$pl = "-s";}else if($row["layout"] == "video"){$pl = "_video";}else{$pl = "_horizontal";}
                    echo "<li class='thumbnail_container' $addstyle> <a class='thumbnail' href=\"../works$pl.php?a_id=" . $row["a_id"] . "\"><span><img src=\"$myimagename\" /></span>\n</a></li>\n";
                    }

                    $totalimages++;
        }
    }
}
echo '</ul>';
?>

好吧,我已经修改了一些代码并且它正在工作但是出于某种原因我在第一张图片之后得到了一个额外的缩略图,没有图片网址或链接网址。我认为这可能与我检查重复a_ids的方法有关:

<?php
echo '<ul>';
$result = mysql_query ("SELECT * from artists left join images on artists.a_id = images.a_id where artists.display_works = '1' and artists.active = '1' order by artists.project_year desc, artists.fullname desc, images.position asc");

while ($row = mysql_fetch_array($result)){
    $check = $row['a_id'];
    if (in_array($check, $a_ids)) {end;}
    else { 
    $a_id=$row['a_id'];
    $a_ids[] = $a_id;
    $image_id=$row['image_id'];

    $myimagename = "http://artists/$a_id/images/$image_id" .  "_large.jpg";
    list($width, $height, $type, $attr) = getimagesize("$myimagename");
    $myimagename = "http://artists/resize.php/$a_id/images/$image_id" . "_large.jpg?resize(157x2000)";

    if($row["layout"] == "vert"){$pl = "_vertical";}else if($row["layout"] == "website"){$pl = "-s";}else   if($row["layout"] == "video"){$pl = "_video";}else{$pl = "_horizontal";}
        echo "<li class='thumbnail_container' $addstyle> <a class='thumbnail' href=\"../works$pl.php?a_id=" . $row["a_id"] . "\"><span><img src=\"$myimagename\" /></span>\n</a></li>\n";
    }
}
echo '</ul>';
?>

1 个答案:

答案 0 :(得分:1)

这是我最终使用的解决方案。真正让我感到困难的是,我并没有抓住两个概念。

第一个是加入。我不明白它是如何合并这两张桌子的。在阅读了更多关于它的内容之后,我现在知道在加入不同名称的列时ON更合适,但我真正想要的是通过a_id列加入两个表。即使我已经离开ON并且两个列名都相同,我应该使用关键字USING来查看它,因为它专门用于同名的列。

我理解困难的第二个概念是如何从新的连接表中获取我想要的所有信息。我不知道使用带有while循环的mysql_fetch_array会遍历每一行并从每一列获取所有数据。一旦我理解了这一点,虽然很容易通过每一行并获得image_id和a_id。

我的最终代码:

<?php
echo '<ul>';
$result = mysql_query ("SELECT * from artists left join images on artists.a_id = images.a_id where artists.display_works = '1' and artists.active = '1' order by artists.project_year desc, artists.fullname desc, images.position asc");

while ($row = mysql_fetch_array($result)){
    $check = $row['a_id'];
    if (!in_array($check, $a_ids) && $check !='') {
    $a_id = $row['a_id'];
    $a_ids[] = $a_id;
    $image_id = $row['image_id'];

    $myimagename = "http://artists/$a_id/images/$image_id" .  "_large.jpg";
    list($width, $height, $type, $attr) = getimagesize("$myimagename");
    $myimagename = "http://artists/resize.php/$a_id/images/$image_id" . "_large.jpg?resize(157x2000)";

    if($row["layout"] == "vert"){$pl = "_vertical";}else if($row["layout"] == "website"){$pl = "-s";}else   if($row["layout"] == "video"){$pl = "_video";}else{$pl = "_horizontal";}
        echo "<li class='thumbnail_container' $addstyle> <a class='thumbnail' href=\"../works$pl.php?a_id=" . $row["a_id"] . "\"><span><img src=\"$myimagename\" /></span>\n</a></li>\n";
    }
}
echo '</ul>';
?>