Trying to display all photos from a directory in a div.
Looking to show all portraits first, then squares, then landscapes last.
Will align and style later, but first I just need a point in the right direction on how to sort the results of a callback.
do1.php
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['showphotos'])) {
$af = glob("main/photos/*.*");
for ($i=0; $i<16; $i++) {
$iname = $af[$i];
$supported_format = array('gif','jpg','jpeg','png','mp4');
$ext = strtolower(pathinfo($iname, PATHINFO_EXTENSION));
if (in_array($ext, $supported_format)) {
list($width, $height) = getimagesize($iname);
if( $height / $width > 1.15) {
$ori = "p";
} elseif( $width / $height > 1.15) {
$ori = "l";
} elseif( $height / $width < 1.15) {
$ori = "s";
} elseif( $width / $height < 1.15) {
$ori = "s";
}
echo "<img class='".$ori.":' title='".$ori.$iname."' src='".$iname."' alt='".$iname."' height='300px' />";
} else {
die("something went terribly wrong you ditz!");
}
}
exit;
}
The above gets all the files, determines whether portrait/landscape/square, and sets a className of s, l, or c.
Script on Index.php
$(document).ready(function(){
$("#galphoto").click(function(){
$("#chme").children().hide(2000, function() {
$("#supics").delay(2000).slideDown(2000);
var id1 = 1;
$.ajax({
url:'do1.php',
type:'POST',
data:{showphotos: id1},
success: function(pics){
$('#supics').html(pics);
}
});
});
});
});
Of course this puts all of the images into the div.
I thought I could filter them somehow by assigning each image a class name based on there orientation, but after many trials and errors I haven’t succeeded.
I know there must be some simple thing like attr('.p')
or something. If this question is a duplicate please point me to the correct page. Thanks for any help.
3
Answers
To achieve the results I needed to use
.load
instead of an ajax call.First I edited do1.php by taking out the
if($_SERVER["REQUEST_METHOD"]...
and then changed theehco
statement to add a unique id.do1.php
So now the page does not need to be AJAX, called just loaded. Also; Each image is assigned a unique id
id='i0', id='i1', id='i2'...
, and each element has an assigned class based on it's orientation (portrait=p, landscape=l, square=s).Next I added three span elements (#c1, #c2, #c3) into the div where I wanted the content to show (#supics).
Next I edited my script to LOAD from do1.php insted of using the AJAX POST.
Script on Index.php
This may be dirty (though I'm no judge at determining that), but it allows me to load everything I need at once, then sort the content, and style it all anyway I need. I hope this helps someone out. Thank you all for the help.
Instead of outputting the result directly, place the results of your orientation check into one of 3 arrays.
Then loop over those arrays outputting what you want, you will also be able to change the attributes to fit the orientation this way.
And this is the jQuery Solution !