PHP - Display 4 most recent images from directory - php

Very new to PHP. I have sucessfully got this piece of code working to display 4 images from a directory, however it shows the first 4 images by name (001.png, 002.png, 003.png and 004.png) which are the lowest numbers and happen to be the least recently uploaded:
<?php
$pictures = glob("directory/*.png");
for( $i=0; $i<=3; $i++ ){
echo "<img src=\"".$pictures[$i]."\" />";
}
?>
I am looking to change that to get the 4 most recently uploaded in a directory by name.
In other words, I would like to display the last 4 images with the highest number.
I have tried this below however I am getting Parse error: syntax error, unexpected T_VARIABLE on line 4
<?php
$pictures = glob("directory/*.png");
$no_pictures = count($pictures)-1
$limit = $no_pictures-3
for( $i = $no_pictures; $i >= $limit; $i--; ){
echo "<img src=\"".$pictures[$i]."\" />\n";
}
?>
Any help is appreciated. Thanks for your time.

You are missing the ending semicolons ; on lines 3 & 4 [edit] AND you have an extra one in your for loop after $i-- -
<?php
$pictures = glob("directory/*.png");
$no_pictures = count($pictures)-1; // was missing ;
$limit = $no_pictures-3; // was missing ;
for( $i = $no_pictures; $i >= $limit; $i--){ // removed ; after $i--
echo "<img src=\"".$pictures[$i]."\" />\n";
}
?>

Related

Trying to solve this php code getting error

i am trying to print 2 4 6 8 10 with spaces, but it prints 246810.
for($i = 1; $i<= 5; $i++){
echo $i * 2.' ';
}
i am getting error : "This page isn’t working"
This might help.
for($i = 1; $i<= 5; $i++){
echo ($i * 2).' ';
}
try:
for ($i = 1; $i <= 5; $i++) {
echo ($i * 2) . ' ';
}
the main reason that your code fails is that the 2 is connected with a dot .
php interprets the 2 as an invalid type as it expects a digit next to it. e.i. 2.0
separating the 2 with a space or in the example with a parenthesis (to explicitly tell the logic) will make the code work
Problem:-2.-> php interprets the 2 as an invalid type because it is expecting a digit next to it something like 2.0. when you add space that simply tell it to apply multiplication first and then add space with it.
1.Either add a space before . like below:-
<?php
for($i = 1; $i<= 5; $i++){
echo $i * 2 .' ';
}
Output:-https://eval.in/856709
2.Or put multiplication into bracket like below:-
<?php
for($i = 1; $i<= 5; $i++){
echo ($i * 2).' ';
}
Output:-https://eval.in/856710
Try this,
for($i = 1; $i<= 5; $i++){
echo ($i * 2).' ';
}

PHP reading database with a variable

I want to read 5 attributes of the database. The 5 attributes have the names post_image_1, post_image_2, post_image_3, post_image_4 and post_image_5. Now I want to show the 5 images on my page, with a for loop.
Here is the loop:
for($i = 1; $i <= 5; $i++){
echo "<img src='image/$row[post_image_$i].png' height='250px' width='250px'>";
}
Now I get an error:
Parse error: syntax error, unexpected '$i' (T_VARIABLE), expecting ']' in
I hope that's enough info to help me. :P
Since you're using arrays, do this:
for($i = 1; $i <= 5; $i++){
echo '<img src="image/'.$row['post_image_'.$i].'png" height="250px" width="250px">';
}
Try this, using string concatenation, it gets at the right field in the array. I'm assuming $row is declared already and contains the keys 'post_image_1', 'post_image_2'...
for($i = 1; $i <= 5; $i++){
echo "<img src='image/" . $row['post_image_' . $i] . ".png' height='250px' width='250px'>";
}

Display random files from a directory?

how to display random files from a directory? The code below only shows random 1 file, output should be 10 files.
<?php
$path = "/files";
$files = scandir($path);
shuffle($files);
for($i = 0; ($i < count($files)) && (!is_file($files[$i])); $i++);
echo $files[$i];
?>
for($i = 0; ($i < count($files)) && (!is_file($files[$i])); $i++);
^
Putting the semicolon there terminates the for loop statement, hence it doesn't actually do anything, and just loops until the condition is false. Remove the semi-colon to fix.
As a clearer example, take the following code:
for($i = 0; $i < 5; $i++);
echo $i;
This will loop five times, as the for loop body statement is blank (due to the statement terminator, the semicolon). Because indentation doesn't matter in PHP, the echo $i will only echo once at the end of the loop, when $i == 5. You can view it better as the following:
for($i = 0; $i < 5; $i++)
;
echo $i;

PHP echo line X times different text

Title might not make complete sense, couldn't think of a way to explain it so I will do a mock PHP example.
<?php
$startNumber = 1;
$endNumber = 15;
$fileExt = '.jpg';
?>
And then on the page it will echo from the $startNumber to the $endNumber in something like a foreach
<?php echo("<img src=\"#.jpg\">"); ?>
Hope this is making sense...
I'd also like to point out that the numbers < 10 start with a 0 so it will need to be in the image source link.
echo is not a function, it's a language construct. Don't use ().
Also you need to use a loop like for() to do that (Also see sidenote of Fred -ii).
$endNumber = 15;
for($i = 1; $i <= $endNumber; $i++) {
if($i < 10) {
$i = "0".$i;
}
echo "<img src=\"".$i.".jpg\" />";
}
If this is not what you asked, add more information.

generating more than one image with a for loop

I am new to the php gd library. I have developed code for generating an image with the gd library and the creation of the image is successful. I have tried to generate more images of same type with a for loop but when i tried to add a button near all the images it didn't fit on one image. It appears like this:
My code
<?php
$num = 5;
for ($j = 1; $j <= $num; $j++) {
echo "<img src=yeah.php /><br><br>";
echo "<button>clickme</button>";
}
?>
I want to fit the button in all the 5 images ..Hope you guys can help me out
Thanks in advance..
<?php
$num = 5;
for ($j = 1; $j <= $num; $j++) {
echo "<img src=yeah.php /><button>clickme</button><br><br>";
}
?>
not sure if it is answer worthy..
this is just a basic html output you output a image then 2 linebreaks then your button
and repeated this process 5 times given the loop that is why you have the current final results.

Categories