Results 1 to 2 of 2
  1. #1
    Join Date
    May 2012
    Posts
    1

    Talking Unanswered: add foreach pagination to my script

    hello,

    I need help with to adding pagination to my foreach loop script. Here is my script:

    Code:
    <?php
    
    
    
    
    
    
    function ListFiles($dir) {
    
        if($dh = opendir($dir)) {
    
            $files = Array();
            $inner_files = Array();
    
            while($file = readdir($dh)) {
                if($file != "." && $file != ".." && $file[0] != '.') {
                    if(is_dir($dir . "/" . $file)) {
                        $inner_files = ListFiles($dir . "/" . $file);
                        if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
                    } else {
                        array_push($files, $dir . "/" . $file);
                    }
                }
            }
    
            closedir($dh);
            return $files;
        }
    }
    
    ?>
    
    <?php  
    
    if (isset($_POST['submit1'])) {
    
    
    
    function count_deep($folder, $filetype = "*", $count_folders = false) {
    	$c = 0;
    	$dirs = array($folder);
    	while($dir = each($dirs)) {
    		foreach(glob($dir[1]."/*", GLOB_ONLYDIR) as $filename) {
    			$dirs[] = $filename;
    		}
    		$c += count(glob($dir[1]."/".$filetype));
    	}
    	if(!$count_folders && ($filetype == "*")) $c -= (count($dirs)-1);
    	return $c;
    }
    
    
    
    
    
    
    
    
    
    echo "<table align=\"center\"><tr><td><img src=\"http://www.dbforums.com/images/totalfiles.png\" /></td><td  background=\"http://www.dbforums.com/images/size.png\" align =\"center\"><font size =\" +3\" color=\"#FFFFFF\"><strong>";
    
    
    echo count_deep("backup");
    
     echo "</strong></font></td></tr><tr><td><img src=\"http://www.dbforums.com/images/totalsize.png\" /></td><td  background=\"http://www.dbforums.com/images/size.png\"><font size =\" +3\" color=\"#FFFFFF\"><strong>" ;
     echo $_SESSION['calc'] ; 
     echo "</strong></font></td></tr></table> <br> ";  
    
    
    
    $i = 1;
    
    
    
    
    
    foreach (ListFiles('backup') as $key=>$file){
    	
    	
    		
    	$num= $i++;
    	
    	
    	
    	
    	
    	
    		
    	
        echo "<table width=\"auto\" align=\"center\"><tr><td align=\"center\" width=\"46\" background=\"http://www.dbforums.com/images/num.png\"><font color=\"#FFFFFF\"><strong>$num</strong</font></td><td align=\"left\" width =\"450\" background=\"http://www.dbforums.com/images/downloadbar.png\"><strong>$file</strong></td><td><a href=\"http://xxxxx.com/$file\"><img src=\"http://www.dbforums.com/images/download.png\" /></a></td><td align =\"left\" background=\"http://www.dbforums.com/images/downloadbar.png\"> <strong>(". date ("F d Y H:i:s", filemtime($file))  ;
    	
    	echo ")</strong> </td><td align = \"center\"  background=\"http://www.dbforums.com/images/size.png\" width =\"99\"><font color=\"#FFFFFF\"><strong>" . humanSize(filesize($file)) ;
    	
    	echo "</strong></font></td></tr></table>";
    	
    	$calc = $calc + (filesize($file)) ; 
    	
    }
    	
    	
    }  
    
    
    ?>
    How can I add pagination to directory Listing like:

    1,2,3,4, >> Previous, NEXT >>

    Thanks

  2. #2
    Join Date
    Jun 2012
    Location
    Denver, CO, USA
    Posts
    20
    Probably the best way is to make a form on your page where the links will pass start/length parameters back to your script. Some things you'll need:

    1) A total count of the number of records.
    2) Number of records per page.

    For each link, you should pass back to the server the following POST (or GET) variables:
    1) Num per page (unless you store that permanently somewhere on the server)
    2) Page number

    Then you can use that to find the start/length of data you want to pull.
    ____________________
    Juacala - Web Application Developer, Eliacom, Inc.
    MySQL GUI Tools for Online Applications: Eliacom

Posting Permissions

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