【问题标题】:Loop through images not working循环浏览图像不起作用
【发布时间】:2014-01-04 16:45:08
【问题描述】:

使用此代码,ID 始终为 1。我希望它在每次循环消息时添加 +1。

如ex1、ex2、ex3等。

我不知道出了什么问题。有人可以帮忙吗?

foreach ($message['attachment'] as $attachment)
{
    if ($attachment['is_image'])
    {
        if ($attachment['thumbnail']['has_thumb'])
            echo '<a href="', $attachment['href'], ';image" id="link_', $attachment['id'], '" onclick="', $attachment['thumbnail']['javascript'], '"><img src="', $attachment['thumbnail']['href'], '" alt="" id="thumb_', $attachment['id'], '" border="0" class="opplastetbilde"/></a><br />';

        else
            $id = 1;

        if ($id < 10) {
            echo '<span class="zoom" id="ex' . $id . '"><img src="' . $attachment['href'] . ';image" alt="" width="' . $attachment['width'] . '" height="' . $attachment['height'] . '" border="0" class="opplastetbilde"/></span><br />';
            $id++;
        }
    }

    echo '<a href="' . $attachment['href'] . '"><img src="' . $settings['images_url'] . '/icons/clip.gif" align="middle" alt="*" border="0" />&nbsp;' . $attachment['name'] . '</a> (', $attachment['size'], ($attachment['is_image'] ? ', ' . $attachment['real_width'] . 'x' . $attachment['real_height'] . ' - ' . $txt['attach_viewed'] : ' - ' . $txt['attach_downloaded']) . ' ' . $attachment['downloads'] . ' ' . $txt['attach_times'] . '.)<br />';
}

【问题讨论】:

  • 请改进你的编码风格!
  • 是的,很抱歉。在我的编辑器中不是这样的。
  • 您正在用逗号连接字符串 (,)。我认为这在 PHP 中不起作用,是吗?

标签: php html list loops foreach


【解决方案1】:

在 foreach 之前移动它,否则它总是重新初始化$id

$id = 1;
foreach ($message['attachment'] as $attachment)
    {

【讨论】:

    【解决方案2】:

    现在,每次循环遍历数组时,您都在设置 $id = 1

    foreach ($message['attachment'] as $attachment) {
        // ...
        $id = 1;
        // ...
    }
    

    为了正确增加它,你需要把它放在循环之前

    $id = 1;
    foreach ($message['attachment'] as $attachment) {
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 2014-04-11
      • 1970-01-01
      相关资源
      最近更新 更多