2

大家好,我的第一篇文章:D

问题:

我正在尝试制作模板库,而不是幻灯片,我可以轻松地在多个站点上重复使用它。
主要用于快速作品集网站,所有者不知道如何更新代码以添加图片。

它需要从选定目录中读取所有图像文件。(jpg, gif, png, bmp) 它需要能够在不更改任何代码的情况下更新内容。(从文件夹动态加载)它需要将img标签写入查看的页面。(使用 JavaScript 进行自定义/css?)

从 php/JavaScript 输出的一组 img 标签需要是缩略图,单击时将链接到完整的 def 图片,这可能在最初制作链接时使用 JavaScript 处理。

进步:

我找到了一个 php 脚本,该脚本将从文件夹中读取文件并将 URL 保存到数组中以便在 JavaScript 中使用。但是,用于显示图片的代码是作为单个块幻灯片放映完成的,因为我需要它单独发布所有图像,而不仅仅是替换同一图像的 src。

例子:

root/index.htm - pastebin[.]com/m52568ed5
root/images/getimages.php - pastebin[.]com/f5395a572
root/images/pic01.png
root/images/pic03.jpg
root/images/asdfs.gif

那么如何让 JavaScript 循环遍历 galleryarray[curimg] 并写出我的链接?

我走了这么远,卡住了。

function rotateimages(){
 // document.getElementById("slideshow").setAttribute("src", "res/gallery/painting/"+galleryarray[curimg])
 // curimg=(curimg<galleryarray.length-1)? curimg+1 : 0
 for (curimg=1;curimg!=0;curimg++;) {
 document.write("<div><img class='gallery' src='" + galleryarray[curimg] + "' /></div>")
 }
}

在此先感谢,布雷登。


编辑:这是我的沙箱来显示发生了什么

-编辑:删除链接

无论我如何更改每个项目的输出,例如,如果我用一个简单的 echo 替换整个部分,我得到的只是以下内容:

<!DOCTYPE html>
<html>
    <head>
        <title>My Gallery</title>
    </head>

    <body>
        <div id="gallery"></div>
    </body>
</html>

似乎它在尝试运行“foreach()”时卡住了

这是当前的php:

<?php

function getDirTree($dir,$p=true) {
    $d = dir($dir);$x=array();
    while (false !== ($r = $d->read())) {
        if($r!="."&&$r!=".."&&(($p==false&&is_dir($dir.$r))||$p==true)) {
                $x[$r] = (is_dir($dir.$r)?array():(is_file($dir.$r)?true:false));
        }
    }

    foreach ($x as $key => $value) {
        if (is_dir($dir.$key."/")) {
                $x[$key] = getDirTree($dir.$key."/",$p);
        }
    }

    ksort($x);
    return $x;
}

$tree = getDirTree("./res/gallery/");

echo '<div id="gallery">';

foreach($tree as $element => $eval) {
    if (is_array($eval)) {

        foreach($eval as $file => $value) {
                if (strstr($file, "jpg")) {
                        $file = 'res/gallery/'.$element.'/'.$file;
                        echo 'test'; //test//echo '<a href="'.$file.'">test</a>'; //test// <img class="gallery" src="'.$file.'" alt="'.$file.'"/></a>';
                }
        }


    }
}
echo '</div>';

考虑到在我开始之前我从未做过 php,我认为我做得很好。

4

3 回答 3

4

非常简单的自动图库脚本,photos.php:

<?php
function getDirTree($dir,$p=true) {
    $d = dir($dir);$x=array();
    while (false !== ($r = $d->read())) {
        if($r!="."&&$r!=".."&&(($p==false&&is_dir($dir.$r))||$p==true)) {
            $x[$r] = (is_dir($dir.$r)?array():(is_file($dir.$r)?true:false));
        }
    }

    foreach ($x as $key => $value) {
        if (is_dir($dir.$key."/")) {
            $x[$key] = getDirTree($dir.$key."/",$p);
        }
    }

    ksort($x);
    return $x;
}

$tree = getDirTree("./foto/");

echo '<div id="gallery">';
echo '<ul class="linone">';
foreach($tree as $element => $eval) {
    if (is_array($eval)) {
        echo '<li><h4>'.$element.'</h4>';
        echo '<ul class="linone photos">';
        foreach($eval as $file => $value) {
            if (strstr($file, "jpg")) {
                $file = 'foto/'.$element.'/'.$file;
                echo '<li><a href="'.$file.'"><img src="'.$thumb.'" alt="'.$thumb.'"/></a></li>';
            }
        }
        echo '</ul>';
        echo '</li>';
    }
}
echo '</ul>';
echo '</div>';

我还使用 lightbox jQuery 插件使这个画廊看起来很舒服。

并且管理此页面的照片也非常非常简单 - 您只需将 .jpg 文件上传到您的照片目录(本例中为“/foto/”)。

索引.php:

<!DOCTYPE html>
<html>
    <head>
        <title>My Gallery</title>
    </head>

    <body>
        <?php require_once('photos.php') ?>
    </body>
</html>

该文件将包含 photos.php 文件并运行它,photos.php 脚本的输出将出现在标签之间。

于 2009-12-13T11:04:09.953 回答
0

您可以使用 PHP 写出 img 标签。

此外,您的 for 循环将无限运行,因为它的结束条件始终为真。

编辑:查看您的 pastebin,您似乎误解了 PHP 的工作原理。<script src="res/getimages.php"></script>将不起作用,因为脚本标签是在客户端读取的。PHP 在服务器端运行。要运行res/getimages.php,您必须执行以下操作:

<?php //This is a PHP opening tag; anything after this is PHP code
include('res/getimage.php'); //Imports the PHP file into index.php
?> <!--this is the closing PHP tag - anything after this is HTML -->

我强烈建议您阅读快速教程,例如W3Schools PHP 教程,它将真正帮助您完成此任务。与JavaScript相同。你可以不断地询问这一切是如何运作的,但你永远不会真正理解它,也无法弄清楚如何将所有部分组合在一起。给它几个小时!

于 2009-12-13T10:54:47.010 回答
0

感谢所有提供帮助的人,非常感谢 Sergey Kunetsov 的画廊脚本,经过轻微修改后完美运行。

对于任何想要它的人来说,这是最终的工作 php。

http://pastebin.com/f442f31f3

显示的文件夹是 $path。

享受,再次感谢。

于 2009-12-14T19:00:30.673 回答