我有一个显示列表中个人资料的个人资料。如下图所示。
用户表
rescue
图片表
id | email | full_name | job_title | bio | profile_photo
代码
image_id | id | artist_img
问题区域
<?php
$db = dbconnect();
$stmt = $db->prepare('SELECT
users.email,
users.full_name,
users.job_title,
users.bio,
users.profile_photo,
images.id,
images.artist_img
FROM users
INNER JOIN images ON users.id=images.id GROUP BY images.id');
$stmt->execute();
$result = $stmt->get_result();
while (($row = mysqli_fetch_assoc($result)) != false) {
$id = $row['id'];
$full_name = $row['full_name'];
$email = $row['email'];
$job_title = $row['job_title'];
$bio = $row['bio'];
$ProfilePhoto = $row['profile_photo'];
$artist_img = $row['artist_img'];
if (isset($ProfilePhoto) && ! empty($ProfilePhoto)) {
$image = "$ProfilePhoto";
} else {
$image = "avatar.jpg";
}
echo "<div class='container team-wrap'>
<div class='row'>
<div class='col-md-6'>
<img class='img-responsive' src='artist/$image'>
</div>
<div class=\"col-md-6\">
<strong>$full_name<br>$job_title</strong>
<br>
<p>$bio</p>
<a href='mailto:$email' class='btn btn-info'>Contact Me</a>
</div>
</div>
</div>
<div class=\"container space team-wrap\">
<div class=\"row\">
<div class=\"col-lg-12\">
<div id=\"gallery-slider\" class=\"slider responsive\">
<div>";
echo"
<img src=\"gallery/$artist_img\" alt=\"\"></a>";
echo "</div>
</div>
<hr>
</div>
</div>
</div>";
}
?>
我遇到的问题是,如果用户有5张图片,它会重复每张图片的个人资料,每个图片会添加5张个人资料。
并且根本不显示其他用户个人资料。显示它是如何显示是看图像,例如它在配置文件1下有4个图像,它显示有配置文件图片..好吧,它为每个图像重新编写所有信息我想要与用户显示具有相同ID的图片像下面的滑块一样..
并且它也拒绝显示其他用户的其他个人资料。
答案 0 :(得分:3)
这是因为您正在以错误的方式对数据进行分组。
正如您的问题所描述的那样,用户ID 有多行,并且您尝试使用GROUP BY
在一行中显示这些多个结果。因此,数据库无法对此类数据进行分组,因为它不知道数据与多个结果之间的关系。
但是,您可以在此处使用JOIN
,因为这是您要执行的操作。使用以下查询
SELECT * FROM `users` LEFT JOIN `images` ON images.id = users.id
然后你应该像这样处理结果:
$users = [];
foreach($results as $row){
if(isset($users[$row->userId]))
$users[$row->userId][] = $row->imageUrl;
else
$users[$row->userId] = [$row->imageUrl];
}
$users
是你想要的。它应该做的工作。
答案 1 :(得分:2)
您需要使用下面的数组示例。你现在调用它的方式就好像它只是一个变量所以它只能拉1。
$artist_img1=array();
$artist_img1 = $row['images'];
$artist_img2=row2['id'];
echo "<img src=\"gallery/".$artist_img2."\" alt=\"\"></a>";
答案 2 :(得分:0)
试试这个。
SELECT t.id,t.email,t.fullname,t2.image_id,t2.picture_name FROM table2 t LEFT JOIN table1 ON t2.id=t.id
答案 3 :(得分:0)
您的数据库条目必须如下表所示。
表1 - 用户
------------------------------------------
id | email | full_name
------------------------------------------
1 | test1@gmail.com | test1
2 | test2@gmail.com | test2
3 | test3@gmail.com | test3
表2 - 图片
------------------------------------------
image id | id | picture_image
------------------------------------------
1 | 1 | image1.jpg
2 | 1 | image2.jpg
3 | 1 | image3.jpg
4 | 1 | image4.jpg
5 | 1 | image5.jpg
6 | 1 | image6.jpg
7 | 1 | image7.jpg
8 | 1 | image1.jpg
9 | 1 | image2.jpg
10 | 1 | image3.jpg
11 | 1 | image4.jpg
12 | 1 | image5.jpg
使用下面的SQL Query通过UserID获取图像
SELECT * FROM users
INNER JOIN images
ON (users.id = images.id)
WHERE (images.id = '1')
以上查询将为您提供个人资料ID 1的结果。
答案 4 :(得分:0)
对于thre查询,您可以使用:
SELECT * FROM `users` LEFT JOIN `images` ON images.id = users.id
您可以逐个迭代用户的所有图片,以区分其他人的个人资料图片(第1张图片)。
$result = $stmt->get_result();
$users = array();
foreach($results as $row){
if (in_array($row['id'], $users)) {
echo "Print profil picture";
} else {
array_push($users, $row['id']);
echo "Print the other pictures";
}
}
答案 5 :(得分:0)
更改两行,您将获得预期的结果
准备(“SELECT users.email, users.full_name, users.job_title, users.bio, users.profile_photo, images.id, GROUP_CONCAT(images.artist_img SEPARATOR'|')as artist_img 来自用户 INNER JOIN图像ON users.id = images.id GROUP BY images.id'); $ stmt-&GT;执行(); $ result = $ stmt-&gt; get_result(); while(($ row = mysqli_fetch_assoc($ result))!= false){ $ id = $ row ['id']; $ full_name = $ row ['full_name']; $ email = $ row ['email']; $ job_title = $ row ['job_title']; $ bio = $ row ['bio']; $ ProfilePhoto = $ row ['profile_photo']; //作为数组分割 $ artist_imgs = explode(“|”,$ row ['artist_img']); if(isset($ ProfilePhoto)&amp;&amp;!empty($ ProfilePhoto)){ $ image =“$ ProfilePhoto”; } else { $ image =“avatar.jpg”; } 回声“ 的 $ FULL_NAME$生物
联络我 “; foreach($ artist_imgs as $ artist_img){ 回声””; } 回声“ “; } ?&GT;答案 6 :(得分:-1)
尝试将此作为您的SQL语句 -
SELECT * FROM
users
INNER JOIN images on users.id = images.id
INNER JOIN (SELECT MAX(image_id) as maxid, id from images GROUP BY id) as g
ON images.image_id = g.maxid
这应该会给你想要的结果。请注意,它将最新的图片作为个人资料图片。如果要使用第一个插入的图像,请使用MIN函数而不是MAX。
即使这样有效,我仍然会遵循@ parantap-parashar的答案,因为它更优雅。
答案 7 :(得分:-1)
假设您从之前的回复中获得了SQL查询中的正确结果集:
SELECT * FROM `users` LEFT JOIN `images` ON images.id = users.id
即。所有用户的表格,根据他们拥有的艺术家图像数量为每个用户复制一行,如果他们没有图像,则为空白。
我认为你缺少的是你的PHP。 您需要识别profile_ID中的更改以抽出您的个人资料,然后在内部循环中 (如果有)。
为了能够这样做,您需要先存储所有艺术家图像。
因此,您设置了第一个配置文件ID,存储图像,然后当您到达下一个配置文件ID时,您输出所有内容,然后再对下一个配置文件进行相同操作等。
请注意,第一次传递时会跳过顶部if
语句。另请注意,捕获配置文件详细信息的变量会被覆盖每个重复行(对于测试数据中的第一个配置文件听起来像12),但每次这不是问题时数据是相同的。
退出主循环时,需要记住输出最后一组数据。
$result = $stmt->get_result();
$profile=$result[0]['id'] // initialise
$imgs=array(); // inititalise
foreach($result AS $row)
{
if($profile!=$row['id'])
{
$profile= $row['id'];
echo "<div class='container team-wrap'>
<div class='row'>
<div class='col-md-6'>
<img class='img-responsive' src='$image'>
</div>
<div class=\"col-md-6\">
<strong>$full_name<br>$job_title</strong>
<br>
<p>$bio</p>
<a href='mailto:$email' class='btn btn-info'>Contact Me</a>
</div>
</div>
</div>";
echo "<div class=\"container space team-wrap\">
<div class=\"row\">
<div class=\"col-lg-12\">
<div id=\"gallery-slider\" class=\"slider responsive\">
<div>";
foreach($imgs AS $img)
{
echo "<a target=\"_blank\" href=\"$img\">
<img src=\"$img\" alt=\"\"></a>";
}
$imgs=array(); // Empty out image array
echo " </div>
</div>
</div>
</div>
<hr>
</div>";
}
$full_name = $row['full_name'];
$email = $row['email'];
$job_title = $row['job_title'];
$bio = $row['bio'];
$ProfilePhoto = $row['profile_photo'];
if (isset($ProfilePhoto) && ! empty($ProfilePhoto)) {
$image = "$ProfilePhoto";
} else {
$image = "avatar.jpg";
}
if($row['artist_img']!==NULL)
{
$imgs[]= $row['artist_img'];
}
}
/* Catch last one */
$full_name = $row['full_name'];
$email = $row['email'];
$job_title = $row['job_title'];
$bio = $row['bio'];
$ProfilePhoto = $row['profile_photo'];
if (isset($ProfilePhoto) && ! empty($ProfilePhoto)) {
$image = "$ProfilePhoto";
} else {
$image = "avatar.jpg";
}
echo "<div class='container team-wrap'>
<div class='row'>
<div class='col-md-6'>
<img class='img-responsive' src='$image'>
</div>
<div class=\"col-md-6\">
<strong>$full_name<br>$job_title</strong>
<br>
<p>$bio</p>
<a href='mailto:$email' class='btn btn-info'>Contact Me</a>
</div>
</div>
</div>";
echo "<div class=\"container space team-wrap\">
<div class=\"row\">
<div class=\"col-lg-12\">
<div id=\"gallery-slider\" class=\"slider responsive\">
<div>";
foreach($imgs AS $img)
{
echo "<a target=\"_blank\" href=\"$img\">
<img src=\"$img\" alt=\"\">
</a>";
}
echo " </div>
</div>
</div>
</div>
<hr>
</div>";