Jump to content

Recommended Posts

am trying to get pagitnation working when reading lines from a text file, it works perfect when I set the limit to 1 to when I set the limit to 30 which would cause 30 lines per page only 18 are shown.

 

I have a text file with numbers on each line :

1

2

3

...ect going to 35

 

then the file is read using this code:

 

<?php
$emails = file("emails.txt"); //load all the emails into an array

$limit = 30; //limit to 30 per page

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

//Find out how many results there are
$totalResults = count($emails);

//Get the total pages
$totalPages = ceil($totalResults / $limit);

//If the page you are on is bigger then the totalPages then set the page to the totalPages
if($page > $totalPages)
{
$page = $totalPages;
}

//Find out where to start looping from
$from = ($page * $limit) - $limit;

//Create a temp array
$tmp_emails = array();

//Start looping
for($i = $from; $i < $from + ceil($totalResults / $totalPages); $i++)
{
$tmp_emails[] = $emails[$i];
}

//Implode the temp array, seperating the elements.
echo implode(",<br/> ", $tmp_emails);

echo "<p style=\"clear:both;\" align=\"center\">\n";


// Build Previous Link
		if($_GET['page'] > 1){
			$prev = ($_GET['page'] - 1);
			echo "<a href=\"m.php?page=$prev\">Previous</a>\n ";
		}

		for($i = 1; $i <= $totalPages; $i++){
			if(($_GET['page']) == $i || $page == $i)
			{

				if($totalPages > 1)
				{
					echo "$i\n ";
				}
			} else {
					echo "<a href=\"m.php?page=$i\">$i</a>\n ";
			}
		}

		// Build Next Link
		if($_GET['page'] < $totalPages){
			$next = ($_GET['page'] + 1);
			echo "<a href=\"m.php?page=$next\">Next</a>\n";
		}
		echo "</p>\n"; 
?>

 

not sure where am going wrong if someone could help me with this that would be great.

Link to comment
https://forums.phpfreaks.com/topic/152798-solved-pagination-problem/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.