2012-02-18 31 views
0

我正在使用连接从两个表中查询数据。对于每个a_id,我需要获取关联的image_id,过滤这些image_id结果,以便只保留第一个结果,最后将结果输出到li中。我认为我的查询很好,但是我在包装我的头部时遇到了一些麻烦,要如何为每个a_id获取image_id并将其输出到我的li。此代码返回一些结果,但它们不是我正在寻找的。带连接表的2D数组

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

嗯,我已经修改了代码位和它的工作,但由于某种原因,我没有图像的URL或链接URL的第一图像后获得一个额外的缩略图。我认为这可能与我的方法检查重复的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>'; 
?> 
+0

你为什么要处理各行的结果里面的取环?完整的数据库结果将不可用,所以无论这种过滤应该无法正常工作。 – 2012-02-18 02:48:16

+0

我应该在哪里移动它?更重要的是它是正确的? – 2012-02-18 03:00:39

+0

将它移到while()循环之外。至于正确,不知道。我无法告诉你想要完成什么。就目前而言,您从数据库中获取一行数据,然后遍历您迄今为止获取的所有内容。然后你再取一行,再遍历每一个字母,等等。你是一个[Schlemiel](http://en.wikipedia.org/wiki/Schlemiel_the_Painter%27s_algorithm) – 2012-02-18 03:07:18

回答

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