1.11M Members

Help with pagination page numbers

 
0
 

Hello.

I have made a PHP script that gets news data from a database and i used pagination to split it into page. I have it fully working with page numbers but i would like to know how i would make it only display 10 page numbers at the bottom at any one time. Can anyone help me please. I will post the full code for this.

CODE:

<?php
//Check if in website//
	if (!defined("IN_WEB")) { die("Access Denied"); }

//Includes//
	include('./includes/main.php');

//Get page number from URL//
	if (isset($_GET['page']))
	{
		$page = $_GET['page'];
		$page = mysqli_real_escape_string($myConnection,$page);	
	} else {
		$page = 1;	
	}

//Record count//
	$sqlCommand = "SELECT * FROM news";
	$record_count = mysqli_query($myConnection, $sqlCommand) or die("Error Getting News Count: " . mysqli_error());
	$numrows = mysqli_num_rows($record_count);
	mysqli_free_result($record_count);

//Total Pages//
	$total_pages1 = ($numrows/$per_page);
	$total_pages = round($total_pages1, 0, PHP_ROUND_HALF_UP);

//Check if page number is valid//
	if(($page>$total_pages) && ($page>1))
	{
		$page = 1;
		echo "<META HTTP-EQUIV=\"Refresh\" Content=\"0; URL=" . $phpself . "?page=1\">";    
		exit;
	}

//Set Y as limit value//
	$y = ($page - 1) * $per_page;

//----Build News Block----//

//Get News Block Title//
	$sqlCommand = "SELECT * FROM news_block LIMIT 1";
	$query = mysqli_query($myConnection, $sqlCommand) or die("Error Getting News Block: " . mysqli_error());
	while ($row = mysqli_fetch_array($query)) { 
		$NewsBlockTitle = $row["Block_Title"];
	} 
	mysqli_free_result($query); 

//Echo The Block Title//
	echo "<!-- News Block -->\n";
	echo "<h1>";
	echo $NewsBlockTitle;
	echo "</h1>\n";

//----Get & display News Items----//

//Get News//
	$sqlCommand = "SELECT * FROM news ORDER BY Date DESC LIMIT $y, $per_page"; //$per_page is set in main.php as 10
	$query = mysqli_query($myConnection, $sqlCommand) or die("Error Getting News Items: " . mysqli_error());
	while ($row = mysqli_fetch_array($query)) { 
		$NewsTitle = $row["Title"];
		$NewsID = $row["ID"];
		$NewsAuthID = $row["Author_ID"];

//Get Author Name//
			$sqlCommand1 = "SELECT Name FROM users WHERE ID='$NewsAuthID' LIMIT 1";
			$query1 = mysqli_query($myConnection, $sqlCommand1) or die("Error Getting Author Name: " . mysqli_error());
			while ($row1 = mysqli_fetch_array($query1)) { 
				$NewsAuthorName = $row1["Name"];
			} 
			mysqli_free_result($query1); 
//---------------//
		
		$NewsDate = date("d/m/Y", strtotime($row["Date"]));
		$NewsBody1 = $row["Body"];

//Limit body characters//
		if (strlen($NewsBody1) >= 1000)
		{
			$NewsBody2 .= substr($NewsBody1,0,1000);
			$NewsBody2 .= " ..... <a href=\"news.php?id=" . $NewsID . "\">(Read More)</a>";
		} else {
			$NewsBody2 .= $NewsBody1;	
		}
		
//Get Comment Count//
	$sqlCommand2 = "SELECT News_ID FROM news_comments WHERE News_ID='$NewsID'";
	$query2 = mysqli_query($myConnection, $sqlCommand2) or die("Error Getting Comment Count: " . mysqli_error());
	$CommentsCount = mysqli_num_rows($query2);
	mysqli_free_result($query2); 

//Build News Blocks//	
	$newsBody .= "<div class=\"sub_title\">\n";
	$newsBody .= "<span><a href=\"news.php?id=" . $NewsID . "\">" . $NewsTitle . "</a></span>\n";
	$newsBody .= "<div class=\"divider2\"></div>\n";
	$newsBody .= "<span class=\"span_right\">" . $NewsAuthorName . "</span>\n";
	$newsBody .= "<div class=\"divider2\"></div>\n";
	$newsBody .= "<span class=\"span_right\"> " . $NewsDate ."</span> \n";
	$newsBody .= "<div class=\"divider2\"></div>\n";
	$newsBody .= " <span class=\"span_right\"><a href=\"news.php?id=" . $NewsID . "\">Comments (" . $CommentsCount .")</a></span>\n";
	$newsBody .= "</div>\n";
	$newsBody .= "<div class=\"news_wrapper\">";
	$newsBody .= "<p>";
	$newsBody .= $NewsBody2;
	$newsBody .= "</p>\n";
	$newsBody .= "</div>";
	$newsBody .= "<br />";
} 
mysqli_free_result($query); 

//Echo the news//
	echo $newsBody;
	echo "<br />\n";

