Jump to content

make next 10 and previous 10 links?


wmguk

Recommended Posts

hey,

 

I use some code to show images in a directory, on the fly, however if there are say 500 images in the directory, it just shows all 500 (obviously taking some time to show) so I was wondering can I adapt the code to just show 10 at a time, and have next/previous 10 link?

 

 

this is above the page

<?php
$login = $_GET['login'];
$NBFile=0;
$dir ="clients/$login";

if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$FileArray[] = $dir."/".$file;
$NBFile=$NBFile+1;
}}}
closedir($handle);
?>

 

On the page

 

<?php
$NBPicswidth=1;
$NBPics=0;
for ($i=0; $i<$NBFile; $i++
) 
{
$image=$FileArray[$i];

echo "<img src='$image'>";

$NBPics=$NBPics+1;
if ( $NBPics==$NBPicswidth ) 
{ 
$NBPics=0; 
}
}
?>

Link to comment
Share on other sites

 

Thanks for the linky :) I'm gonna have a look at it in a bit, it looks seriously advanced, i was hoping there would be a few lines of code I could add to the existing code i have, rather than a totally new script, ...

 

Just having a glance at the pagnation code, it looks really really heavy.. is it just a case of pasting in to an existing page and it will self generate ?

 

 

Link to comment
Share on other sites

also

 

to use a pagination method call as a function with the total_pages and current page as the parameters

 

e.g. $pagination = pagination_one($total_pages,$page);

 

the only issue is, I wont know how many pages there are, so i wont be able to set this parameter?

Link to comment
Share on other sites

There should be some way to find the number of pages. I think you find the number of images, divide by number of images per page, and round up. To get number of images, maybe use scandir() to get the directories and files in that directory, and loop through them counting the ones that end in a certain extension?

Link to comment
Share on other sites

ok thats cool, :) thank you :)

 

they are all .jpg and always will be so thats excellent thank you. if i can find out how many images are there then might be able to customise the original script or something...

 

this will sound really dumb, but im only now getting really in to php, so please bear with me :)

 

how do you use a function? would you create the function on an include page then call to it otherwise the php will be a nightmare?

Link to comment
Share on other sites

I cannot find anyway to restrict the script to only display 10 or 20 images, ... I was thinking, now i have the counting variable, I can set the the next variable, etc etc....

 

can anyone help on this, as im in over my head i think?

Link to comment
Share on other sites

You must have an array of image filenames to print, right? Just loop through them with a counter variable, incrementing the counter each time, and only print the image if the counter is within a certain range, which depends on what page you're on.

 

$image_counter = 0;
foreach ($image_array as $image_filename) {
     if ($image_counter >= ($page-1)*10 && $image_counter < ($page)*10) {
          echo '<img src="'.$image_filename.'" />';
     }
     $image_counter++;
}

 

Edit: Actually in your original script to display images in the first post of this topic, the $i would work as a counter. So it'd look like this:

 

<?php
$NBPicswidth=1;
$NBPics=0;
for ($i=0; $i<$NBFile; $i++) {
     if ($i >= ($page-1)*10 && $i < ($page)*10) {
          $image=$FileArray[$i];
     
          echo "<img src='$image'>";

          $NBPics=$NBPics+1;
          if  ($NBPics == $NBPicswidth) { 
               $NBPics=0; 
          }
     }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.