我有四张桌子:分类广告,州,城市和img。我想显示属于唯一ID(用户)的所有图像。
例如:来自明尼苏达州(明尼苏达州)的用户发布了一则关于销售有5张照片的汽车的广告。
我得到了所有处理此查询的内容,但结果是重复每个图片的用户信息。
<?php
$query = mysql_query("SELECT * FROM classifieds
LEFT JOIN img ON img.classified_id = classifieds.id
LEFT JOIN state ON classifieds.state_id = state.id
LEFT JOIN city ON classifieds.city_id = city.id WHERE id = '".$id."' AND
authorized = '1'
");
while($row = mysql_fetch_array($query, MYSQL_ASSOC))
{
?>
<div align="center"><a href=' <?php echo $row['image_path']; ?>' rel='lightbox'
title='<?php echo $row['title']; ?>' style='text-decoration:none;'> <img
src='<?php echo $row['image_path']; ?>' width='150' border='0' />
</a>
</div>
<div class="show_location" style="width:225px; hight:10px; padding-top:20px;">
<span class='style55'><?php echo $lang['D_STATE']; ?> <span style="color:#06F">
<?php echo $row['statename']; ?></span> </div>
<div style="width:225px; hight:10px; padding-top:20px;"> <?php echo
$lang['D_CITY']; ?><span style="color:#06F"> <?php echo $row['city']; ?></span>
</span> </div>
<?php
}
?>
我在这个网站上找了一些答案,我试图分组,但它只返回第一张图片。
由于
答案 0 :(得分:1)
您的'img'表有相同'classified_id'的多条记录..因此您的查询将为相同的分类ID生成多条记录。你有两个选择
而不是在主查询中连接img表,编写单独的查询以在while循环中获取图像。
使用'group_concat'函数检索图像..但您必须将连接的字符串拆分为图像名称。
试试这个(第一个选项)
<?php
$query = mysql_query("SELECT * FROM classifieds
LEFT JOIN state ON classifieds.state_id = state.id
LEFT JOIN city ON classifieds.city_id = city.id WHERE classifieds.id = '" . $id . "' AND authorized = '1'");
$row = mysql_fetch_array($query, MYSQL_ASSOC);
$get_images_query = "SELECT * FROM img WHERE classified_id = " . $row['id'];
$exec_get_images_query = mysql_query($get_images_query);
while($res_images = mysql_fetch_array($exec_get_images_query))
{
?>
<div align="center"><a href=' <?php echo $res_images['image_path']; ?>' rel='lightbox' title='<?php echo $row['title']; ?>' style='text-decoration:none;'> <img src='<?php echo $res_images['image_path']; ?>' width='150' border='0' /></a>
</div>
<?php
}
?>
<div class="show_location" style="width:225px; hight:10px; padding-top:20px;"><span class='style55'><?php echo $lang['D_STATE']; ?> <span style="color:#06F"> <?php echo $row['statename']; ?></span></div>
<div style="width:225px; hight:10px; padding-top:20px;"><?php echo $lang['D_CITY']; ?><span style="color:#06F"> <?php echo $row['city']; ?></span></div>