显示每个用户的三张图片 [英] Show three images from each user

查看:75
本文介绍了显示每个用户的三张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图片表,其中用户图片通过其ID和图片物理链接进行保存.

I have a pictures table where users pictures are saving with their ID and pic physical link.

userID  |  picture
1       |  picnameLink
1       |  picnameLink
2       |  picnameLink
1       |  picnameLink
2       |  picnameLink
3       |  picnameLink

现在,我想在一个jquery图片库块中最多显示3张图片,其中一个块应显示来自同一位用户的全部3张图片,如果用户的图片少于3张,则该图片不应显示图片文本.

Now, I want to show maximum 3 pictures in a jquery picture gallery block where one block should show all 3 pictures from one same user and if a user have less than 3 pictures, it should show no image text.

我尝试通过mysql查询对组进行处理,但是没有得到想要的结果.我必须使用两个循环吗?

I have tried to do with group by mysql query but I am not getting desired result. Do I have to use two loops?

-编辑fthiella--这是代码

--Edit for fthiella-- Here is code

$query = "SELECT * FROM pictures GROUP BY userID";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
    $image_array[] = $row['picLink'];
    $id_array[] = $row['pic_id'];
}
$num_images_to_display = 3; /* MODIFY TO REFLECT NUMBER OF IMAGES TO SHOW PER SCREEN */
$num_images = count($image_array);
$image_path = "../images/"; /* MODIFY TO REFLECT THE PATH TO YOUR IMAGES */
$y = $num_images_to_display;
if(!isset($_GET['first'])){
        $first = 0;
    }else{
        $first = (int) $_GET['first'];
}
$x = $num_images - 1;
$z = $x - $y;
if($first>$z) {
    $first = $z;
}
$last = $first + $num_images_to_display;

这是HTML区域:

<div style="position:absolute; top:50px; left:100px; width:800px; text-align: center;">
    <?PHP
    $i = $first;
    while($i<$last) { $showme = $image_path . $image_array[$i]; ?>

<?php if($image_array[$i]!="") { ?><img src="<?PHP echo $showme; ?>" width="176px" height="197px"><?php } else { ?><img src="../image/no_image.jpg" width="176px" height="197px"><?PHP } ?>

    $prev = $first-1;
    $next = $first +1;
    if($prev<0){ $prev = 0; }
    ?>
</div>

此查询的结果按组显示图片,但是我希望每个用户最多三张图片,如果用户少于三张图片,则没有图片显示.

Result of this query shows pictures in groups but I want maximum three pictures of each user where no image shows if a user has less than three images.

推荐答案

我不知道是否有更好的解决方案,但我认为您可以使用此方法:

I don't know if there's a better solution, but i think you could use this:

SELECT p1.userID, p1.picture as pic1, p2.picture as pic2, p3.picture as pic3
FROM
  pictures p1 left join pictures p2
  on p1.userID=p2.userID and p1.picture<>p2.picture
  left join pictures p3
  on p1.userID=p3.userID and p1.picture<>p3.picture and p2.picture<>p3.picture
GROUP BY p1.userID

这将为每个用户选择三张图像.如果用户的图像少于三个,它将显示空值;如果用户具有三个以上,则将在所有图像之间选择三个.

This will select three images for each user. If a user has less than three images, it will show nulls, if it has more, it chooses three between all of them.

该查询使用变量:该查询使用变量:

An alternative, that show three images each one in a different row, is this query that makes use of variables:

SELECT userid, picture
FROM (
  SELECT
    userid,
    picture,
    case when @prec_id=userid then @row:=@row+1 else @row:=1 end as row,
    @prec_id:=userid
  FROM 
    `pictures`,
    (SELECT @prec_id:=0, @row:=0) s
  ORDER BY userid) s
WHERE row<=3

编辑:一次为每个用户显示三张图片,我将使用以下代码开始:

to show three images for each user at a time I would use my first query, and I would start with some code like this:

<?php
$mysqli = new mysqli("localhost", "username", "password", "test");

$image_path = "../images/";
$no_image = "../image/no_image.jpg";

if(!isset($_GET['first'])){
  $first = 0;
} else {
  $first = (int) $_GET['first'];
}

if ($stmt = $mysqli->prepare("SELECT p1.userID, p1.picture as pic1, p2.picture as pic2, p3.picture as pic3
FROM
  pictures p1 left join pictures p2
  on p1.userID=p2.userID and p1.picture<>p2.picture
  left join pictures p3
  on p1.userID=p3.userID and p1.picture<>p3.picture and p2.picture<>p3.picture
GROUP BY p1.userID
LIMIT ?,1")) {

    $stmt->bind_param("i", $first); 
    $stmt->execute();
    $stmt->bind_result($user, $pic1, $pic2, $pic3);
    $stmt->fetch();
    $stmt->close();
}
$mysqli->close();
?>
<div style="position:absolute; top:50px; left:100px; width:800px; text-align: center;">
  <img src="<?PHP echo (isset($pic1) ? $image_path.$pic1 : $no_image); ?>" width="176px" height="197px">
  <img src="<?PHP echo (isset($pic2) ? $image_path.$pic2 : $no_image); ?>" width="176px" height="197px">
  <img src="<?PHP echo (isset($pic3) ? $image_path.$pic3 : $no_image); ?>" width="176px" height="197px">
</div>

(它已经得到改进,但是您可以从它开始.我使用的是mysqli而不是mysql)

(it has be improved, but you could start with it. I'm using mysqli instead of mysql)

这篇关于显示每个用户的三张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