使用PHP在HTML页面上检索图像数组 [英] Retrieving an array of images on HTML page using PHP

查看:147
本文介绍了使用PHP在HTML页面上检索图像数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php从目录中检索图像。图像显示在PHP页面(getUser.php)上,但这些图像如何在任何HTML上显示。


getUser.php

I am retrieving images from a directory using php. Images are displayed on PHP page (getUser.php) but how can these images be display on any HTML.
getUser.php

<?php


$dir = 'images';
$file_display = array('jpg','jpeg','png','gif');

if (file_exists ($dir) == false) {
    echo 'Directory \'', $dir, '\' not found!';
}
else{
    $dir_contents = scandir($dir);


    foreach($dir_contents as $file) {
        $file_type = strtolower(end(explode('.', $file)));

        If($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
            echo '<img src="', $dir, '/', $file, '" alt="', $file, '" />';
            /*echo '<td>';
echo '<img src =';
echo $dir."/".$file;
echo  '/>';
echo '</td>'; */

        }       

    }

}


?> 

one.html

我想要这个要在html(div)中显示的图像数组。这是图像标记的源代码,需要从PHP文件中读取。我在ASP.NET中已经完成了这个但是无法在PHP中解决这个问题

one.html
I want this array of images to be displayed in html (div). That is the source of the image tag takes reading from the PHP file. I have done this in ASP.NET but can't figure it out in PHP Is there anything like

<img src="images/vimages/<%=row["image"] %>" ASP.NET

这个PHP。

this PHP.

<div id="pics">
                <img src="img/...jpg" alt=""  />

</div>



上面的div将使用getUser.php文件动态填充。


The above div will be dynamically populating using getUser.php file.

推荐答案

如果我理解你的问题,你正在寻找一些php模板系统。

If I understand your question, you are looking for some template system in php.

首先,php中已有一个。 PHP允许你使用标签混合html和php代码。

Firstly, there is one already in the php. PHP allows you to mix html and php code using tags.

所以你可以创建类似的东西:

So you can create something like:

<?php if ($images): ?>
<?php $counter = 1 ?>
<ul>
<?php foreach ($images as $image): ?>
    <li id="item-<?php echo $counter++ ?>">
    <img src="<?php echo $image['source']?>">
    </li>
<?php endforeach ?>
</ul>
<?php endif?>

虽然将应用程序逻辑和html代码分开是一个好习惯,但纯php并不是最好的工具。这个工作。幸运的是,网上有很多工具,例如
Smarty 拿铁

While it is good practice to separate application logic and html code, pure php is not the best tool for this job. Fortunately, there is plenty of tools on web, for example Smarty or Latte

这篇关于使用PHP在HTML页面上检索图像数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