SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist sitedesigner's Avatar
    Join Date
    May 2005
    Location
    On the Computer
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Simple Page Pagination?

    I want to add pagination to this.


    PHP Code:
    <?

    $dh 
    opendir$dir );                                           
          while( 
    $filename readdir$dh ) ) {                             
               
    $filepath $dir.$filename;                                  
               if( 
    is_file$filepath ) and ereg"\.jpg$"$filename )  || ereg"\.gif$"$filename ) || ereg"\.png$"$filename ) ) { 
                    
    $gallery[] = $filepath;
               }
                          
    function 
    DateCmp($a$b) {
          if(
    $a[1]==$b[1]) return 0;
          else return (
    $a[1] < $b[1]) ? -1;
    }

    function 
    SortByDate(&$Files) {
        
    usort($Files'DateCmp');
    }

    SortByDate(array_reverse($gallery));    
          
    foreach( 
    $gallery as $image ) {
               echo 
    "<li><a href='$image'><img src='$image' alt='$cat$count' /></a></li>";
            }

    ?>

    Thanks!
    █ [o] Programmer? PM me!
    █ [o] Getting back into the Business
    █ [o] Sitedesigner

  2. #2
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sitedesigner View Post
    I want to add pagination to this.


    PHP Code:
    <?

    $dh 
    opendir$dir );                                           
          while( 
    $filename readdir$dh ) ) {                             
               
    $filepath $dir.$filename;                                  
               if( 
    is_file$filepath ) and ereg"\.jpg$"$filename )  || ereg"\.gif$"$filename ) || ereg"\.png$"$filename ) ) { 
                    
    $gallery[] = $filepath;
               }
                          
    function 
    DateCmp($a$b) {
          if(
    $a[1]==$b[1]) return 0;
          else return (
    $a[1] < $b[1]) ? -1;
    }

    function 
    SortByDate(&$Files) {
        
    usort($Files'DateCmp');
    }

    SortByDate(array_reverse($gallery));    
          
    foreach( 
    $gallery as $image ) {
               echo 
    "<li><a href='$image'><img src='$image' alt='$cat$count' /></a></li>";
            }

    ?>

    Thanks!
    Okay, what have you tried?
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  3. #3
    SitePoint Evangelist sitedesigner's Avatar
    Join Date
    May 2005
    Location
    On the Computer
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't tried anything yet, because I don't even have a clue how to do it
    █ [o] Programmer? PM me!
    █ [o] Getting back into the Business
    █ [o] Sitedesigner

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about something like this:
    Code php:
    $gallery = array();
    $dir = $_SERVER['DOCUMENT_ROOT']."/test/images/";
    $dh = opendir( $dir );                                           
    while( $filename = readdir( $dh ) ) {                             
    	$filepath = $dir.$filename;                                  
    	if( is_file( $filepath ) and (ereg( "\.jpg$", $filename )  || ereg( "\.gif$", $filename ) || ereg( "\.png$", $filename )) ) { 
    		$gallery[] = $filepath;
    	}
    }
     
    function DateCmp($a, $b) {
    	if($a[1]==$b[1]) return 0;
    	else return ($a[1] < $b[1]) ? -1 : 1;
    }
    function SortByDate(&$Files) {
    	usort($Files, 'DateCmp');
    }
    SortByDate(array_reverse($gallery)); 
     
    $perpage = 5;
    $total = count($gallery);
    $start = isset($_GET['start']) ? $_GET['start'] : 0;
     
    for($i = $start; $i < $current+$perpage; $i++){   
    	#echo "<li><a href=\"{$gallery[$i]}\"><img src=\"{$gallery[$i]}\" alt=\"$cat$count\" /></a></li>";
    	echo $image."<br>";
    }
    echo "<br><br>";

    I assume you can manage the paging link at the bottom.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Evangelist sitedesigner's Avatar
    Join Date
    May 2005
    Location
    On the Computer
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is the paging link within the for loop? or after the for loop?
    █ [o] Programmer? PM me!
    █ [o] Getting back into the Business
    █ [o] Sitedesigner

  6. #6
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sitedesigner View Post
    is the paging link within the for loop? or after the for loop?
    Think about it.

    Do you want the paging link displayed multiple times (inside the loop) or just once (outside the loop)??
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  7. #7
    SitePoint Evangelist sitedesigner's Avatar
    Join Date
    May 2005
    Location
    On the Computer
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so here's the pagination that i came up with. Couple things

    Say the array as 14 images in it.
    the script still goes to the 4th page, even though there arent any images on that page. any help as to why?

    Thanks!

    PHP Code:
    <?php

    $numperpage 
    5// number of results per page

    if (isset($_GET['page'])) {
      
    $page intval($_GET['page']) - 1;
    } else {
      
    $page 0;
    }

                
    $dir 'icon/animals/';
                
                <
    ul>
                      <?
        if(
    $nodir==1) { include 'favourites.php'; } else {
          
    $dh opendir$dir );                                           
          while( 
    $filename readdir$dh ) ) {                             
               
    $filepath $dir.$filename;                                  
               if( 
    is_file$filepath ) and ereg"\.jpg$"$filename )  || ereg"\.gif$"$filename ) || ereg"\.png$"$filename ) ) { 
                    
    $gallery[] = $filepath;
               }
          }
          
    //-find the date of each file
    function DateCmp($a$b) {
          if(
    $a[1]==$b[1]) return 0;
          else return (
    $a[1] < $b[1]) ? -1;
        }
    // sort each array
    function SortByDate(&$Files) {
        
    usort($Files'DateCmp');
        }
    // reverse the array to get in correct order
    SortByDate(array_reverse($gallery));    
        

      

          for (
    $i = ($page $numperpage); ($i < (($page $numperpage) + $numperpage) && $i count($gallery)); $i++         ) {
               
    $image $gallery[$i];
               echo 
    "<li><a href='$image'><img src='$image' alt='$cat$count' /></a></li>";
        }
    $back $page;
    $next $page 2;
    $display $page 1;
    echo 
    "<p>page $display - (Gallery: " count($gallery) . " images) - ";



    if ( 
    $page == && ($numperpage count($gallery)) )
    echo 
    "<a href=?page=$next>next </a>";
    else if ( 
    count($gallery) <= ($page $numperpage) )
    echo 
    "<a href=?page=$back> back</a>";
    else if ( 
    count($gallery)%($page $numperpage) > )
    echo 
    "<a href=?page=$back> back</a> | <a href=?page=$next>next </a>";
    else 
    echo 
    "Page Error";
    █ [o] Programmer? PM me!
    █ [o] Getting back into the Business
    █ [o] Sitedesigner

  8. #8
    SitePoint Evangelist sitedesigner's Avatar
    Join Date
    May 2005
    Location
    On the Computer
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone help? I really tried my best here
    █ [o] Programmer? PM me!
    █ [o] Getting back into the Business
    █ [o] Sitedesigner


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •