You are viewing our Forum Archives. To view or take place in current topics click here.

One last hurdle - PHPPosted:

Craig
  • Winter 2018
Status: Offline
Joined: Jan 16, 20127 Year Member
Posts: 19,771
Reputation Power: 10103
Motto: .-. . .--. / -- . / -.. .- -.. -.. -.--
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
  • 2000 Thanks
Status: Offline
Joined: Jun 11, 20099 Year Member
Posts: 9,897
Reputation Power: 3152
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Craig wroteI'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
  • Halloween!
Status: Offline
Joined: Jan 16, 20127 Year Member
Posts: 19,771
Reputation Power: 10103
Motto: .-. . .--. / -- . / -.. .- -.. -.. -.--
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.