how to custom pagination in php with limited page numbers - php

I use below code for pagination.How to add first,previous,next,last links in pagination.
Only 9 paging numbers should allow in pagination.
EX : First , Previous . ( 9 paging numbers) ,Next ,Last
I have attached image for reference.
<?php
$page = $_GET['url_page'];
$limit =5;
if($page==""){
$page =1;
$start_limit =0;
$end_limit = $page * $limit;
}
else {
$end_limit = $page * $limit;
$start_limit =$end_limit - $limit;
}
$array_count_res = count($result_array);
$choice = ceil($array_count_res /$limit);
$previous_page = $page-1;
$next_page = $page+1;
<?php if($page !=1) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $previous_page; ?>">
Previous</a>
<?php } ?>
<?php for($pa = 0 ;$pa < $choice;$pa++){ ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $pa+1; ?>">
<?php echo $pa+1;echo " "; ?></a>
<?php } ?>
<?php if($page !=$choice) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $next_page; ?>">
Next</a>
<?php } ?>
<?php
for($m=$start_limit;$m < $end_limit;$m++) {
if($result_array[$m]['name'] !="") {
?>

finaly i got answer
<?php
$page = $_GET['url_page'];
$limit =25;
if($page==""){
$page =1;
$start_limit =0;
$end_limit = $page * $limit;
}
else {
$end_limit = $page * $limit;
$start_limit =$end_limit - $limit;
}
$array_count_res = count($contacts);
$choice = ceil($array_count_res /$limit);
$previous_page = $page-1;
$next_page = $page+1;
$first_paging = $page - 2;
$second_paging = $page - 1;
$third_paging = $page + 1;
$four_paging = $page + 2;
$last_page =$choice -$page;
for($m=$start_limit;$m < $end_limit;$m++) {
?>
<tr><td> data</td></tr>
<?php } ?>
<!---pagination starts----->
<tr><td id="importcaontact_page" >
<?php if($page > 3) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/1">
«First</a>
<?php } ?>
<?php if($page !=1) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $previous_page; ?>">
«Previous</a>
<?php } ?>
<?php if($first_paging > 0 ) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $first_paging; ?>">
<?php echo $first_paging; ?></a>
<?php } ?>
<?php if($second_paging > 0 ) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?><?php echo $second_paging; ?>">
<?php echo $second_paging; ?></a>
<?php } ?>
<?php echo $page; ?>
<?php if($third_paging <= $choice ) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $third_paging; ?>">
<?php echo $third_paging; ?></a>
<?php } ?>
<?php if($four_paging <= $choice ) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $four_paging; ?>">
<?php echo $four_paging; ?></a>
<?php } ?>
<?php if($page !=$choice) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $next_page; ?>">
Next »</a>
<?php } ?>
<?php if($last_page > 1) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $choice; ?>">
Last »</a>
<?php } ?>
</td></tr>

Related

Pagination PHP display negative numbers

