display images with same key in PHP? - php

I'm trying to display all the images with the same key from a folder.
the images are stored like so:
34526Image1.jpg
34526Image2.jpg
34526Image3.jpg
34526Image4.jpg
etc etc....
so the key is the first part of the image name (the random numbers) which in this case is 34526.
I've tried to use glob() function in PHP but I only get the first image which is 34526Image1.jpg.
my glob() code is this:
<?php
foreach(glob('../my-images/') as $image)
$i = 1;
{
$pic_list .= '<a id="example1" href="'.$image.''.$randKey.'Image'.$i++.'.jpg"><img alt="example1" src="../my-images/'.$randKey.'Image'.$i++.'.jpg" /></a>';
}
echo $pic_list ;
?>
could someone please advise on this?

Your call to glob contains no pattern. You can use wildcards for no or many characters with *, or for single characters using ?. Also ranges like for example [0-9] are possible. In your case you want everything that starts with a random number, determinted by $randKey, followed by the word Image, a counter value and .jpg. So all you have to do is to use a wildcard for the counter value like in your example 34526Image*.jpg.
This results in the following code
<?php
$pic_list = '';
$id = 0;
foreach(glob('../my-images/'.$randKey.'Image*.jpg') as $image) {
$pic_list .= '<a id="example'.++$id.'" href="'.$image.'"><img alt="example'.$id.'" src="'.$image.'" /></a>';
}
echo $pic_list;
?>

you have error in your foreach statement, for each file in folder you execute only 1 code line $i = 1;, everything after ; is executed only one time, after all iterations of foreach
you need to use proper foreach:
$files = glob('../my-images/'); // need to define proper mask here to get only files with $randKey
$i = 1;
foreach($files as $image)
{
$pic_list .= '<a id="example1" href="'.$image.''.$randKey.'Image'.$i.'.jpg">';
$pic_list .= '<img alt="example1" src="../my-images/'.$randKey.'Image'.$i.'.jpg" />';
$pic_list .= '</a>';
$i++;
}

Related

how to display image randomly inside for loop in php

i have a simple webpage where i am trying to echo multiple images, my code is like below
<?php for($l=1;$l<=45;$l++){?>
<div class="thumb" style="background-image: url(l<?=$l?>.jpg);"></div>
<?php } ?>
so here the images are displayed fine in an order from 1 to 45, but what i want is images to display randomly everytime the page is loaded, can anyone please tell me how to accomplish this, thanks in advance
As mentioned in the comments, just create an array and shuffle it.
$images = [];
for ($l = 1; $l <= 45; $l++) {
$images[] = "<div class='thumb' style='background-image: url(l{$l}.jpg);'></div>";
}
shuffle($images);
echo implode("\n", $images);

how to use "for" loop PHP to make multiple variable in PHP,

