Jump to content

Pagination - need help

- - - - -

  • Please log in to reply
2 replies to this topic

#1
amir2000

amir2000

    Newbie

  • Members
  • PipPip
  • 13 posts
Hi All,

I have the following code:
<?php

$files = array_merge(glob("*.jpg"), glob("*.JPG"),

                    glob("*.JPEG"), glob("*.jpeg"));


array_multisort(

    array_map( 'filemtime', $files ),

    SORT_DESC,

    $files

);


//simple foreach loop

foreach($files as $photo) {

   //replace thumbnails to thumbnails directory

   $thumbnail = str_replace('.', '.', $photo);

   echo '<li><a href="'.$photo.'"><img src="'.$thumbnail.'" alt=" " width="120" height="120" /></a></li>';

}

?>

I would like to add pagination to it that it will show certain amount of photos per page.

Thanks in advance,
Amir

#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Pagination? If you want to do that you'd need to count the elements and do a while or for loop to iterate through them, stopping at the end of the page and offsetting based on the page you are on.

<?PHP
//?p=1
$perPage = 20;
$page = (int)$_GET['p']; // 1
$i = ($page-1)*$perPage; //offset = 0
$max = $i+$perPage; // Max = 20

while($i < $count && $i  < $max) {
//code goes here
}


#3
amir2000

amir2000

    Newbie

  • Members
  • PipPip
  • 13 posts
Hi,
Found the way:
 <?php



$files = array_merge(glob("*.jpg"), glob("*.JPG"),

                    glob("*.JPEG"), glob("*.jpeg"));


array_multisort(

    array_map( 'filemtime', $files ),

    SORT_DESC,

    $files

);


array_multisort(array_map('filemtime',$files),SORT_NUMERIC,SORT_DESC,$files); // Sort by modified date


$page = empty($_GET['page']) ? 1 : $_GET['page'];

$num_per_page =16;

$total_pages = ceil(count($files)/$num_per_page);

$break=1;

echo "<div id='gallery'><ul>";

for($i = ($page - 1) * $num_per_page; $i < $page * $num_per_page; $i++) {


if($files[$i] != '')

echo "<li><a href=\"$files[$i]\"><img src=\"$files[$i]\" width=\"120\" height=\"120\" alt=\"$files[$i]\" /></a></li>";

}

echo "</ul></div>";

if($break==6){echo "<br/>";} // Breaks the table into two rows of 6

if($break==12){echo "<br/>";}


$break++;


echo "<br/>";

if ($page == 1)

{

}

else

{

$first = 1;

$prev = $page-1;

echo "<a class='s'  href=\"$self?manage=media&page=$prev\"><img src=\"..\images\lightbox-btn-prev.gif\" alt='previous' /></a> ";

}


if ($page < $total_pages)

{

$next = $page+1;

echo " <a class='s' href=\"$self?manage=media&page=$next\"><img src=\"..\images\lightbox-btn-next.gif\" alt='next' /></a>";

}

else

{

$next = ''; 

$last = ''; 

}


?>

In this way also the rest of the javascript working pefect.

Thanks,
Amir




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users