Jump to content

Displaying Images From Array Using PHP/HTML


BorysSokolov

Recommended Posts

I have several images in a directory, which I would like to output(in an organized way) to the page. I've tried reading all the directory contents, and loading their names into an array to display the images using HTML, but I can't for the life of me figure out how to then position the pictures correctly in CSS.

 

Here's the code:

 

<?php
$dirLoc = 'myWebsite/galleryPics';
$handle = opendir($dirLoc);


while($readDir = readdir($handle)){
if($readDir != '.' && $readDir != '..'){
$pic[] = $readDir;
}
}


foreach($pic as $k){
echo "<div class = \"picsDiv\">";
echo "<img src = \"$dirLoc/$k\" height = \"100px\" width = \"100px\"/></div>";
}
?>


<html>
<head>
<style type = "text/css">

.picsDiv {width:150px;
 height:150px;
 border:1px solid red;
 background-color:grey;}

img {position:relative;
 top:50%;
 left:50%;
 margin-top:-50px;
 margin-left:-50px;}
</style>
</head>
</html>

 

Thank you in advance for any feedback.

What is "correctly" supposed to be? How does it look now and how do you want it to look?

 

At the moment the images are positioned in a single, straight, vertical column, with no spaces in between them. I'd like them, however, to run left to right, skipping to the next line once there's no more space, with some space separating them; like a typical grid layout.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.