Page 1 of 1

Help me with my pagination script / sorting

Posted: Tue Jun 03, 2008 6:00 am
by Sindarin
The following script is one I made up for an archives page. It scans a directory and gets their filenames which displays in a html table sorted by data of upload. Problem was that they put way too many files in it and thus I added a pagination script. Now the problems I have are:

1. The pagination has come to a point the client doesn't like it. It displays like Page: [1] [2] [3] [4] [5] [6] [7] [8] [9] while he would prefer to display like: Page: [First][1] [2] [>>] ... [<<] [8] [9] [Last] or something like it

2. Secondly, a filename will be like interview05.06.2008.zip, how can I get the last 10 characters (05.06.2008 in our case) treat it as date and sort the pagination according to that (newest first)?

Code: Select all

<?PHP
function dirList ($directory, $sortOrder){
 
    //Get each file and add its details to two arrays
    $results = array();
    $handler = opendir($directory);
    while ($file = readdir($handler)) { 
        if ($file != '.' && $file != '..' && $file != "robots.txt" && $file != ".htaccess"){
            $currentModified = filectime($directory."/".$file);
            $file_names[] = $file;
            $file_dates[] = $currentModified;
        }   
    }
       closedir($handler);
 
//clear text file
$fhandle=fopen('archives/entries.txt','w') ;
fwrite($fhandle,"");
fclose($fhandle);
 
    //Sort the date array by preferred order
    if ($sortOrder == "newestFirst"){
        arsort($file_dates);
    }else{
        asort($file_dates);
    }
   
    //Match file_names array to file_dates array
    $file_names_Array = array_keys($file_dates);
    foreach ($file_names_Array as $idx => $name) $name=$file_names[$name];
    $file_dates = array_merge($file_dates);
   
    $i = 0;
 
    //Loop through dates array and then echo the list
    foreach ($file_dates as $file_dates){
        $date = $file_dates;
        $j = $file_names_Array[$i];
        $file = $file_names[$j];
        $i++;
 
//write entries used for pagination
 
$fhandle=fopen('archives/entries.txt','a') ;
fwrite($fhandle,"<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-7' /></head><body>
<left><a href='/archives/upload/$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . "</a></left><br>");
fclose($fhandle);
    }
 
}
 
 
dirlist("E:/Sites/site.com/archives/upload","newestFirst");
 
 
 
//pagination start
$pagenum = 1;
$perpage = 15;
$filename="archives/entries.txt";
 
    $array = file($filename);
    $count = count($array);
    if(!$_GET['page'])
        {
        $page = 1;
    } else {
        $page = $_GET['page'];
    }
 
    for($i=0;$i<=$count;$i++)
    {
        if($array[$i])
        {
            $array2[$i]['info'] = $array[$i];
            if($i2==($perpage-1))
            {
                $array2[$i]['page'] = $pagenum++;
                $i2 = 0;
            }
            else
            {
                $array2[$i]['page'] = $pagenum;
                $i2++;
            }
        }
    }
 
$numpages = $array2[count($array2)-1]['page'];
 
    foreach($array2 as $iarray)
    {
        if($iarray['page'] == $page)
        {
            echo $iarray['info']."<br>\n";
        }
    }echo "<br>Page: ";
 
    for($i3=1;$i3<=$numpages;$i3++)
    {
        if($page == $i3)
 
            echo '['.$i3.']';
        else            
                                      echo '  <a href="'.$PHP_SELF.'?page='.$i3.'">'.$i3.'</a> ';
    }
//pagination end
 
 
?>

Re: Help me with my pagination script / sorting

Posted: Tue Jun 03, 2008 7:14 am
by vargadanis
Getting the last 10 characters is not that hard. All you need to do is find the last occurance of ".".

Code: Select all

$string = "filename05.06.2008.zip";
$dotpos = strrpos($string, ".");
 
/**
* Remove the ext
*/
$search = substr($string, 0, $dotpos); // filename05.06.2008
 
/**
* Get the last 10 characters
*/
$date = substr($search, -10); // 05.06.2008
 
I think you can do the rest by yourself. :)