[英]How to get the pictures from folder one by one and display in the page using PHP
[英]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?
我使用此代碼在我的網頁上添加產品...
<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圖像,01a.jpg縮略圖圖像,02.jpg圖像,02a.jpg縮略圖圖像等)。
提前致謝 :)
聲明$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`)
);
第2步:創建連接文件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。
一旦完成,您的目標將會實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.