I'm trying to code pagination and it should show this:
<<1...11>>
but instead it shows this:
<<-5...11>>
This happens if the content less than 11 pages.
this is my code
<?php if($page != 1): ?>
<a href="?page=<?php echo $page-1; ?>&srch-term=<?php echo #$_GET['srch-term']; ?>&tag=<?php echo #$_GET['tag']; ?>&color=<?php echo #$_GET['color']; ?>" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
<?php else: ?>
<span aria-hidden="true">«</span>
<?php endif; ?>
</li>
<?php
$limitPage = 5;
$fromPage = ($page - $limitPage) <= 0 ? 1:$page - $limitPage;
$endPage = ($page + $limitPage) >= $count? $count:$page+$limitPage;
if($page <= ($limitPage)) $endPage = ($limitPage*2) + 1;
if(($page - $limitPage) > ($count - ($limitPage*2))) $fromPage = $count - ($limitPage*2);
?>
<?php for($i=$fromPage;$i<=$endPage;$i++): ?>
<li class="<?php echo ($page == $i)? 'active':''; ?>"><?php echo $i; ?></li>
<?php endfor; ?>
<li class="<?php echo ($page == $count)? 'disabled':''; ?>">
<?php if($page != $count): ?>
<a href="?page=<?php echo $page+1; ?>&srch-term=<?php echo #$_GET['srch-term']; ?>&tag=<?php echo #$_GET['tag']; ?>&color=<?php echo #$_GET['color']; ?>" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
<?php else: ?>
<span aria-hidden="true">»</span>
<?php endif; ?>
Looks like this is the line that is not working the way you expect.
if(($page - $limitPage) > ($count - ($limitPage*2))) $fromPage = $count - ($limitPage*2);
Here is a php sandbox link with some debugging code that should help you out. http://sandbox.onlinephpfunctions.com/code/df2faac69d217ce8e5bea86b63325b8dd4c49600
$limitPage = 5;
$page = 1;
$count = 5;
$fromPage = ($page - $limitPage) <= 0 ? 1:$page - $limitPage;
$endPage = ($page + $limitPage) >= $count? $count:$page+$limitPage;
echo $fromPage . ' ' . $endPage. "\n";
if($page <= ($limitPage)) $endPage = ($limitPage*2) + 1;
echo $fromPage . ' ' . $endPage. "\n";
if(($page - $limitPage) > ($count - ($limitPage*2))) $fromPage = $count - ($limitPage*2);
echo $fromPage . ' ' . $endPage;

PHP Pagination removes search term and reverts back to displaying all results, fix?

After searching for a specific course the results are displayed correctly but once I change page it reverts back to displaying all of the available results. How do I keep the search term results?
Here's the code
<?php
$con = mysqli_connect("localhost", "root", "root", "courses");
// $total = mysqli_query($con, "SELECT count(title) as total")->fetch()['total']);
if(isset($_GET['searchword'])){
$searchword = $_GET['searchword'];
}
else {
$searchword = "";
}
$result = mysqli_query($con, "SELECT count(title) FROM course WHERE title LIKE '$searchword%'");
$row = mysqli_fetch_row($result);
// Total rows.
$rowstotal = $row[0];
//pages
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 100 ? (int)$_GET['per-page'] : 10;
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$pages = $rowstotal / $perPage;
$sql = "SELECT * FROM course WHERE title LIKE '%$searchword%' ORDER BY title ASC LIMIT {$start},{$perPage}";
$query = mysqli_query($con, $sql);
$resultsText = "$rowstotal results";
$resultsList = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$courseId = $row["id"];
$courseAward = $row["award"];
$courseName = $row["title"];
$courseDetails = $row["summary"];
$resultsList .= ''.$courseAward.' '.$courseName.' <br>'.$courseDetails.'<br/>';
}
mysqli_close($con);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Courses</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<nav class="navbar navbar-default navbar-static-top navbar-inverse">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">
<img alt="Napier" src="">
</a>
</div>
</div>
</nav>
<div class="container">
<!-- Search -->
<form method="GET">
<input type="text" class="form-control" name="searchword" placeholder="Search" value="<?php echo $searchword ?>" style="width:30%;"><br>
</form>
<!-- Results -->
<div class="results">
<p></p>
<p>Showing <?php echo $perPage; ?> of <?php echo $resultsText; ?></p>
<p id="content"><?php echo $resultsList; ?></p>
</div>
<div class="pagination">
<?php for($x = 1; $x <= $pages; $x++): ?>
<a href="?page=<?php echo $x; ?>&per-page=<?php echo $perPage; ?>"<?php if($page === $x) { echo ' class="selected"'; } ?>><?php echo $x; ?></a>
<?php endfor; ?>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
</body>
</html>
Your page links don't preserve the searchword parameter. Change this:
<a href="?page=<?php echo $x; ?>&per-page=<?php echo $perPage; ?>"<?php if($page === $x) { echo ' class="selected"'; } ?>><?php echo $x; ?></a>
to this:
<a href="?page=<?php echo $x; ?>&per-page=<?php echo $perPage; ?>&searchword=<?php echo $searchword; ?>"<?php if($page === $x) { echo ' class="selected"'; } ?>><?php echo $x; ?></a>

smart PHP Pagination

I want to split pages to make it more smart and look nice,
I use this code to make Pagination
if (!isset($_GET['page'])){
$page = 1;
}
else {
$page = intval($_GET['page']);
}
$numbershownews = 1;
$x = ($page-1)*$numbershownews;
$select_news_all = $mysqli->query("SELECT id FROM news where FIND_IN_SET('$id_newscats_cat', cats) order by time desc");
$num_news_all = $select_news_all->num_rows;
$cn = $num_news_all/$numbershownews;
$select_news = $mysqli->query("SELECT * FROM news where FIND_IN_SET('$id_newscats_cat', cats) order by time desc limit $x,$numbershownews");
while ($rows_news = $select_news->fetch_array(MYSQL_ASSOC)){
$id_news = $rows_news ['id'];
$title_news = $rows_news ['title'];
}
i show pages by this code
<?
$pagenext = $page+1;
$pageprev = $page-1;
?>
<ul>
<li>
<?
if (($page == 1) or ($page == "")){
?>
<i class="fa fa-angle-double-right"></i>
<?
}else{
?>
<a class="link" href="<? echo "{$newurltitle_newscats_cat}-c-{$id_newscats_cat}-p-{$pageprev}.html"; ?>"><i class="fa fa-angle-double-right"></i></a>
<?
}
?>
</li>
<?php
for($i=0; $i<$cn; $i++){
$pagenumber = $i+1;
if($page == $pagenumber){
echo "<li>{$pagenumber}</li>";
}else{
?>
<li><a class="link" href="<? echo "{$newurltitle_newscats_cat}-c-{$id_newscats_cat}-p-{$pagenumber}.html"; ?>"><? echo $pagenumber; ?></a></li>
<?
}
}
?>
<li>
<?
if($page >= $cn){
?>
<i class="fa fa-angle-double-left"></i>
<?
}else{
?>
<a class="link" href="<? echo "{$newurltitle_newscats_cat}-c-{$id_newscats_cat}-p-{$pagenext}.html"; ?>"><i class="fa fa-angle-double-left"></i></a>
<?
}
?>
it works well but it gives me pages like this 1,2,3,4,5,6,7,8,9,10 i want to split pages like this 1,2,3,4,...,9,10 how can i do this
thanks

php records navigation

<?php
$host='localhost';
$user='root';
$password='root';
$database='database';
$startindex=#$_REQUEST['seek'];
$db=mysql_connect($host, $user, $password)
or die ("Impossibile connettersi al server $host");
mysql_select_db($database, $db)
or die ("Impossibile connettersi al database $database");
$query="SELECT * FROM ordini_master";
$dbResult=mysql_query($query, $db);
$AffectedRows=mysql_affected_rows($db);
mysql_data_seek($dbResult, $startindex);
$row=mysql_fetch_row($dbResult);
foreach($row as $k=>$v)
{
$myfield=mysql_fetch_field($dbResult, $k);
print($myfield->name . " : $v <br/>");
}
mysql_free_result($dbResult);
mysql_close($db);
print("<br/>Seleziona il record<br/>");
for($index=0; $index<$AffectedRows; $index++)
{
print("<a href=\"{$_SERVER['PHP_SELF']}?seek=$index\" >" .
($index+1) . "</a> ");
}
?>
This code allow the navigation between a query records, so it create a page foreach record in database and so shows one record time. How can i modify that code to paging every 10 records? So i want to show 10 records time and create a page for the next.
Sorry for my english (I'm italian) , i hope you can help me.
What you need first is the LIMIT statement from mysql. MySql states:
The LIMIT clause can be used to constrain the number of rows returned
by the SELECT statement. LIMIT takes one or two numeric arguments,
which must both be nonnegative integer constants (except when using
prepared statements).
With two arguments, the first argument specifies the offset of the
first row to return, and the second specifies the maximum number of
rows to return. The offset of the initial row is 0 (not 1):
As for how to implement it in your code I could not have written a better answer as the one found here.
Use LIMIT to get as many records as you wish.
Example:
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
Here is your modified code. This should work for your.
I haven't tested it but give it a try:
<?php
$host='localhost';
$user='root';
$password='root';
$database='database';
$startindex=(isset($_REQUEST['seek']) ? $_REQUEST['seek'] : 0);
$db=mysql_connect($host, $user, $password)
or die ("Impossibile connettersi al server $host");
mysql_select_db($database, $db)
or die ("Impossibile connettersi al database $database");
$queryCnt = "SELECT count(*) as cnt FROM ordini_master";
$CntRow=mysql_fetch_row(mysql_query($query, $db));
$CntData = $CntRow[0];
$step = 10;
$query="SELECT * FROM ordini_master LIMIT $startindex, $step";
$result=mysql_query($query, $db);
while ($row = mysql_fetch_assoc($result)) {
foreach($row as $k=>$v) {
$myfield=mysql_fetch_field($result, $k);
print($myfield->name . " : $v <br/>");
}
}
mysql_free_result($result);
mysql_close($db);
print("<br/>Seleziona il record<br/>");
for($index=0; $index<$CntData; $index=$index+$step) {
print("<a href=\"{$_SERVER['PHP_SELF']}?seek=$index\" >" .
($index+1) . "</a> ");
}
Use the information from tadman too.
It's very important to do something to prevent SQL injection and it's also necessary to use PDO or the mysqli extension because the mysql extension will not longer be supported.
You can use COUNT to display your records and create pages
function getLimitData($start,$limit){
//getting all items
$db = new PDO('mysql:host=localhost; dbname=data','root','');
$results = $db->query("SELECT * FROM table ORDER BY ID DESC limit $start,$limit");
$results = $results->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
//database connection
$db = new PDO('mysql:host=localhost; dbname=data','root','');
//pagination pages
$nav_counter = basename($_SERVER['SCRIPT_FILENAME']);
$page_counter = $nav_counter;
$nav_counter = rtrim($nav_counter, ".php");
$cPage = $page_counter + 1;
//creating next pages
$first_next = $nav_counter + 1 ;
$sec_next = $first_next + 1 ;
$third_next = $sec_next + 1 ;
$fourth_next = $third_next + 1 ;
$fifth_next = $fourth_next + 1 ;
$sixth_next = $fifth_next + 1 ;
$seventh_next = $sixth_next + 1 ;
$next_page = $seventh_next + 1;
//creating previous pages
$first_prev = $nav_counter - 1 ;
$sec_prev = $nav_counter - 2 ;
$third_prev = $nav_counter - 3 ;
$fourth_prev = $nav_counter - 4 ;
$fifth_prev = $nav_counter - 5 ;
$sixth_prev = $nav_counter - 6 ;
$seventh_prev = $nav_counter - 7 ;
//row count
$tableExists = $db->tableExist();
$ROW_COUNT = $db->getRowCount();
$numRows= 9; //number of items to be displayed
//last page
//last page
$last_page = ($ROW_COUNT / $numRows) - 1;
if(!is_int($last_page)){
$last_page = (int)$last_page + 1;
}
$pageNate = '';
if($ROW_COUNT <= $numRows){
$pageNate = 'class="hide"';
}else{
$pageNate = 'class="exist"';
}
//displaying torrents
$start = 0;
$limit = $numRows;
if ($page_counter !== 'index.php') {
$start = ($limit * $nav_counter);
}
//getting number of rows left in the table
$rows_left = $db->getLimitData($start, $limit);
$number_rows = 0;
foreach ($rows_left as $r) {
$number_rows = $number_rows + 1;
}
if ($number_rows < $numRows) {
$limit = $number_rows;
}
$items = $db->getLimitData($start, $limit);
?>
displaying item
<ol>
<?php foreach($items as $item) : ?>
<li><php echo $item['Value']; ?></li>
<?php endforeach; ?>
</ol>
pagination code
<?php
$pages = array();
for ($counter = 1; $counter <= $last_page; $counter++) {
$pages[] = $counter;
}
//storing pages in array and creating a page if it doesn't exist
foreach ($pages as $key) {
$page = $key.'.php';
//if page doesn't exists create page
if(file_exists($page)== false && $key <= $last_page){
copy('index.php', $page);
}
}
?>
<p class="pagenav" >
<a href="index.php" <?php if ($page_counter == 'index.php') {echo 'class="hide"';} ?>><<</a>
<a href="<?php if ($page_counter == '1.php') {echo 'index.php';}else{echo "$first_prev".".php";} ?>" <?php if ($page_counter == 'index.php') {echo 'class="hide"';} ?>><</a>
<a href="<?php echo "$seventh_prev".".php"; ?>" <?php if($seventh_prev <= 0){echo 'class="hide"';} ?>><?php echo $seventh_prev;?></a>
<a href="<?php echo "$sixth_prev".".php"; ?>" <?php if($sixth_prev <= 0){echo 'class="hide"';} ?>><?php echo $sixth_prev;?></a>
<a href="<?php echo "$fifth_prev".".php"; ?>" <?php if($fifth_prev <= 0){echo 'class="hide"';} ?>><?php echo $fifth_prev;?></a>
<a href="<?php echo "$fourth_prev".".php"; ?>" <?php if($fourth_prev <= 0){echo 'class="hide"';} ?>><?php echo $fourth_prev;?></a>
<a href="<?php echo "$third_prev".".php"; ?>" <?php if($third_prev <= 0){echo 'class="hide"';} ?>><?php echo $third_prev;?></a>
<a href="<?php echo "$sec_prev".".php"; ?>" <?php if($sec_prev <= 0){echo 'class="hide"';} ?>><?php echo $sec_prev;?></a>
<a href="<?php echo "$first_prev".".php"; ?>" <?php if($first_prev <= 0 ){echo 'class="hide"';} ?>><?php echo $first_prev;?></a>
<a <?php if ($page_counter == 'index.php') {echo 'class="hide"';}else{ echo 'id="here"';} ?>><?php echo $nav_counter; ?></a>
<a href="<?php echo $first_next.'.php'; ?>" <?php if($first_next <= $last_page){echo 'class="exist"';}else{echo 'class="hide"';} ?>><?php echo $first_next;?></a>
<a href="<?php echo "$sec_next".".php"; ?>" <?php if($sec_next <= $last_page){echo 'class="exist"';}else{echo 'class="hide"';} ?>><?php echo $sec_next;?></a>
<a href="<?php echo "$third_next".".php"; ?>" <?php if($third_next <= $last_page){echo 'class="exist"';}else{echo 'class="hide"';} ?>><?php echo $third_next;?></a>
<a href="<?php echo "$fourth_next".".php"; ?>" <?php if($fourth_next <= $last_page){echo 'class="exist"';}else{echo 'class="hide"';} ?>><?php echo $fourth_next;?></a>
<a href="<?php echo "$fifth_next".".php"; ?>" <?php if($fifth_next <= $last_page){echo 'class="exist"';}else{echo 'class="hide"';} ?>><?php echo $fifth_next;?></a>
<a href="<?php echo "$sixth_next".".php"; ?>" <?php if($sixth_next <= $last_page){echo 'class="exist"';}else{echo 'class="hide"';} ?>><?php echo $sixth_next;?></a>
<a href="<?php echo "$seventh_next".".php"; ?>" <?php if($seventh_next <= $last_page){echo 'class="exist"';}else{echo 'class="hide"';} ?>><?php echo $seventh_next;?></a>
<a href="<?php echo "$first_next".".php"; ?>" <?php if($first_next <= $last_page){echo 'class="exist"';}else{echo 'class="hide"';} ?>>></a>
<a href="<?php echo $last_page.'.php'; ?>" <?php if($nav_counter == $last_page){echo 'class="hide"';}else{echo 'class="exist"';} ?>>>></a>
</p>

Menu Items Not Closing?

I'm coding a staff panel but I'm stuck I've added a menu (navigation) but I'm stuck on how to go about opening it when clicked and closing it when clicked if open.
Here is the code i have so far;
<ul id="menu" class="nav">
<?php
$url = $_GET['url'] ? $core->clean($_GET['url']) : 'core.home';
$query3 = $db->query("SELECT * FROM menu WHERE url = '{$url}'");
$array3 = $db->assoc($query3);
if (!$array3['usergroup']) {
$array3['usergroup'] = "invalid";
}
$query = $db->query("SELECT * FROM usergroups ORDER BY weight ASC");
while ($array = $db->assoc($query)) {
if (in_array($array['id'], $user->data['uGroupArray'])) {
?>
<div class="menustyle" onclick="Radi.menuToggle('<?php echo $array['id']; ?>');">
<div class="menutext"><?php echo $array['name']; ?></div>
</div>
<ul>
<li class="menuitems"<?php if ($array['id'] != $array3['usergroup']) { ?> onclick="Radi.menuToggle('<?php echo $array['id']; ?>');" style="display: none;"<?php } ?> id="mitems_<?php echo $array['id']; ?>">
<?php
$query2 = $db->query("SELECT * FROM menu WHERE usergroup = '{$array['id']}' AND visible = '1' ORDER BY weight ASC");
$i = "a";
while ($array2 = $db->assoc($query2)) {
?>
<li>
<?php echo $array2['text']; ?>
</li>
<?php
$i++;
if ($i == "c") {
$i = "a";
}
}
?>
</ul>
</li>
<?php
}
}
?>
</li>
</ul>
</div>
And this is the defualt code when you download radipanel;
<div style="float: left; width: 200px;">
<?php
$url = $_GET['url'] ? $core->clean($_GET['url']) : 'core.home';
$query3 = $db->query("SELECT * FROM menu WHERE url = '{$url}'");
$array3 = $db->assoc($query3);
if (!$array3['usergroup']) {
$array3['usergroup'] = "invalid";
}
$query = $db->query("SELECT * FROM usergroups ORDER BY weight ASC");
while ($array = $db->assoc($query)) {
if (in_array($array['id'], $user->data['uGroupArray'])) {
?>
<div class="box">
<div class="square menu" style="background: #<?php echo $array['colour']; ?>;" onclick="Radi.menuToggle('<?php echo $array['id']; ?>');">
<img id="menutoggle_<?php echo $array['id']; ?>" class="menutoggle" src="_img/<?php echo ( $array['id'] != $array3['usergroup'] ) ? 'plus' : 'minus'; ?>_white.png" alt="Toggle" align="right" />
<strong><?php echo $array['name']; ?></strong>
</div>
<div class="menuitems"<?php if ($array['id'] != $array3['usergroup']) { ?> style="display: none;"<?php } ?> id="mitems_<?php echo $array['id']; ?>">
<?php
$query2 = $db->query("SELECT * FROM menu WHERE usergroup = '{$array['id']}' ORDER BY weight ASC");
$i = "a";
while ($array2 = $db->assoc($query2)) {
?>
<a href="<?php echo $array2['url']; ?>" class="<?php echo $i; ?>">
<?php echo $array2['text']; ?>
</a>
<?php
$i++;
if ($i == "c") {
$i = "a";
}
}
?>
</div>
</div>
<?php
}
}
?>
</div>
So any ideas on why my code is not doing it?

Categories