0

我对 PHP 中的相册有疑问。画廊有以下结构:

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

1 回答 1

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 项的标题/描述。

于 2012-05-20T17:38:43.637 回答