i have more then 5 link for image, so i want to make like this,
$p_image1, $p_image2, $p_image3, $p_image4, $p_image5
but i dont understand why my cod is not work...
this is my code for get array data:
$id = mysqli_real_escape_string($koneksi,$_GET['i']);
$query = mysqli_query($koneksi,"SELECT * FROM `tb_produk` WHERE `p_id` = '$id'");
$get = mysqli_fetch_array($query);
this is my code for loop:
if ($j_image > 1) {
for ($i = 1; $i <= $j_image; $i++) {
$p_image[] = $get['p_image'.$i];
if ($i > 4) {
break;
}
?>
<li data-uk-slideshow-item="<?php echo $i ?>">
<img src="<?php echo $get['p_image'.$i]; ?>">
</li>
<?php
}
}
?>
why this is not work, thanks for your help before :)
I think creating dynamic named variables is just a way to make things harder than it is.
Instead I believe fixing the problem is the solution.
Dynamic variables is just harder to work with than arrays and will make bugs in your code some day.
Here I use a foreach on the $get array which means it will loop the items there is and we don't have to count and guess stuff.
Then I removed this new array you created as I don't see the point of it and instead go directly to the output part with my foreach variable $image.
I also keep the code in PHP and echo the html as I find that easier to read, but that is purely opinion and you can do whichever you want.
$i=1;
foreach($get as $image){
if ($i > 4) {
break;
}
echo "<li data-uk-slideshow-item=" . $i .">\n";
echo ' <img src="' . $image . '">' . "\n";
echo "</li>\n";
$i++;
}
Example output:
<li data-uk-slideshow-item=1>
<img src="1">
</li>
<li data-uk-slideshow-item=2>
<img src="2">
</li>
<li data-uk-slideshow-item=3>
<img src="3">
</li>
<li data-uk-slideshow-item=4>
<img src="4">
</li>
Not sure if your code is supposed to output four or five items.
But I just left the if and break as it was in your code.
https://3v4l.org/MrAng
Array is the perfect way to handle multiple similar values. You should really not consider making separate variables for this.
Still, in order to create dynamic variables, you need to use an additional $, and wrap them in curly braces {..}:
for ($i = 1; $i <= $j_image; $i++) {
// Create string for dynamic variable name and put it inside curly braces
// use $ in front to define this as a new variable
${'p_image'.$i} = $get['p_image'.$i];
if ($i > 4) {
break;
}

PHP foreach loop for all images in a HTML 3 table column

Hi I'm a newbie with PHP and this site, so please be nice :)
I'm currently having trouble working the below PHP foreach code out as I'm trying to echo all images in a HTML table 3 column but it echo's with 2 only.
UPDATE: I've managed to fix some issues thanks to the comments guy's, thank you. However, I', now experiencing another issue which is confusing.
Basically, If I have one picture in a folder, it will echo that one picture, but If I put two pictures there, it echo's out with 4, 1 first picture echo's with 2 and the second is with 2 as well. Basically showing 4 images even though I have 2 images in that folder. I can't seem to fix this..
Here's the code:
<?php
// get images
$images = glob($imagedir.'/' . "*.png");
$i = 0;
echo'<table><tr>';
foreach($images as $image)
{
$i++;
echo '<td><img src="'.$image.'" height="200"></td>';
if($i == 3)
{
echo '</tr><tr>';
$i = 0;
}
}
echo '</tr></table>';
?>
Thanks in Advance
You're code for rendering the HTML is just fine.
If you have duplicates, the content of your imagedir must be wrong.
A few remarks:
glob($imagedir.'/' . "*.png"); also include directories which names end as .png.
Depending on the amount of images, the last table row will not be completely filled with table cells.
It's good practice not using the php closing tag ?> at the end a the php file.
I've altered you code to avoid above to problems.
I'm sure there more/other ways to do this, but this came in mind first.
<?php
$imagedir = 'images';
//Get *.png files only
$images = array_filter(glob("$imagedir/*.png"), 'is_file');
//Make image array divisble by 3 (columns)
while (count($images) % 3 != 0) {
$images[] = '';
}
echo'<table><tr>';
for ($i = 1; $i <= count($images); $i++) {
//Render TD if $image is not empty
if ($images[$i-1] != '' != '') {
echo '<td><img src="' . $images[$i-1] . '">', "<br>Image $i</td>";
}
//Close table row after 3 TD's
if($i % 3 == 0)
{
echo '</tr><tr>';
}
}
echo '</tr></table>';

PHP pull specific sets of images from a directory

I'm pulling an image from a directory (images) and placing it in an html table row along with my mysql query. PHP finds the image as the mysql DB id matches the image name. i.e. if id = 12 then 12.jpg is pulled from directory using $imgnum in the path.
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$imgnum = $row["id"];
?>
<tr>
<td>
<img src="images/<?php echo $imgnum ?>.jpg"/>
The issue is there may be a series of images all associated with that row, i.e. 1-1.jpg 1-2.jpg and so on, sometimes there may be only one and the code above works fine.
Is there a way to output all images with the name equalling the id but with a dash afterwards so it picks up other associated images? Even if someone can tell me what to learn to achieve this?
cheers
You can use php's glob() to find similar files. Something along the lines of this:
foreach (glob("/path/to/images/folder/" . $imgnum . '-*') as $filename) {
// do something with $filename
}
Thought I'd post what I used from the help above. It now outputs a list of all of the images I needed.
<?php
$imgnum = $id;
foreach (glob("images/" . $imgnum . '-*') as $filename) {
echo "<a href=''><img src='$filename' class='imgsize'></a> <br>";
}
?>

Using glob() to display images from a directory while echo'ing a unique first image

I am quite new to PHP and I did not want to ask anything here until I absolutely could not find a way to do this.
I have a situation where I would like to use PHP to read and display images from a directory but I would like the first image read to be echo'd uniquely
My current code as follows:
$imageDir = "img/landMarks/carousel/";
$images = glob($imageDir.'*.jpg');
foreach ($images as $image){
echo '<div class="item">'."\r\n\t\t";
echo '<img src="'.$image.'" alt=""></div>'."\r\n\t";}
Just a simple directory pull and echo with formatting.
This will be for a BootStrap carousel and unfortunately the first image in line has to have <div class="item active"> or the whole thing fails.
So to reiterate, the first image pulled needs to be <div class="item active"> while the rest need to be <div class="item">
I think I went through an entire pack of cigarettes wracking my brain around this.
use a flag for that. hope to help.(-:
$imageDir = "img/landMarks/carousel/";
$images = glob($imageDir.'*.jpg');
$flag=1;
foreach ($images as $image){
echo '<div class="item' .($flag?' active':''). '">'.PHP_EOL."\t\t";
echo '<img src="'.$image.'" alt=""></div>'.PHP_EOL."\t";
$flag=0;
}
How about using a for loop instead of a foreach loop. When index == 0 you can add "active" to your div's class.
Note I didn't run this code- but it should give you the right idea.
$imageDir = "img/landMarks/carousel/";
$images = glob($imageDir.'*.jpg');
for($i = 0; $i < count($images); $i++){
//If the index is 0, it's the first image.
$class = ($i == 0) ? 'item active' : 'item';
//Dynamically change your div's class
echo "<div class='$class'>\r\n\t\t";
echo '<img src="'.$images[$i].'" alt=""></div>'."\r\n\t";
}

Categories