I have a code to display images from folder which is working fine but in Google chrome it's showing one extra box like image.How I can fix this ?
Here is my code:
<?php
$files = glob("photogallery/thumb/group1/*.jpg*");
for ($i=0; $i<=count($files); $i++)
{
$num = $files[$i];
echo '<img src="'.$num.'" width="100px" height="100px" alt="">'." ";
}
?>
The problem is that count() is going to return the total number of items in your zero-indexed array. That means if your $files array has 10 items in it, the lowest index is 0 and the highest index is 9.
Your comparison of $i<=count($files) means that $i would go from 0 to 10 (11 total items) in this example.
I recommend using foreach anyway. It's a more generic way of iterating over a collection, and allows for sparse arrays.
foreach ($files as $file) {
// use the $file variable here in your echo
}
<?php
$files = glob("photogallery/thumb/group1/*.jpg*");
for ($i=0; $i<=count($files); $i++)
{
if(trim($files[$i])){
$num = $files[$i];
echo '<img src="'.$num.'" width="100px" height="100px" alt="">'." ";
}
}
?>
Related
I'm trying to make something like a flexbox with CSS and PHP. The idea is to upload .jpg images to a folder and then sort them into four columns equally. My current approach is to count the total number of files in the folder, divide by four and with that create indices for a foreach loop to iterate and put the images into the columns. I need it to look like this.
Later on I'll have to make a popup on each of these images which should bring some basic HTML/CSS thing. Not sure if that's something to take in mind for this particular case.
This is my (incomplete/not working) code:
<?php
$fi = new FilesystemIterator("../../showoff_images", FilesystemIterator::SKIP_DOTS);
printf("There were %d Files", iterator_count($fi));
$fi = iterator_count($fi);
#Divided $f1 by four to get the number of items in columns
$fby4 = intdiv($fi,4);
#Create start index for each image
$f0 = 0;
$f1 = $fby4-1;
$f2 = $fby4*2-1;
$f3 = $fby4*3-1;
$f4 = $fby4*4-1;
#CHECK THE INDEX FOR THE IMAGES. START AT 0 AND START ITERATING WITH THE $fn VALUES FOR THE FOLLOWING FUNCTION!
#Iterates images
$directory="../../showoff_images/";
$images=glob($directory . "*.jpg");
$lazyload = "lazy";
$measure = "100%";
$distance = "20px";
foreach($images as $image=>$value) {
if($value > 4) continue;
++$value;
echo
"
<h2 style = padding-top= ".$distance." padding-bottom= ".$distance." padding-right= ".$distance." padding-left= ".$distance."></h2>
<img src=" .$image." loading =". $lazyload ." height=".$measure." width=".$measure.">
";
#++$f0;
}
?>
Really appreciate your help and glad to be joining the forum!
Comment to answer
You should be able to use array_chunk for this, which will allow you to break your array up into sub-arrays.
Here's a sample, not really bound to your data but it should make sense.
$letters = range('a', 'z');
$rows = array_chunk($letters, 4);
foreach($rows as $row){
echo '<div>', PHP_EOL;
foreach($row as $letter) {
echo $letter, PHP_EOL;
}
echo '</div>', PHP_EOL;
}
Demo here: https://3v4l.org/HpTSH
I have a php procedure that scans a directory where its content is thumbnail images of pdf files. The thumbnails are then displayed in a table, and included in an iframe of a parent web page. Each thumbnail is itself a hyperlink when clicked will open the actual pdf file. To avoid having a horizontal scroll bar, 9 images in a table row is perfect. I wrote a nested loop that in effect acts like a word wrap, where it displays 9 images and then begins another row. The actual code is much more entailed, so I have it pared down to a bare minimum example. It seems almost counter intuitive on first glance, decrementing $i on the second line of the outer loop, but it works. I wonder if anyone has a more elegant solution?
$ary = array(1,2,3,4,5,6,7,8,9,10);
for ($i=1; $i<(count($ary)+1); $i++) {
$i = $i-1;
for($j=0; $j<9; $j++) {
if ($i === count($ary)) break;
echo ($ary[$i].", ");
$i+=1;
}
echo "<br>";
}
The completed code now where $ndx is the count of the array, $dir is the scanned directory containing the png images, and $rtDir is the directory where the pdf's are saved:
if ($ndx > 0) {
$tbl = '<div id="draggable" class="ui-widget-content">
<ul>
<table><tr>';
/* place 9 images on one row */
foreach ($myfiles as $index => $image) {
$pdf = basename($image, ".png");
$pdf = $pdf . ".pdf";
$pdf = $rtDir.$pdf;
$tbl .= '<td>
<span class="zoom">
<a href="'.$pdf.'" target="_blank">
<li><img id="pdfthumb'.$index.'" class="myPhotos" alt="pdf'.$index.'" src="'.$dir.$image.'" ></li>
</a>
</span>
</td>';
if ($index % 9 == 8) {
/* end the current row and start a new one */
$tbl.= "</tr></table><br><br><table style='margin-top:-40px'><tr>";
}
}
$tbl .= "</tr></table></ul></div>";
printf($tbl);
unset($myfiles);
}
Thanks everyone for your suggestions.
So you want 9 images on each row? There are usually two logical choices:
Alternative 1: You use array_chunk(), e.g. like this:
$chunks = array_chunk($images, 9);
foreach ($chunks as $chunk) {
foreach ($chunk as $image) {
// image printing goes here
}
echo '<br'>;
}
Alternative 2: You use the modulo operator, e.g. like this:
foreach ($images as $index => $image) {
// images printing goes here
if ($index % 9 == 0) { // or 8, since it's a 0-index array... I don't remember
echo '<br>';
}
}
I mostly use the second version, myself, if I have to - or I ask one of our designers to make it fit to a proper width through css. Do note also that the second version won't work if your images array is associative.
$b = count( $ary ) - 1 ;
for( $i = 0 ; $i <= $b ; $i++ ) :
echo $ary[$i] ;
if( ( $i + 1 ) % 9 == 0 ) :
echo "<br>" ;
endif ;
endfor ;
like the above comment i like modulus but i also prefer the for loop, hope this helps.
I want to loop and echo images specific value, I have a db value e.g 100, but i want to echo only 96 images only not more than that. Also whatever the value from DB and loop should print the exact times not exceeding 96(which is fixed)
$check = "SELECT white FROM balloons";
$rs = mysqli_query($con,$check);
if(mysqli_num_rows($rs)==1)
{
$row = mysqli_fetch_assoc($rs);
$g=$row['white']; //eg 2,
for($imagecount=0;$imagecount>$g;$imagecount++) {
echo '<img src="img/whiteB.png" class="w_over" />';
}
}
for ($i=0; $i < min($g, 96); $i++) {
echo '<img src="img/whiteB.png" class="w_over" />';
}
I'd change
for($imagecount=0;$imagecount>$g;$imagecount++) {
to
for($imagecount=0;$imagecount<=min($g,96);$imagecount++) {
Pls. note that the 2nd param in for needs to inverted (loop while condition is true)
If i get your question correctly you just need to change your line,
$g=$row['white'];
to:
$g = ($row['white'] < 96) ? $row['white'] : 96;
Side Note:
for($imagecount = 0; $imagecount < $g; $imagecount++) //Should be less then
^
Error in for loop condition!
change for loop head:
for($imagecount=0;$imagecount<$g;$imagecount++)
How can i show or echo image which is inserted in comma format like this
When i m inserting 1 image
<img src="images/<?php echo row['image'];?>" >
this code is running fine.. But when i m inserting more than 1 image at a time. its not showing any image.. Should i use rtrim or explode to remove commas & show images.. any help .. ?? Thanks in advance
You need to explode the return data in an array and then need to display them, something as
$images = explode(",",$row['image']);
if(sizeof($images) > 0 ){
foreach($images as $image){
echo '<img src="images/'.$image.'">' ;
echo "<br />";
}
}
You can style the display of images separation as you want instead of BR as in my exapmple
Well.. how do you want to show multiple images in the same tag?
Loop trough them:
$images = explode(',', $row['images']);
foreach($images as $img)
{
echo '<img src="images/' .$img .'" >';
}
you can explode them then loop through them
$img = explode("," $row['image']);
for($i = 0; $i < sizeof($img); $i++){
echo "<img src=\"images/" . $img[$i] . "\" >";
}
But you should really normalize your table/database so many images have 1 or more parent id
i have some basic php code that pulls an image from a particular image folder when the user asks using a form.
I will have many image folders and want to generate a random image instead of using
case 'A' : echo "<a href=\"Alphabet-Letters/Letters-A\">
<img src=\"image/data/A/A_001.jpg\" id=\"A1\" width=\"70\" height=\"120\" title=\"A1\"/> </a>" ; break;
My question is this as the form is processed with someone using the letter A the picture of that letter appears. The php code for this is
if (array_key_exists('check_submit', $_POST))
{
$letters = $_POST['Comments'];
$num_letters = strlen($letters);
for($i = 0; $i < $num_letters; $i++)
{
switch ($letters[$i]) {
case 'A' : echo "<a href=\"Alphabet-Letters/Letters-A\">
<img src=\"image/data/A/A_001.jpg\" id=\"A1\" width=\"70\" height=\"120\" title=\"A1\" alt=\"Image A\"/>
</a>" ;
break;
This only pulls the exact image i have asked, but i have hundreds in that folder and would like a more simple code to work with.
Please can someone help, they gave advise on using random image from folder, but that only works as a starting point not on the code I already have.
Thanks for your time
The solution posted at the link below should achieve the functionality you are looking for.
https://stackoverflow.com/a/4478788/1152375
so you should be able to do something like
case 'A' : echo "<a href=\"Alphabet-Letters/Letters-A\">
<img src=\"image/data/A/" . random_pic("folder_with_pics") . "\" id=\"A1\" width=\"70\" height=\"120\" title=\"A1\</a>";
break;
before you get to the output code, you will want to get a list of all files in the relevent directory ( http://php.net/manual/en/function.dir.php ) in a numbered array.
count the number of items in the array ( http://php.net/manual/en/function.count.php ) and randomly pick one ( http://php.net/manual/en/function.rand.php ) using the min and max settings of rand.
Try using the scandir function to find all the files in the folder, and then use the rand function to randomly choose one:
if(!empty($_POST['check_submit']))
{
$letters = strtoupper(trim($_POST['Comments']));
$num_letters = strlen($letters);
for($i = 0; $i < $num_letters; $i++)
{
$letter = $letters[$i];
$folder = 'image/data/'.$letter;
$files = scandir($folder);
array_shift($files);
array_shift($files);
$index = rand(0, count($files) - 1);
$file = $files[$index];
echo "<a href=\"Alphabet-Letters/Letters-{$letter}\">\n";
echo "<img src=\"image/data/{$letter}/{$file}\" id=\"{$letter}{$index}\" width=\"70\" height=\"120\" title=\"{$letter}{$index}\" alt=\"Image {$letter}\"/>\n";
echo "</a>\n";
}
}
Found the answer by using #Buchow_php and trial and error.
for ($i=0; $i<$num_letters; $i++)
{
$image_num = '$image'.$i;
echo "<input type=\"hidden\" name=\"option[$image_num]\" value=\"$skus[$i]\" />";
}
together with the previous code and now it brings all the image files into an array for posting to my submit