简体   繁体   中英

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?

I use this code to add products on my webpage...

<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>

I don't want to rewrite this code over and over again... I want a code that will get the images of the products from a folder and display them with a specific text for each product.

The texts for all product will bee in a folder with the same number as the image(image number 01, text number 01, image number 02, text number 02 etc. ).

If a product doesn't have a text file it will be displayed(the image of the product) but without text.

In the folder of images I have the image and the thumbnail image of the same product(01.jpg-image, 01a.jpg-thumbnail image, 02.jpg-image, 02a.jpg-thumbnail image etc. ).

Thanks in advance :)

Declare the $filepath and try it with this piece of code :

<?
$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'/>";
}
?>

Also have a look at the Official PHP Site

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

To make it Dynamic follow the below listed steps

Step 1 : Create a table named images

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

Step 2 : Create the connection file 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();
  }
?>

Step 3 : Create the image.php file

<?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'];
?>

Once done, put the connection file (config.php), images.php and the images folder in the same directory and run the images.php once giving the appropriate values to directorypath, database name, username, password etc.

Once done, your goal will be achieved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM