You are viewing our Forum Archives. To view or take place in current topics click here.
One last hurdle - PHP
Posted:

One last hurdle - PHPPosted:

Craig
  • Spooky Poster
Status: Offline
Joined: Jan 16, 201212Year Member
Posts: 20,271
Reputation Power: 17065
Motto: 2b || !2b
Motto: 2b || !2b
Status: Offline
Joined: Jan 16, 201212Year Member
Posts: 20,271
Reputation Power: 17065
Motto: 2b || !2b
So, I've been racking my brain for days now, and am now seeking help for the final bit of this page. I have a self indexing PHP files that embeds images into a web page, that is updated automatically very regularly so now i need to limit the amount of items per page.

// Main dir and sorting functions       
$dir = "ss/";       
    chdir($dir);   
    array_multisort(array_map('filemtime', ($files = glob('*.jpg', GLOB_BRACE))), SORT_DESC, $files);
    foreach($files as $filename)   
{

// Encode the URL         
$unencodedUrl = $filename;
$encodedUrl = rawurlencode($unencodedUrl);


// Display on screen     
     echo '<li><a href="ss/' . $encodedUrl . '"><img class="img" src="ss/' . $encodedUrl . '" alt="Image" /><span>' . $filename . '</span>';


I'm seriously lost on this one as i can add pages, but it ruins the rest of the code. And I have no idea why. So any help would be greatly appreciated.

Another option would be to limit the amount of files it scans for inside the directory. Would there be a simple way of telling it to only scan for the 50 most recent files in the said directory?
#2. Posted:
speed
  • Winter 2022
Status: Offline
Joined: Jun 11, 200914Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Status: Offline
Joined: Jun 11, 200914Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Craig wrote I'm seriously lost on this one as i can add pages, but it ruins the rest of the code. And I have no idea why. So any help would be greatly appreciated.


I'm a bit confused what you mean by this part. What do you mean by "add pages?"

It sounds like you're just trying to display the X most recent images, right? If so, you should be using a for loop rather than a foreach loop.

You've already got the images sorted, so just loop from 0 to whatever number you want to display and use that to index the sorted array.

Obviously you'll want to check to make sure that $files is at least as long as the number you want to display.

pseudocode:

$display = 50;
if(count($files) < $display){
  $display = count($files);
}
for($i = 0; $i < $display; $i++){
  $unencodedUrl = $files[$i];
  $encodedUrl = rawurlencode($unencodedUrl);

  echo ....
}
#3. Posted:
Craig
  • Winter 2022
Status: Offline
Joined: Jan 16, 201212Year Member
Posts: 20,271
Reputation Power: 17065
Motto: 2b || !2b
Motto: 2b || !2b
Status: Offline
Joined: Jan 16, 201212Year Member
Posts: 20,271
Reputation Power: 17065
Motto: 2b || !2b
Lol I wasn't even close. But thank you, again.

And yeah, reading that back even I can't understand what i meant.

But what you provided worked a treat, so thank you for that.

Feel free to close!
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.