【问题标题】:Show three images from each user显示每个用户的三张图片
【发布时间】:2012-12-08 10:33:26
【问题描述】:

我有一个图片表,其中用户的图片与他们的 ID 和图片物理链接一起保存。

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

现在,我想在 jquery 图片库块中显示最多 3 张图片,其中一个块应显示来自同一用户的所有 3 张图片,如果用户的图片少于 3 张,则不应显示图像文本。

我尝试使用 mysql 查询进行分组,但没有得到想要的结果。我必须使用两个循环吗?

--为 fthiella 编辑-- 这是代码

$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>

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

【问题讨论】:

  • 显示您尝试过的代码。然后告诉我们它正在做什么以及它与你想要它做什么有什么不同。
  • 我没有看到任何代码,但您应该编辑原始帖子并将其粘贴到那里。
  • 对不起,安迪,我刚刚用代码编辑了我的主要问题,因为我是新来的。
  • 我没有任何答案,但我只是第一个说“给我们看代码”。
  • @travis 从first 开始?但首先是像页码这样的东西吗?所以如果first=0,显示第一个用户的image1,image2,image3(或空图像),first=1显示第二个用户的image1,image2,image3?

标签: php mysql loops picturegallery


【解决方案1】:

我不知道是否有更好的解决方案,但我认为你可以使用这个:

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

这将为每个用户选择三个图像。如果用户的图片少于三张,则显示空值,如果多张,则在所有图片中选择三张。

另一种方法是使用变量来显示三张图像,每张图像都在不同的行中:

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

编辑:每次为每个用户显示三张图片我会使用我的第一个查询,我会从这样的一些代码开始:

<?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)

【讨论】:

  • 这是否总是从单行中选择 .picture 值?我担心 p1.picture 可能会选择与 p3.picture 不同的分组行(导致它们相同)。
  • 不错,但是说它是 100,是否有替代连接的替代方法?
  • @dagon 我编辑并添加了一个替代方法,它使用变量,因此它不是标准 SQL,但仅适用于 MySql
  • @ebyrob 抱歉,我不明白... p1.userid、p2.userid 和 p3.userid 总是相同的,图片总是不同的,所以它们总是在不同的行上。 . 我觉得应该没问题
  • +1 用于那个非常好的解决方案......我只是在摆弄同样的东西:o)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
相关资源
最近更新 更多