【问题标题】:How to get the pictures from a folder one by one and display them on a page with specific text for each image using php?如何从一个文件夹中一张一张地获取图片,并使用php将它们显示在一个页面上,每个图片都有特定的文本?
【发布时间】:2014-07-11 08:48:16
【问题描述】:

我使用此代码在我的网页上添加产品...

<table align="center" border="0" cellpadding="0" cellspacing="0" width="500">
<tr>
<td width="250" >  <a href="img/01.JPG" rel="lightbox" ><img src="img/01a.JPG" border="0"   width="200" height="150"><br><br><br><br></a>   </td>
<td width="250" >
<center>
<span class="style2">Name of the product</span><br>
Description of the product
<br><br>
<b></b><br><br><br><br><br>
</center>
</td>
</tr>
</table>

我不想一遍又一遍地重写这段代码...我想要一个代码,它可以从文件夹中获取产品的图像,并用每个产品的特定文本显示它们。

所有产品的文字都将放在与图片相同编号的文件夹中(图片编号01,文字编号01,图片编号02,文字编号02等)。

如果产品没有文本文件,它将显示(产品的图像)但没有文本。

在图片文件夹中,我有同一产品的图片和缩略图(01.jpg-image、01a.jpg-thumbnail image、02.jpg-image、02a.jpg-thumbnail image 等)。

提前致谢:)

【问题讨论】:

    标签: php image text


    【解决方案1】:

    声明$filepath并用这段代码试试:

    <?
    $string =array();
    $filePath='directorypath/';  
    $dir = opendir($filePath);
    while ($file = readdir($dir)) { 
       if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) { 
       $string[] = $file;
       }
    }
    while (sizeof($string) != 0){
      $img = array_pop($string);
      echo "<img src='$filePath$img'  width='100px'/>";
    }
    ?>
    

    还可以查看 官方 PHP 站点

    http://php.net/manual/en/function.opendir.php

    http://php.net/manual/en/function.scandir.php

    http://php.net/manual/en/refs.utilspec.image.php

    要使其成为动态,请遵循以下列出的步骤

    第 1 步:创建一个名为 images 的表

    create table `images` (
    `id` int(4)auto_increment,
    `imagename` varchar(30),
    `description` varchar(100),
    PRIMARY KEY (`id`)
    );
    

    第二步:创建连接文件conf.php

    <?php
    $con=mysqli_connect("localhost","username","password","databasename");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    ?>
    

    第 3 步:创建image.php 文件

    <?php
    include ('config.php');
    $sql="select image, description from images";
    $sql_res=mysqli_query($con,$sql);
    while($row=mysqli_fetch_array($sql_res,MYSQLI_ASSOC))
    {
    ?>
    <img src="folderpath/<?php echo $row["images"];?> />
    Description 
    <?php
    echo $row['description'];
    ?>
    

    完成后,将连接文件 (config.php)、images.php 和 images 文件夹放在同一目录中,并在为目录路径、数据库名称、用户名、密码提供适当的值后运行 images.php等

    一旦完成,您的目标就会实现。

    【讨论】:

    • 但是文字......产品的描述。
    • 如果是这样连接数据库是最好的。
    • 我不知道如何使用数据库...我在学校学过 html,在家里学过一些 php,但不是很了解...我刚开始...谢谢PHP 手册 :)
    • 顺便...你能给我一个完整的php教程吗(所有的php)...在家里我从我哥哥2005年给我的一本旧书中学到的史蒂文·霍尔兹纳的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2013-10-15
    • 2015-04-22
    • 2013-07-27
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    相关资源
    最近更新 更多