Page 1 of 1

pagination

Posted: Wed Apr 18, 2007 2:56 am
by kpraman
I am using the below code to display contents of directory.

Code: Select all

$dir = "on_line_course/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
		    if($file !='.' && $file !='..')
			{
            	echo "filename: $file : filetype: " . filetype($dir . $file);
				echo "<br>";
			}
        }
        closedir($dh);
    }
}

I want to make paging without the use of database, is it possible to do?

Posted: Wed Apr 18, 2007 2:58 am
by aaronhall
Hint: arrays have numbered keys

Posted: Wed Apr 18, 2007 3:49 am
by kpraman
Thanks for replying.

This is the code i tried,

Code: Select all

$count=count($filename);

$num = $_GET['num'];
if(empty($num)){
$num = 1;
};
$limit = 6;

$start = ($num-1)*$limit;
$start = round($start,0);


//$limit=$limit + $start;

$limit_file=$limit=$limit + $start;
echo "start $start";
echo "<br/>";
echo "limit $limit";
echo "<br/>";

for($i=$start;$i<=$limit;$i++)
{
   echo $filename[$i];
   echo "<br/>";
}

$totalpages = ceil($count / $limit);
$totalpages = round($totalpages,0);

echo $totalpages;

$c = 0;
echo "<br>";
while($c<$totalpages){
$page = $c + 1;
if($_GET['num']==$page){
echo "[$page] ";
}else{
echo "<a href=?num=$page class='pagenum'>[$page] </a>";
}
$c = $c+1;
}
The links are giving problems

Posted: Wed Apr 18, 2007 4:04 am
by aaronhall
All HTML element parameters must be enclosed in double quotes

Code: Select all

echo "<a href=\"?num=$page\" class=\"pagenum\">[$page] </a>";

Posted: Wed Apr 18, 2007 4:29 am
by kpraman
the links are displaying correctly. When i click on the links the links shows wrong numbers.

Posted: Wed Apr 18, 2007 4:37 am
by Rovas
I think is because of these

Code: Select all

$limit_file=$limit + $start; 
// you should use
$last_file=$limit_file + $limit;