//----Page Numbering----//

	if(($numrows - $per_page) < 1)
	{
//If only 1 page of news//
		echo "<center>";
		echo "Page 1 of 1\n";
		echo "<br />";	
		echo "<br />";	
		echo "</center>";	
	} else {
//Build page numbering//
		echo "<center>";
		echo "Page $page of $total_pages\n";
		echo "<br />";
		echo "<table width=\"250\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">\n";
		echo "<tr>\n";
		echo "<td width=\"125\">\n";
//Build first and previous buttons//
		if ($page != 1)
		{
			echo " <a href='" . $phpself . "?page=1'  >&laquo;&laquo; First </a>  ";	
			$prev = $page - 1;
			echo " <a href='" . $phpself . "?page=" . $prev . "'> &laquo; Previous </a> ";
		}
//Build middle divider//
		if (($page != 1) && ($page != $total_pages))
		{
			echo " |";	
		}
	echo "</td>\n";
	echo "<td width=\"125\">\n";
//Build middle divider//
		if (($page != 1) && ($page != $total_pages))
		{
			echo "| ";	
		}
//Build last and next buttons//
		if ($page != $total_pages)
		{
			$next = $page + 1;
			echo " <a href='" . $phpself . "?page=" . $next . "' > Next &raquo; </a> "; 
			echo " <a href='" . $phpself . "?page=" . $total_pages . "' > Last &raquo;&raquo; </a> ";	
		}
	echo "</td>\n";
	echo "</tr>\n";
	echo "</table>\n";
//Build page numbers//
	$i=1;
		for($i=1;$i<=$total_pages;)
		{
			if($page!=$i)
				echo " | <a href='" . $phpself . "?page=" . $i . "'>" . $i. "</a> | ";		
			else
				echo " | <a href='" . $phpself . "?page=" . $i . "'><b>" . $i. "</b></a> | ";
			$i++;
		}
		
			echo "<br />";
			echo "</center>";
		} // End of "if(($numrows - $per_page) < 1)"//
	echo "<!-- End Of News Block -->\n";
?>

As i say, this code fully works.

Thanks in advance.

 
0
 

on pagination you will find number of tutorials on net. Please try to find a tutorials which meets your requirement. If you do not find any tutorials after making deep search in search engine like google. Please let us know.

 
0
 

on pagination you will find number of tutorials on net. Please try to find a tutorials which meets your requirement. If you do not find any tutorials after making deep search in search engine like google. Please let us know.

The reason I posted here is because I couldn't find anything helpful on the net.

 
0
 

Nice Logic

$total_pages = round($total_pages1, 0, PHP_ROUND_HALF_UP);

i think it is better to use ceil() instead of round() here

 
0
 

Thanks meempat. Does anyone have any ides how to go about the page number limiting ?

 
0
 

Any one know any way of doing this?

 
0
 

I hate to have to bump, but its been a while with no reply.

 
0
 

Hi.

I have updated the code slightly but i still have not found anything on my problem.

Updated code:

<?php
//Check if in website//
	if (!defined("IN_WEB")) { die("Access Denied"); }

//Record count//
	$sqlCommand = "SELECT * FROM news";
	$record_count = mysqli_query(ConnectToMYSQLI(), $sqlCommand) or die("Error Getting News Count: " . mysqli_error());
	$numrows = mysqli_num_rows($record_count);
	mysqli_free_result($record_count);

//Total Pages//
	$total_pages = ceil(($numrows/$per_page));
	
//Get page number from URL//
	if (isset($_GET['page']))
	{
		$page = $_GET['page'];
		$page = mysqli_real_escape_string(ConnectToMYSQLI(),$page);	
		
		//Check if page number is valid//
		if(($page>$total_pages) && ($page>1))
		{
			$page = 1;
			echo "<META HTTP-EQUIV=\"Refresh\" Content=\"0; URL=" . $phpself . "?page=1\">";    
			exit;
		}
			
		if($_GET['page'] <1)
		{
			$page = 1;
			echo "<META HTTP-EQUIV=\"Refresh\" Content=\"0; URL=" . $phpself . "?page=1\">";    
			exit;		
		}
			
		if($_GET['page'] == "")
		{
			$page = 1;
			echo "<META HTTP-EQUIV=\"Refresh\" Content=\"0; URL=" . $phpself . "?page=1\">";    
			exit;	
		}
		
	} else {
		$page = 1;	
	}
	

//Set limit value//
	$limit = ($page - 1) * $per_page; //$per_page is set in main.php as 10

//----Build News Block----//

//Get News Block Info//
	$sqlCommand = "SELECT * FROM news_block LIMIT 1";
	$query = mysqli_query(ConnectToMYSQLI(), $sqlCommand) or die("Error Getting News Block: " . mysqli_error());
	while ($row = mysqli_fetch_array($query)) { 
		$NewsBlockTitle = $row["Block_Title"];
	} 
	mysqli_free_result($query); 

//Echo The Block Title//
	echo "<!-- News Block -->\n";
	echo "<h1>";
	echo $NewsBlockTitle;
	echo "</h1>\n";
	echo "<br />\n";

