I’m using join to query data from two tables. For each a_id I need to get the associated image_id, filter those image_id results so only the first one remains, and finally output the results into an li. I think my query is good but I’m having a little trouble wrapping my head around how to get the image_id for each a_id and to output that to my li. This code returns some results but they are not what I’m looking for.
<?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>';
?>
Well, I’ve modified the code a bit and it’s working but for some reason I am getting an extra thumbnail after the first image with no image url or link url. I think it may have something to do with my method for checking for duplicate 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>';
?>
Here is the solution that I ended up using. What was really making this difficult for me is that I wasn’t grasping two concepts.
The first was JOIN. I didn’t understand how it was merging the two tables. After reading some more about it I now know that ON is more appropriate when joining columns of different names but what I really wanted was to JOIN both tables by their a_id columns. Even though I’ve left the ON and have made both column names the same I should look into using the keyword USING as it is specifically used for columns of the same name.
The second concept I was having difficulty understanding was how to get all the info I wanted from the new joined table. I didn’t know that using mysql_fetch_array with a while loop would go through each row and get all the data from every column. Once I understood this though it was easy enough to go through each row and get the image_id and a_id.
My final code: