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' >«« First </a> ";
$prev = $page - 1;
echo " <a href='" . $phpself . "?page=" . $prev . "'> « 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 » </a> ";
echo " <a href='" . $phpself . "?page=" . $total_pages . "' > Last »» </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.