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?>">;
}
Related
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>';
}
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 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.
The following code returns the urls of the pics that has to be displayed:
$link = "http://example.com/ext/lib/exe/process.phpt=87458+52&w=466&h=471&:forum_pic:queue_hasb_bunny_khd_tyun_02300.jpg, Imm processing,http://example.com/storage/high resolution/i-white bunny-tyun.jpg,only cash payment,process after christmas, handle with care notification, date: 2-5-2015";
$extraFieldData = explode(',',$link);
$images= array();
foreach($extraFieldData as $efd){
if(strpos($efd,'jpg')!== false){
$images[] = $efd;}
}
foreach ($images as $i){
echo $i;
}
<img src= "<?php echo $i;?>"/>
The code works fine to echo the urls of the pictures but when I try to display the urls in the browser, only one picture is displayed and not the other one. How to get both the urls to display?
If it shows only one image (and the image URLs are correct), you're doing it outside of the loop and $i has the value of the last image iterated from the $images array.
For example:
$arr = ['one', 'two', 'three', 'four'];
foreach($arr as $a) {
}
echo $a;
This will output four.
I have both jpg and gif images in my directory and I am trying to display them both.
However, below will only display gif:
$pictures = glob("images/*.{gif,jpg}", GLOB_BRACE);
And this below will only display jpg:
$pictures = glob("images/*.{jpg,gif}", GLOB_BRACE);
Here is the whole thing I am working with:
<?php
$pictures = glob("images/*.{gif,jpg}", GLOB_BRACE);
for( $i=0; $i<=10; $i++ ){
echo "<img src=\"".$pictures[$i]."\" />";
}
?>
I have also tried with an absolute path and had no such luck displaying both.
What might be the problem?
Thanks in advance.
You seem to be limiting your search to the first 10 matches. If there are more than ten of each, then you will get them in the order you specified (since they are sorted by how they are found, not alphabetically).
You could use a foreach loop to iterate through all files, or you could add sort($pictures) before the loop.