I am trying to create a dynamic gallery in php with specific order of pictures on the page, but I can't find the function or piece of php code to do so.
Conditions:
The gallery must be dynamic, pictures will be coming from a folder via php, because when I'll add/upload more pictures in the folder they must be displayed on the page without adding manually the html tags in. ( this part is easy, problem is the condition 2 ).
The first row will have 5 pictures, the second - 4 pictures (important for the specific effect).
My code:
$files = glob("layout/gallery/*.jpg");
rsort($files, SORT_NATURAL);
for ($i=0; $i < count($files); $i++) {
for( ; $i<5; $i++){
$one = $files[$i];
echo '<img src="'.$one.'">' . '<br><br>';
}
echo '<br>';
for( ; $i<9; $i++){
$two = $files[$i];
echo '<img src="'.$two.'">' . '<br><br>';
}
}
The code works well, but it just displays 9 pictures obviously. I was unable to make it dynamic displaying 5 pictures first, 4 pictures after and stay this way in a loop till displays all pictures from that folder.
You can take advantage of the array_splice function that removes elements from the array everytime it returns those elements :
$files = glob("layout/gallery/*.jpg");
rsort($files, SORT_NATURAL);
// split the files in rows
$rows = [];
while(count($files) != 0) {
// even rows have 5 elements, odd ones have 4
$num_files_to_splice = count($rows) % 2 == 0 ? 5 : 4;
$rows[] = array_splice($files, 0, $num_files_to_splice);
}
// display them accordingly
foreach($rows as $row) {
foreach($row as $file) {
echo '<img src="'.$file.'">';
}
echo '<br><br>';
}
Related
The following code lists the files in the "img" directory and gives the total number of files at the end:
$path = 'img/*.*';
$count = 0;
foreach(glob($path) as $filename){
echo basename($filename)."<br>";
$count++;
}
echo "<br>".$count;
Now I want to list the files, but skip the first 100 files.
So instead of showing files 1 to 500, I want to see files 101 to 500.
How would I achieve this? Thanks in advance.
Inside the loop, add an if condition to echo the file name only if $count >= 100:
$path = 'img/*.*';
$count = 0;
foreach(glob($path) as $filename){
if($count >= 100) echo basename($filename)."<br>";
$count++;
}
echo "<br>".$count;
I am trying to display a certain amount of random images from one directory on my website without displaying duplicates.
I found this question: Display 2 random pictures PHP with no duplicates which partially answers my problem:
<?php
$pics = array('image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg');
$images = array_rand($pics, 2);
?>
<img src="images/<?php echo $pics[$images[0]]; ?>" />
<img src="images/<?php echo $pics[$images[1]]; ?>" />
The problem I have is that the images are uploaded to the folder with completely different/random names such as 1a2265fg65444.jpg 55v4423097ww6.jpg etc, so I can't manually add them to the $pics array. I need to somehow scrape the directory for *.jpg and have the array generated automatically
I did try Michal Robinsons answer on Show Random images from a folder without repeating using JS or PHP but couldn't get it to print anything for some reason:
$all_images = glob("/images/photos/{*.jpg}", GLOB_BRACE);
shuffle($all_images);
$images = array();
foreach ($all_images as $index => $image) {
if ($index == 15) break; // Only print 15 images
$image_name = basename($image);
echo "<img src='/images/photos/{$image_name}' />";
}
Perhaps I'm missing something?
Any pointers would be simply awesome.
Many thanks
try this
$scan = scandir("/images/photos/");
shuffle($scan);
$r = rand(2, count($scan)); // maybe (count($scan) - 1)
printf("<img src='/images/photos/%s' />", basename($scan[$r]));
Thanks Aron, but I managed to fix it by changing:
glob("/images/photos/ to
glob("images/photos/
To complicate things further though, I just realized I need to show 1 image from a 1st folder followed by another image from a 2nd folder, and repeat this about 5 times, still without showing any duplicate :/ This is as far as I have got, but seem to be digging a bigger hole that doesn't work, as I'm stuck at the foreach...:
$all_images1 = glob("images/photos/{*.jpg}", GLOB_BRACE);
$all_images2 = glob("images/photos1/{*.jpg}", GLOB_BRACE);
shuffle($all_images1);
shuffle($all_images2);
$images1 = array();
$images2 = array();
$class = 1;
foreach ($all_images1 as $index1 => $image1) {
if ($index1 == 10) break; // Only print 15 images
$image_name1 = basename($image1);
echo "<img src='/images/photos/{$image_name1}' class='image".$class++."' /> <img src='/images/photos1/{$image_name2}' class='image".$class++."' />";
}
Am I going in the wrong direction to do this?
Thanks Ted
I have this two arrays:
$files=glob("filepath/folder/*.*);
$thumbs=glob("filepath/folder/thumbs/*.*);
Main folder called in $files gets the full resolution images, while thumb has thumbnails for faster page loading, so it does not need to load the big images and resize them.
But my problem is now how to echo these two loops out?
I've tried nested foreach loops like
foreach ($files as $fl){
foreach ($thumbs as $tb) {
echo '<img src="<?=$tb?>">';
}
}
But this makes duplicates because it echoes for each element in files and for each in thumbs.
How can I echo them without duplicating?
Thanks everyone for help, this worked
$len = max(count($files), count($thumbs));
for($i=0; $i<$len; $i++){
$fl = isset($files[$i]) ? $files[$i] : '';
$tb = isset($thumbs[$i]) ? $thumbs[$i] : '';
echo <img src="<?=$tb?>">;
}
I am trying to figure out why the following piece of code breaks the outputted PDF..
I am copying several PDF files, and they get combined just fine, and the PDF forms get filled out, but when I enable to code below I only get a blank PDF (Nothing filled in) and this shows up on the error log.
Warning: input PDF is not an acroform, so its fields were not filled.
for ($i = 1; $i <= $totesi; $i++) {
$pgnum = ($i + 1);
if(!copy('../attachment2.pdf', $pgnum . '.pdf')) {
echo 'Failed to Copy';
}
$pdfarr[] .= $pgnum . '.pdf';
}
I build the array so I delete only those files afterwards.
This is the command that combines them, maybe I am missing something?
pdftk *.pdf cat output file.pdf
And again as said with the code above, it doesn't work.
Update:
I tried it with splitting the archive with pdftk it works fine for 1 page, if the array contains less than 10 meters, anything more it will error out.
if ($totesi >= 1) {
for ($i = 1; $i <= $totesi; $i++) {
if(strpos($type, 'Nodal') !== FALSE) {
$tempstr .= ' 6';
} elseif(strpos($type, 'All Inc') !== FALSE) {
$tempstr .= ' 5';
}
}
shell_exec("pdftk 1.pdf cat ".$pdffile." ". $tempstr ." output 1111111.pdf");
unlink('1.pdf');
}
Warning: input PDF is not an acroform, so its fields were not filled.
pdftk 1.pdf cat 1-4 5 5 output 1111111.pdf
This doesn't work either.
I am trying to create a webpage which shows 10 files stored in a directory each time. I don't want to use database for it though. This is what I have up to this point.
<?php
$exclude = array("index.php");
$cssfiles = array_diff(glob("*.php"), $exclude);
foreach ($cssfiles as $cssfile) {
$filename = "http://example.com/lessons/css/".$cssfile;
outputtags($filename,true,true);
}
?>
This prints out all results, I can't figure out how to show just first ten results and after that when user clicks next 10 more results without using database. I think using a database just for this purpose doesn't make sense.
EDIT The reason I want to do it this way is because I am getting max_user_connection error.
You could do that by storing your files into an array and sort it as you wish, like following :
$exclude = array("index.php");
$cssfiles = array_diff(glob("*.php"), $exclude);
$files = array();
foreach ($cssfiles as $cssfile) {
$filename = "http://example.com/lessons/css/".$cssfile;
$files[] = $filename;
}
asort($files);
// Pagination start from here
$page = 1; // You will get this parameter from the url using $_GET['page'] for instance
$limit = 10; // Number of files to display.
$offset = (($page-1) * $limit);
$max = ($page * $limit);
$max = $max > count($files) ? count($files) : $max;
for ($i=$offset; $i<$max; $i++) {
echo $files[$i] . PHP_EOL;
}
echo 'Total Pages : ', count($files). PHP_EOL;
echo 'Page number : ' , $page;
Maybe you could use ajax and extract the pagination page to avoid fetching all the files each time.
It depends on how you want to select those ten pages. One could use a for loop that lists the files 0-9 as and may count from 10-19,..., depening on the page range the user requests.
However, when a file is added, the system would get out of order. In that case, loading / saving some sorting Information to sessions / cookies could solve the problem.
EDIT : Using a database is, however, the standard for tasks like this. Even if you don't like them, you will most likely need them for more complex Tasks that require sorting, searching or joining multiple datasets, that are just too complicated to achieve with the filesystem functions.