I am using Bootstrap to display pagination on my pages but I have an issue. I want to use the bootstrap active class to show the current page but I don't know how to go about it.
When I add the active class (as shown in the code below), all the links become active. Please what do I do?
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 6;
$sql = "SELECT * FROM offers ORDER BY i.id ASC LIMIT $start_from, 6";
$sql2 = "SELECT COUNT(id) FROM offers";
$rs_result = $db-> query($sql2, array('id' => $_SESSION['id']));
$row = $rs_result->fetch();
$total_records = $row[0];
echo $total_records;
$total_pages = ceil($total_records / 6);
for ($i=1; $i<=$total_pages; $i++) {
;echo"
<ul class='pagination'>
<li class='active' ><a href='myoffer.php?page=$i' >$i</a></li>
</ul>
";
};
?>
You should add a condition that tells which page is currently being displayed (based on the value of the query string 'page', also you should put the echo <ul line before your loop:
echo "<ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
if ($page == $i)
echo "<li class='active' >";
else
echo "<li>";
echo "<a href='myoffer.php?page=$i' >$i</a></li>";
};
echo "</ul>";
This is a copy of Aram Tchekrekjian's answer
($page == $i) ? "<li class='active' >" : "<li>";
Even better than that is to have the ternary operator inside of the <li>
tag and task it with echo class ="active" portion. So it would look like this
<li <?php ($page == $i) ? class="active" : false;?>>1</li>
Related
I want to show a pagination which only shows the recent and next 3 pages, but I can't get it working.
Here's my current code:
$stmt = MySQL::connection3()->prepare("SELECT COUNT(ID) AS TOTAL FROM products");
$stmt->execute();
$row = $stmt->fetch();
$total_pages = ceil(intval($row["TOTAL"]) / $results_per_page);
foreach (range(1, $total_pages) as $i) {
if ($i == $page) {
?>
<li class="page-item active">
<?php echo $i ?>
</li>
<?php
} else {
?>
<li class="page-item">
<?php echo $i ?>
</li>
<?php
}
}
Thanks!
I create this example you can run and adapt with your code:
<style>.active{color:green!important}</style>
<?php
echo get_pagination_links('10','100');
function get_pagination_links($current_page, $total_pages)
{
$links = "";
if ($total_pages >= 1 && $current_page <= $total_pages) {
$i = max(2, $current_page - 3);
for (; $i < min($current_page + 4, $total_pages); $i++) {
if($i==$current_page){
$links .= "<li class='page-item active'><a href='?page=$i;' class='page-link'>$i</a></li>";
}else{
$links .= "<li class='page-item'><a href='?page=$i;' class='page-link'>$i</a></li>";
}
}
return $links;
}
}
?>
Output:
7-8-9-10-11-12-13
obviously I didn't connect a database and therefore I used a static result
I want to be able to add a active class to a pagination so I know which page I am on but am unsure how to add it into the current code I have, below is what I currently have
$per_page=6;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
if ($result = $mysqli->query("SELECT * FROM TABLENAME ORDER BY COLUMNNAME ASC LIMIT $start_from, $per_page"))
// Count the total records
$total_records = mysqli_num_rows($result);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
//Going to first page
echo "<div class='btn-group' style='margin:0 auto;display:table;'>";
echo "<br><br><center><a href='view-all-customers.php?page=1' class='btn btn-primary float-button-
light' style='color:#FFFFFF;padding:6px 15px;'>".'First Page'."</a>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='view-all-customers.php?page=".$i."' class='btn btn-primary float-button-light'
style='color:#FFFFFF;padding:6px 15px;'>".$i."</a>";
};
// Going to last page
echo "<a href='view-all-customers.php?page=$total_pages' class='btn btn-primary float-button-light'
style='color:#FFFFFF;padding:6px 15px;'>".'Last Page'."</a></center>";
echo "</div>";
I have left out the data table rows as thought it's better to show the pagination coding more than showing the table tr and td rows outputting the data
//check page and add active class
for ($i=1; $i<=$total_pages; $i++) {
$isActive = '';
if($i == $page){
$isActive = 'active';
}
echo "<a href='view-all-customers.php?page=".$i."' class='".$isActive."btn btn-primary float-button-light' style='color:#FFFFFF;padding:6px 15px;'>".$i."</a>";
};
I have counted the total results and designed the pagination links. onclicking i am passsing the number value to php file. how to handle $page variable in ajax response. how to show previous 1 2 ... 100 next links like the pagination functionality. I have tried with the following functionality
$page = 1;
$total = 10;
$limit = 20;
$total_pages = ceil($total / $limit);
if (isset($_GET["page"] ))
{
$page = $_GET["page"];
}
else
{
$page=1;
};
<ul class="pagination">
<li class="association_page active">1</li>
<li class="association_page">2</li>
<li class="association_page">3</li>
</ul>
echo "<ul class='pagination'>";
if ($page > 1) {
echo "<li class='association_page'><a href='#' class='button'>Previous</a></li>";
}
for ($i=1; $i<=$total_pages; $i++) {
if($page==$i){
echo "<li class='association_page active'>".$i."</li>";
} else {
echo "<li class='association_page' >".$i."</li>";
}
};
if($page < $total_pages){
echo "<li class='association_page'>NEXT</li>";
}
echo "</ul>";
Onclick i am passing the page number to php file and reponse the results json encoding. How to show
Json Output:
{"page":"8","html_content":"<li class=\"14\"></li>"}
I have not shared full content
Hi i am a new programmer,
i have a PHP & Mysql based pagination, i want to add Ajax functionality to it.
i have gone through many tutorials but i was not able to find any which tells about adding ajax onto existing pagination they all tell about making Ajax based pagination.
i want user be able to paginate even if javascript is turned off. so i want to add some Ajax to my code so that i can be able to paginate with Ajax and PHP.
i can use jquery .load() method to paginate.
please look at my code and suggest me how i can fetch page url for ajax to paginate
i guess something like this has to work. i cant figure out how, please help.
or tell me some tutorial i can learn from.
Jquery Code
$(document).ready(function(){
$('#pagination').click(function(){
$('pageurl').load('is-test2.php #PaginationDiv');});
});
PHP & MySQL Based Pagination
<?php
require_once('_ls-global/php/connection.php');
$db = mysql_select_db($database,$connection) or trigger_error("SQL", E_USER_ERROR);
$sql1 = "SELECT COUNT(*) FROM $table";
$result1 = mysql_query($sql1, $connection) or trigger_error("SQL", E_USER_ERROR);
$row = mysql_fetch_row($result1);
$numrows = $row[0];
$rowsperpage = 2;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
$currentpage = (int) $_GET['page'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql2 = "SELECT * FROM internet_security ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result2 = mysql_query($sql2, $connection) or trigger_error("SQL", E_USER_ERROR);
$list = mysql_fetch_assoc($result2);
$startrow = ($currentpage-1) * $rowsperpage;
Code in html
h3>Results <?php echo ($startrow+1) ?> - <?php echo min($startrow + $rowsperpage, $row) ?> of <?php echo ($totalpages *$rowsperpage) ?></h3>
<ul><?php
if ($currentpage!=$totalpages) {
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$totalpages'>$totalpages</a></li> ";
$nextpage = $currentpage + 1;
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Next»»</a></li> ";
}?></ul>
<ul><?php
if($currentpage<$totalpages){
for ($x = ($currentpage - 3); $x < (($currentpage + 3) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <li id='pcurrent'><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></li>";
} else {
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></li> ";
}}}
}
}
?> </ul>
<ul><?php
if ($currentpage > 1){
$prevpage = $currentpage - 1;
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>««Prev</a></li> ";
echo "<li><a href='{$_SERVER['PHP_SELF']}?page=1'>1</a></li> ";
}?></ul>
since you are getting your page variable into the PHP script with GET method, you can pass the variable like :
$(document).ready(function(){
$('#pagination ul li a').click(function(){
e.preventDefault();
$('#divtoreplace').load($(this).attr("href"));
});
});
hello guys im using this second part of pagination script to show pagination;
<?
if ($pageno == 1) {
echo "<li class='previous-off'>«« İlk Sayfa</li> <li class='previous-off'>« Önceki Sayfa</li> ";
} else {
echo " <li><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=1'>«« İlk Sayfa</a></li> ";
$prevpage = $pageno-1;
echo "<li> <a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$prevpage'>« Önceki Sayfa</a> </li>";
} // if
echo " <li class='active'>$pageno</li>";
if ($pageno == $lastpage) {
echo " <li class='previous-off'>«« Sonraki</li> <li class='previous-off'>« Son Sayfa</li> ";
} else {
$nextpage = $pageno+1;
echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$nextpage'>Sonraki »</a></li> ";
echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$lastpage'>Son Sayfa »»</a></li> ";
} // if
?>
like you see here: echo " <li class='active'>$pageno</li>"; i only can show first, prev, active page, next, last page..
my question is: i want to show more pages near active page.. how can i do this?
i mean pagination style is now like:
FIRST PREV 1 NEXT LAST
i want
FIRST PREV 1 2 3 4 5 6 7 NEXT LAST
thanks
Just add a loop which loops from the first to the last page numbers:
for($page_number = 1; $page_number <= $amount_of_pages; $page_number++)
if($page_number == $pageno)
echo " <li class='active'>$page_number (active)</li>";
else
echo " <li class='active'>$page_number</li>";
To let this work you'd need to find the maximum amount of items and divide it with the amount of items on a page:
$result = mysql_query("SELECT COUNT(*) FROM table");
$row = mysql_fetch_row($result);
$amount_of_items = $row[0];
$amount_of_pages = $amount_of_items / 10; // 10 items on a page
Of course you need to add some checks and stuff, this only shows the basic principles.
Between the part which displays the previous page links and the part which displays the next page links, you need a loop between 1 and total pages.
<?php
$currentPage = 3;
$totalPages = 10;
?>
First page
Prev page
<?php
for ($i = 0; < $totalPages; $i++) {
printf('Page %d', $_SERVER['PHP_SELF'], $i, ($i == $currentPage ? 'active' : ''), $i);
}
?>
Next page
Last page
<?
if ($pageno == 1) {
echo "<li class='previous-off'>«« İlk Sayfa</li> <li class='previous-off'>« Önceki Sayfa</li> ";
} else {
echo " <li><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=1'>«« İlk Sayfa</a></li> ";
$prevpage = $pageno-1;
echo "<li> <a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$prevpage'>« Önceki Sayfa</a> </li>";
} // if
for($page_number = 1; $page_number <= $lastpage; $page_number++)
if($page_number == $pageno) {
echo "<li class='active'>$pageno</li>";
}
else {
echo "<li><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$page_number'>$page_number</a></li>";
}
if ($pageno == $lastpage) {
echo " <li class='previous-off'>«« Sonraki</li> <li class='previous-off'>« Son Sayfa</li> ";
} else {
$nextpage = $pageno+1;
echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$nextpage'>Sonraki »</a></li> ";
echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$lastpage'>Son Sayfa »»</a></li> ";
} // if
?>
this is fixed version. works fine.