//----Get & display News Items----//

//Get News//
	$sqlCommand = "SELECT * FROM news ORDER BY Date DESC LIMIT $limit, $per_page"; //$per_page is set in main.php as 10
	$query = mysqli_query(ConnectToMYSQLI(), $sqlCommand) or die("Error Getting News Items: " . mysqli_error());
	while ($row = mysqli_fetch_array($query)) { 
		$NewsTitle = $row["Title"];
		$NewsID = $row["ID"];
		$NewsAuthID = $row["Author_ID"];

//Get Author Name//

$NewsAuthorName = GetAuthorNameFunction($NewsAuthID);

//---------------//
		
		$NewsDate = date("d/m/Y", strtotime($row["Date"]));
		$NewsBody1 = $row["Body"];
		$NewsBody1 = wordwrap($NewsBody1, 110, "\r\n", true);
//Limit body characters//
		if (strlen($NewsBody1) >= 1000)
		{
			$NewsBody2 = substr($NewsBody1,0,1000);
			$NewsBody2 .= " ..... <a href=\"news.php?id=" . $NewsID . "\">(Read More)</a>";
		} else {
			$NewsBody2 = $NewsBody1;
		}
		
//Get Comment Count//
	$sqlCommand2 = "SELECT News_ID FROM news_comments WHERE News_ID='$NewsID'";
	$query2 = mysqli_query(ConnectToMYSQLI(), $sqlCommand2) or die("Error Getting Comment Count: " . mysqli_error());
	$CommentsCount = mysqli_num_rows($query2);
	mysqli_free_result($query2); 

//Build News Blocks//	
	$newsBody .= "<div class=\"sub_title\">\n";
	$newsBody .= "<span><a href=\"news.php?id=" . $NewsID . "\">" . $NewsTitle . "</a></span>\n";
	$newsBody .= "<div class=\"divider2\"></div>\n";
	$newsBody .= "<span class=\"span_right\">" . $NewsAuthorName . "</span>\n";
	$newsBody .= "<div class=\"divider2\"></div>\n";
	$newsBody .= "<span class=\"span_right\"> " . $NewsDate ."</span> \n";
	$newsBody .= "<div class=\"divider2\"></div>\n";
	$newsBody .= " <span class=\"span_right\"><a href=\"news.php?id=" . $NewsID . "#comments\">Comments (" . $CommentsCount .")</a></span>\n";
	$newsBody .= "</div>\n";
	$newsBody .= "<div class=\"news_wrapper\">";
	$newsBody .= "<p>";
	$newsBody .= $NewsBody2;
	$newsBody .= "</p>\n";
	$newsBody .= "</div>";
	$newsBody .= "<br />";
} 
mysqli_free_result($query); 

//Echo the news//
	echo $newsBody;
	echo "<br />\n";

//----Page Numbering----//

	if(($numrows - $per_page) < 1)
	{
//If only 1 page of news//
		echo "<center>";
		echo "Page 1 of 1\n";
		echo "<br />";	
		echo "<br />";	
		echo "</center>";	
	} else {
//Build page numbering//
		echo "<center>";
		echo "Page $page of $total_pages\n";
		echo "<br />";
		echo "<table width=\"250\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">\n";
		echo "<tr>\n";
		echo "<td width=\"125\">\n";
//Build first and previous buttons//
		if ($page != 1)
		{
			echo " <a href='" . $phpself . "?page=1'  >&laquo;&laquo; First </a>  ";	
			$prev = $page - 1;
			echo " <a href='" . $phpself . "?page=" . $prev . "'> &laquo; Previous </a> ";
		}
//Build middle divider//
		if (($page != 1) && ($page != $total_pages))
		{
			echo " |";	
		}
	echo "</td>\n";
	echo "<td width=\"125\">\n";
//Build middle divider//
		if (($page != 1) && ($page != $total_pages))
		{
			echo "| ";	
		}
//Build last and next buttons//
		if ($page != $total_pages)
		{
			$next = $page + 1;
			echo " <a href='" . $phpself . "?page=" . $next . "' > Next &raquo; </a> "; 
			echo " <a href='" . $phpself . "?page=" . $total_pages . "' > Last &raquo;&raquo; </a> ";	
		}
	echo "</td>\n";
	echo "</tr>\n";
	echo "</table>\n";
//Build page numbers//
	$i=1;
		for($i>=1;$i<=$total_pages;$i++)
		{
				if($page!=$i)
				{
					echo " | <a href='" . $phpself . "?page=" . $i . "'>" . $i. "</a> | ";	
				}
				else
				{
					echo " | <a href='" . $phpself . "?page=" . $i . "'><u><b>" . $i. "</b></u></a> | ";
				}

		}
		
			echo "<br />";
			echo "</center>";
} // End of "if(($numrows - $per_page) < 1)"//
	echo "<!-- End Of News Block -->\n";
?>

If anyone can offer any help i would be very happy as i have been looking for ages.

Thanks.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: