Log in

View Full Version : pagination set up



hannah sofia
12-14-2010, 11:14 PM
Dear Sir/Madam,

Anyone who can provide me code about pagination w/out using mysql?

I want my files from a certain folder to be shown in my home page.

So far the code I have is this:

<?php

if ($handle = opendir('myFOLDER/')) {
$i = 0;
$allfiles = array();
while(($file = readdir($handle)) ) {
if ($file != "." && $file != "..") {
$allfiles[] = $file ;
}
}
closedir($handle);

rsort( $allfiles ); # sort names in reverse order
while( $i < 2 ){
$to_include = 'myFOLDER/' . $allfiles[$i] ;
include ("$to_include");
$i++ ;
}
}
?>

On that code, I specified "2" files to be included on my homepage and it works fine.

What I really want to achieve is to have a code which can automatically generate a new clickable page links number after showing 2 files on my homepage, or 2 files on every pages generated.

Hope this make sense :)

thank you.

djr33
12-15-2010, 03:44 PM
If you find a tutorial for MySQL, you can apply the same ideas. Basically you need to determine two numbers: 1) The number of pages that exist, let's call that $n; 2) the current page, let's call that $p.

Then what you will do is generate links to pages 1-$n. You can make it fancy by making the links to the current page 'grayed out' so you can't click them (use an if statement). And just use a variable in the url like mypage.php?page=10 to decide which to load.

When loading the pages, check if $_GET['page'] is set. If so, check if it's between 1 and $n. If so, load that. If not, just load the first page.

It can be more complex if you want, but this is a good way to start.

hannah sofia
12-15-2010, 07:10 PM
hi sir,
I dont really know how to apply ur tutorial :) all i know is copy/paste :(

But thankyou for taking time to reply :)

hannah sofia
12-16-2010, 01:39 PM
If you find a tutorial for MySQL, you can apply the same ideas. Basically you need to determine two numbers: 1) The number of pages that exist, let's call that $n; 2) the current page, let's call that $p.

Then what you will do is generate links to pages 1-$n. You can make it fancy by making the links to the current page 'grayed out' so you can't click them (use an if statement). And just use a variable in the url like mypage.php?page=10 to decide which to load.

When loading the pages, check if $_GET['page'] is set. If so, check if it's between 1 and $n. If so, load that. If not, just load the first page.

It can be more complex if you want, but this is a good way to start.


Hello sir,

Iv found a code which I believe very related to what outcome I want on my site, pls help me convert to pagination of my files in a folder named "archives"

the content of the "archives" folder is 1.php, 2.php, 3.php etc



<?php

//The directory to your images folder, with trailing slash
$dir = "images/";

//Set the extensions you want to load, seperate by a comma.
$extensions = "jpeg,jpg";

//Set the number of images you want to display per page
$imagesPerPage = 1;

//Set the $page variable
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}

//Load all images into an array
$images = glob($dir."*.{".$extensions."}", GLOB_BRACE);

//Count the number of images
$totalImages = count($images);

//Get the total pages
$totalPages = ceil($totalImages / $imagesPerPage);

//Make sure the page you are on is not greater then the total pages available.
if($page > $totalPages){
//Set the currnet page to the total pages.
$page = $totalPages;
}

//Now find where to start the loading from
$from = ($page * $imagesPerPage) - $imagesPerPage;

//Now start looping
for($i = $from; $i < ($from + $imagesPerPage); $i++){
//We need to make sure that its within the range of totalImages.
if($i < $totalImages){
//Now we can display the image!
echo "<img src='{$images[$i]}' alt='{$images[$i]}' />";
}
}

//Now to display the page numbers!
for($p = 1; $p <= $totalPages; $p++){
if($p == $page){
$tmp_pages[] = "<strong>{$p}</strong>";
}else{
$tmp_pages[] = "<a href='?page={$p}'>{$p}</a>";
}
}

//Now display pages, seperated by a hyphon.
echo "<br />" . implode(" - ", $tmp_pages);
?>

djr33
12-16-2010, 06:46 PM
You're not going to find something that you can cut and paste for this. If you do need a script that you don't need to edit, you may want to look into paid help.

The script you just posted is close, but it would require significant testing and some changes that can't be predicted (depending on how your server is setup). The ideas are good, but it will be different for your purposes.

hannah sofia
12-17-2010, 11:17 PM
You're not going to find something that you can cut and paste for this. If you do need a script that you don't need to edit, you may want to look into paid help.

The script you just posted is close, but it would require significant testing and some changes that can't be predicted (depending on how your server is setup). The ideas are good, but it will be different for your purposes.


I found this code:


<?php
$perpage = 1;
$total = count($arraywithfiles);
$pages = ceil($total / $perpage);
// here comes some simlpe error checking
$start = !isset($_GET['page']) ? 0 : $_GET['page'];
$url = !isset($_GET['page']) ? $perpage : $_GET['page'] + $perpage;
if ($pages == 1 || $_GET['page'] + $perpage > $total)
$end = $total;
for ($i = $start; $i < $end; $i++)
{
echo "$arraywithfiles[$i] - size - dat<br>";
}
echo "<a href=\"go.php?page=$url.php\">next page</a>";
?>

The function is that it will generate an endless link
http:// mydomain.com/go.php?page=1.php
from page=1.php to unlimited

Anyone who has a kind heart will modify that for me? Wherein I can set a value to limit the number of pages and will show a "previous" link if I reach the last page.

Thankyou in advance!