画廊用拇指,太多的foreach循环(?)(PHP)

时间:2012-05-20 17:07:46

标签: php loops foreach gallery thumbnails

我在PHP中使用agallery时出现问题。该画廊具有以下结构:

            <div class="imageElement">
                <h3>Item 1 Title</h3>
                <p>Item 1 Description</p>
                <a href="open/1.jpg" title="open image" class="open"></a>
                <img src="big/1-gallery.jpg" class="full" />
                <img src="thumb/1-mini.jpg" class="thumbnail" />
            </div>
      and so on

我有三个文件夹: 大 - 有正常的图像, 拇指 - 用拇指, 打开 - 大图像。

我创建了一个foreach循环:

<?php foreach(glob("images/folder/images/".$id."/big/*.jpg") as $filename) : ?>

<div class="imageElement">
<h3>Item 1 Title</h3>
<p>Item 1 Description</p>
<a href="path-to-large-image" title="open image" class="open"></a>
<? echo '<img src="'.$filename.'" class="full">'?>

<?php foreach(glob("images/folder/images/".$id."/thumb/*.jpg") as $filename2) : ?    >
<? echo '<img src="'.$filename2.'" class="thumbnail">';break;?>
 <?php endforeach ?>
</div>
<?php endforeach ?>

不幸的是,我只获得了第一张 - 每张图片都有相同的拇指。在这种情况下我应该如何使用foreach循环?我应该如何使用下一个循环从打开的文件夹中获取图像?

感谢您的回答。

编辑:

我的html输出显示:

<div class="imageElement">
<h3>title</h3>
<p>text</p>
<a href="/images/folder/4/open/1.jpg" title="open image" class="open"></a>
<a href="/images/folder/4/open/2.jpg" title="open image" class="open"></a>
<a href="/images/folder/4/open/3.jpg" title="open image" class="open"></a>
<a href="/images/folder/4/open/4.jpg" title="open image" class="open"></a>

<img src="images/folder/images/4/big/1.jpg" class="full">

<img src="/images/folder/images/4/thumb/1.jpg" class="thumbnail">
<img src="/images/folder/images/4/thumb/2.jpg" class="thumbnail">
<img src="/images/folder/images/4/thumb/3.jpg" class="thumbnail">
<img src="/images/folder/images/4/thumb/4.jpg" class="thumbnail">
</div>

<div class="imageElement">
<h3>title</h3>
<p>text</p>
<a href="/images/folder/4/open/1.jpg" title="open image" class="open"></a>
<a href="/images/folder/4/open/2.jpg" title="open image" class="open"></a>
<a href="/images/folder/4/open/3.jpg" title="open image" class="open"></a>
<a href="/images/folder/4/open/4.jpg" title="open image" class="open"></a>

<img src="images/folder/images/4/big/2.jpg" class="full">

<img src="/images/folder/images/4/thumb/1.jpg" class="thumbnail">
<img src="/images/folder/images/4/thumb/2.jpg" class="thumbnail">
<img src="/images/folder/images/4/thumb/3.jpg" class="thumbnail">
<img src="/images/folder/images/4/thumb/4.jpg" class="thumbnail">
</div>

等等。

它应该看起来:

<div class="imageElement">
<h3>title</h3>
<p>text</p>
<a href="/images/folder/4/open/1.jpg" title="open image" class="open"></a>
<img src="images/folder/images/4/big/1.jpg" class="full">
<img src="/images/folder/images/4/thumb/1.jpg" class="thumbnail">
</div>

<div class="imageElement">
<h3>title</h3>
<p>text</p>
<a href="/images/folder/4/open/2.jpg" title="open image" class="open"></a>
<img src="images/folder/images/4/big/2.jpg" class="full">
<img src="/images/folder/images/4/thumb/2.jpg" class="thumbnail">
</div>

1 个答案:

答案 0 :(得分:0)

构建代码的方式,混合PHP和HTML,可能会混淆PHP。试试这个:

<?php
foreach(glob("images/folder/images/".$id."/big/*.jpg") as $filename) {

  echo '<div class="imageElement">
<h3>Item 1 Title</h3>
<p>Item 1 Description</p>
<a href="path-to-large-image" title="open image" class="open"></a>
<img src="'.$filename.'" class="full">';

  foreach(glob("images/folder/images/".$id."/thumb/*.jpg") as $filename2) {
    echo '<img src="'.$filename2.'" class="thumbnail">';
  }
  echo '</div>';
}

编辑:

这会产生你想要的东西:

foreach($x as $filename) {
  $chunks=explode("/", $filename);
  $file=$chunks[count($chunks)-1];

  echo '<div class="imageElement">
  <h3>Item 1 Title</h3>
  <p>Item 1 Description</p>
  <a href="./images/folder/images/'.$id.'/open/'.$file.'" title="open image" class="open"></a>
  <img src="./images/folder/images/'.$id.'/big/'.$file.'" class="full">
  <img src="./images/folder/images/'.$id.'/thumb/'.$file.'" class="thumbnail">

  </div>


  ';

}

输出:

<div class="imageElement">
<h3>Item 1 Title</h3>
<p>Item 1 Description</p>
<a href="./images/folder/images/4/open/1.jpg" title="open image" class="open"></a>
<img src="./images/folder/images/4/big/1.jpg" class="full">
<img src="./images/folder/images/4/thumb/1.jpg" class="thumbnail">

</div>


<div class="imageElement">
<h3>Item 1 Title</h3>
<p>Item 1 Description</p>
<a href="./images/folder/images/4/open/2.jpg" title="open image" class="open"></a>
<img src="./images/folder/images/4/big/2.jpg" class="full">
<img src="./images/folder/images/4/thumb/2.jpg" class="thumbnail">

</div>


<div class="imageElement">
<h3>Item 1 Title</h3>
<p>Item 1 Description</p>
<a href="./images/folder/images/4/open/3.jpg" title="open image" class="open"></a>
<img src="./images/folder/images/4/big/3.jpg" class="full">
<img src="./images/folder/images/4/thumb/3.jpg" class="thumbnail">

</div>


<div class="imageElement">
<h3>Item 1 Title</h3>
<p>Item 1 Description</p>
<a href="./images/folder/images/4/open/4.jpg" title="open image" class="open"></a>
<img src="./images/folder/images/4/big/4.jpg" class="full">
<img src="./images/folder/images/4/thumb/4.jpg" class="thumbnail">

</div>

唯一需要解决的问题是第1项标题/说明。