来自foreach循环的彼此相邻的图像

时间:2015-07-16 10:20:37

标签: php html css

当我们从PHP中的while循环中选择图像时,我想将图像放在一起。

所以,目前它看起来像http://prntscr.com/7tb42j

我希望将图像放在一起。

我的foreach循环如下所示:

<div id='profile-album'>
<?php 
    $get_fotos = "SELECT * FROM fotos_profile WHERE username=:username LIMIT 4";
    $stmt = $db->prepare($get_fotos);
    $stmt->bindParam(':username', $username);
    $stmt->execute();

    foreach($stmt as $row)
    {
        $pic = $row['pic'];
        $title_foto = $row['title'];
?>
    <div id='album-foto-1'><img src="userdata/profile_fotos/<?php echo $pic; ?>" height='100px' width='206px' style='padding-right: 6px'/></div>

   <?php } ?>

2 个答案:

答案 0 :(得分:2)

您只需要为包含图片的每个div添加display:inline-block

<div id='profile-album'>
<?php 
    $get_fotos = "SELECT * FROM fotos_profile WHERE username=:username LIMIT 4";
    $stmt = $db->prepare($get_fotos);
    $stmt->bindParam(':username', $username);
    $stmt->execute();

    foreach($stmt as $row)
    {
        $pic = $row['pic'];
        $title_foto = $row['title'];
?>
    <div id='album-foto-1' style="display:inline-block"><img src="userdata/profile_fotos/<?php echo $pic; ?>" height='100px' width='206px' style='padding-right: 6px'/></div>

   <?php } ?>

答案 1 :(得分:0)

确保在“album-foto”中设置了css(请参阅下面我为什么使用album-foto而不是album-foto-1):

.album-foto {
    display:inline; // or inline-block depending how you want to be displayed
}

此外,如果你要显示多个图像,你应该使用一个类而不是图像的ID,因为相同ID的复制效果不佳:

  foreach($stmt as $row)
    {
        $id = $row["id"]; // or whatever your ID field is
        $pic = $row['pic'];
        $title_foto = $row['title'];
?>
    <div class='album-foto' id='album-foto-<? echo $id; ?>'><img src="userdata/profile_fotos/<?php echo $pic; ?>" height='100px' width='206px' style='padding-right: 6px'/></div>

   <?php } ?>