Get images from multiple folders within /images and place each folders contents in its own div

StackOverflow https://stackoverflow.com/questions/13738672

Вопрос

I am trying to make php do the following.

I have a structure of folders in my root directory. /images/1234, /images/2345, /images/3456 etc. (multiple folders all in images)

I would like to get the contents of each folder and put their contents into their own div, using php and not hard coding as the folders and files will change regularly.

<div><img src="images/1234/1.jpg"><img src="images/1234/2.jpg"><img src="images/1234/3.jpg"></div>
<div><img src="images/2345/1.jpg"><img src="images/2345/2.jpg"><img src="images/2345/3.jpg"></div>
<div><img src="images/3456/1.jpg"><img src="images/3456/2.jpg"><img src="images/3456/3.jpg"></div>

The files/folders will constantly be added so these divs would have to be in some sort of foreach loop for each folder found in the images directory.

Essentially each folder in images folder would be a div, the divs would auto populate as the browser was refreshed if new folder existed.

I can make it work with php to bring out all images and just place on page,r but I need this structure instead. Thanks for any help.

Это было полезно?

Решение

foreach (glob('images/*') as $dir) {
  echo '<div>'; // One div per directory
  foreach (glob($dir."/*.jpg") as $img) {
    echo "<img src='$img'/>";
  }
  echo "</div>\n";
}

Другие советы

You need stored in one place all of your data structures in string format. example (sql database). Then execute a sql query to retrieve all the locations of roads image files. Then wrap with a php function in order to apply the paths (path).

This should be something like his.

WITH SQL DATABASE

<?php
// Formulate Query
$querySQL = "SELECT imageURL FROM tableImage";

// Perform Query
$result = mysql_query($querySQL );

//Gets all channels url images in an array variable
//Then attributed to code html image tag

<div><img src=".$result[0]." ></div>
<div><img src=".$result[1]." ></div>
<div><img src=".$result[2]." ></div>

?>

WITHOUT SQL it is possible to do to retrieve the list of image files with their full path without sql. Only the folder path should be known and given hard.

<?php
//path to directory to scan
$directory = "../images/team/harry/";

//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");

<div><img src="$directory.$images[0]." ></div>
<div><img src="$directory.$images[1]." ></div>
<div><img src="$directory.$images[2]." ></div>

?>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top