How do active class on pagination - php

I'm trying to active class on list
Kindly if any one can help me to add class="active" on displayed pagination page:
$perpage= $conf['perpage'];
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
};
$start_from = ($page-1) * $perpage;
$result = "SELECT * FROM topics LIMIT $start_from, $perpage";
$result = mysql_query ($result);
$n = 0;
while ($row = mysql_fetch_array ($result)){
echo '<tr>';
echo '<td>'.$row['topic_no'].'</td>';
echo '</tr>';
++$n;
}
$sql = "SELECT * FROM topics";
$result = mysql_query($sql);
$total_records = mysql_num_rows($result);
$total_pages = ceil($total_records / $perpage);
echo '<ul class="pagination">';
echo "<li><a href='topics.php?page=1'>".'<'."</a></li> ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<li ><a href='topics.php?page=".$i."'>".$i."</a></li> ";
};
echo "<li><a href='topics.php?page=$total_pages'>".'>'."</a> </li>";
echo '</ul> ';
Thanks in Advance

You Can also try this code. it's working
$c="active";
for ($i=1; $i <$total_page ; $i++) {
if($page==$i)
{
$c="active";
}
else
{
$c="";
}
echo "<li class=\"$c\">$i</li>";
}

$active = $i == $page ? 'class="active"' : '';
echo "<li ><a {$active} href=\"topics.php?page={$i}\">{$i}</a></li> ";
We just need to add class="active" only if page is current, otherwise we add nothing. If you already have classes for rows - you just need to use smth like
$activeClass = $i == $page ? 'active' : '';
echo "<li ><a class=\"my-row-class1 {$activeClass}\" href=\"topics.php?page={$i}\">{$i}</a></li> ";

echo "<li if($_GET['page']==$i){ class='active'}><a href='topics.php?page=".$i."'>".$i."</a></li> ";
Please try this. It might help you

$search = #$_GET['page']; // get value form other page
$page ='A';
for ($Page=1; $Page <27 ; $Page++) { ?> // abcdef... create
<li class="<?php if($search==$page){ echo 'active'; } ?>"> <?php echo ''. $page++ . '' ;?> </li>
<?php } ?>
/* if($search==$page){ echo 'active'; } this code means if search value == alphabet then class active call automatically */

Related

Limiting bootstrap pagination in PHP to 5 pages

I'm trying to create pagination in PHP where 5 pages are displayed.
When on the first page we have:
[1][2][3][4][5] ...[325][>>]
Clicking on [325] will take you to that page (the last record), clicking on the right arrow will take you to page [2].
When on the second page we have:
[<<][1]...[2][3][4][5][6] ...[325][>>]
And when on the last page we have:
[<<][1]...[321][322][323][324][325]
I've been researching on how to do this without much luck. I think I understand that I need to create an array with adjacent pages of 2 on each side of the active page, when we are on any page except the first or last page. I also need to create an <li>1</li>
and
<li><?php echo $last_record; ?></li>
for when a user is anywhere but the first or last record.
I have the following code which works great, however when we start getting a large number of records, the pagination count gets ridiculous.
<ul class="pagination pull-left pagination-md">
<?php
// Creates back button in pagination
if(isset($page)) {
if($page > 1) {
$page_minus = $page-1;
echo "<li><a href='blog.php?page=$page_minus'> « </a></li>";
}
}
?>
<?php
global $con;
$q_pagination = mysqli_prepare($con, "SELECT COUNT(*) FROM admin_panel WHERE ");
$q_pagination->execute();
$result_pagination = $q_pagination->get_result();
$rows_result = $result_pagination->fetch_array();
$total_rows = array_shift($rows_result);
$post_per_page = $total_rows/15;
$post_per_page = ceil($post_per_page);
for($i = 1; $i <= $post_per_page; $i++) {
if(isset($page)){
if($i == $page) {
echo "<li class='active'><a href='blog.php?page=$i'>$i</a></li>";
}
else {
echo "<li><a href='blog.php?page=$i'>$i</a></li>";
}
}
}
// Creates the forward button in pagination
if(isset($page)){
if($page+1 <= $post_per_page) {
$page_plus = $page+1;
echo "<li><a href='blog.php?page=$page_plus'> » </a></li>";
}
}
?>
</ul>
I'll admit, after researching and attempting to make this work I'm just getting twisted in the logic. Does anyone have any thoughts on how to best approach this? Leads, current examples, etc. Most of what I've found is dated, incomplete, or stupid long.
<?php
session_start();
include "mysqli_connect.php";
$companyID = $_SESSION['compid'];
$db = new Database();
$dbc = $db->getConnection();
$display = 3; //number of records per page
$pages;
$dbb = new Database();
$dbcb = $dbb->getConnection();
$stat = "select * from reservationStatus where resStatId = '$companyID' ";
$r = mysqli_query($dbcb, $stat);
//variable for sorting - default is for registration date
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'rd';
switch ($sort)
{
case 'ln':
$orderby = 'uniquenumber ASC';
break;
case 'fn':
$orderby = 'status ASC';
break;
case 'rd':
$orderby = 'resId ASC';
break;
case 'em' :
$orderby = 'resDate ASC';
break;
default:
$orderby = 'resId ASC';
break;
}
if(isset($_GET['p']) ) //already calculated
{
$pages=$_GET['p'];
}
else
{
//get the total number of records from the table
$q = "select count(resId) from reservation where companyID = '$companyID'";
$r = mysqli_query($dbc, $q);
$row = mysqli_fetch_array($r, MYSQLI_NUM);
$records=$row[0];
if($records > $display ) //$display is the number of records per page
//ceil rounds fractions up to integer value
$pages=ceil($records/$display);
else
$pages = 1;
}
//now determine where in the database to start
if(isset($_GET['s']) ) //already calculated
$start=$_GET['s'];
else
$start = 0;
//$q = "select * from users LIMIT $start, $display";
$q = "select * from reservation where companyID = '$companyID' order by $orderby LIMIT $start, $display";
$r = mysqli_query($dbc, $q);
if($r)
{
echo '<br />';
//display a table of results
echo '<div class="container">';
echo '<h1> Your Orders </h1>';
echo '<table align="center" class="table table-bordered table-striped" width="60%">';
echo '<tr bgcolor="#87CEEB">
<td><b>View</b></td>
<td><b>Change Status</b></td>
<td><b> Reference Number</b></td>
<td><b>Status</b></td>
<td><b>Date</b></td>
<td><b>Total Price</b></td></tr>';
//above is the header
//loop below adds the user details
//use the following to set alternate backgrounds
$bg = '#eeeeee';
while($row = mysqli_fetch_array($r))
{
$stat = "select * from reservationStatus where resStatusId = $row[7] ";
$rr = mysqli_query($dbcb, $stat);
$roww = mysqli_fetch_array($rr);
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
echo '<tr bgcolor="' . $bg. '">
<td>View</td>
<td>Change Status </td>
<td>'.$row[2].'</td>
<td>'.$roww[1].'</td>
<td>'.$row[1]. ' ' . $row[8].'</td>
<td>'.$row[3].'</td>
</tr>';
}
echo '</table></div>';
}
else
{
echo '<p class="error">' . $q . '</p>';
echo '<p class="error"> Oh dear. There was an error</p>';
echo '<p class="error">' . mysqli_error($dbc) .'</p>';
}
mysqli_free_result($r);
//makes links to other pages if required
if($pages > 1)
{
echo '<br /><p> ' ;
//find out what page we are on
$currentpage = ($start/$display)+1;
//need a previous button if not first page
if($currentpage != 1)
{
echo ' <a href="viewOrdersForCompanies.php?$s=' . ($start - $display) .
'&p=' .$pages . '&sort='.$sort.'">&nbspPrevious&nbsp</a>';
}
//create the numbered pages
for($i = 1; $i <= $pages; $i++)
{
if($i != $currentpage)
{
//the 's' paramater is used in the link to determine which the value
// in the LIMIT clause used in the select statement near the top of the page
echo '<a href="viewOrdersForCompanies.php?s=' . (($display * ($i-1))) . '&p='
. $pages . '&sort='.$sort.'">&nbsp' . $i . '&nbsp</a>';
}
//&nbsp is a character to insert a whitespace
}
//if not last page create next button
if($currentpage != $pages)
{
echo '<a href="viewOrdersForCompanies.php?s=' . ($start+$display) . '&p=' . $pages
. '&sort='.$sort.'">&nbspNext&nbsp</a>';
}
echo '</p>';
}
?>

Pagination with Jquery MySQLi?

how to convert MySQL to MySQLi with PHP and jQuery.
If I run the file below using MySQL work but I want to use MySQLi, how to change it?
cek my file:
pagination_class.php
<?php class Pagination_class
{
var $result;
var $anchors;
var $total;
function Pagination_class($qry,$starting,$recpage)
{
$rst = mysql_query($qry) or die(mysql_error());
$numrows = mysql_num_rows($rst);
$qry .= " limit $starting, $recpage";
$this->result = mysql_query($qry) or die(mysql_error());
$next = $starting+$recpage;
$var = ((intval($numrows/$recpage))-1)*$recpage;
$page_showing = intval($starting/$recpage)+1;
$total_page = ceil($numrows/$recpage);
if($numrows % $recpage != 0)
{
$last = ((intval($numrows/$recpage)))*$recpage;
}
else
{
$last = ((intval($numrows/$recpage))-1)*$recpage;
}
$previous = $starting-$recpage;
$anc = "<ul class='pagination m_bottom_0 f_top_5'>";
if($previous < 0)
{
$anc .= "<li class='disabled'>
<a href='javascript:;'>
<i class='mdw-icon fa fa-angle-left'></i>
</a>
</li>";
}
else
{
$anc .= "<li>
<a href='javascript:pagination($previous);'>
<i class='mdw-icon fa fa-angle-left'></i>
</a>
</li>";
}
$norepeat = 2;
$j = 2;
$anch = "";
for($i=$page_showing; $i>1; $i--)
{
$fpreviousPage = $i-1;
$page = ceil($fpreviousPage*$recpage)-$recpage;
$anch = "<li><a href='javascript:pagination($page);'>$fpreviousPage </a></li>".$anch;
if($j == $norepeat) break;
$j++;
}
$anc .= $anch;
$anc .= "<li class='active'><a href='javascript:;'>".$page_showing."</a></li>";
$j = 1;
for($i=$page_showing; $i<$total_page; $i++)
{
$fnextPage = $i+1;
$page = ceil($fnextPage*$recpage)-$recpage;
$anc .= "<li><a href='javascript:pagination($page);'>$fnextPage</a></li>";
if($j==$norepeat) break;
$j++;
}
if($next >= $numrows)
{
$anc .= "<li class='disabled'>
<a href='javascript:;'>
<i class='mdw-icon fa fa-angle-right'></i>
</a>
</li>";
}
else
{
$anc .= "<li>
<a href='javascript:pagination($next);'>
<i class='mdw-icon fa fa-angle-right'></i>
</a>
</li>";
}
$anc .= "</ul>";
$this->anchors = $anc;
$this->total = "<div>Halaman : <strong>$page_showing</strong> dari <strong>$total_page</strong>. Total : <strong>$numrows</strong> records</div>";
}
} ?>
and below file berita_view
please check in pastebin
Thanks.

unable to display :active page in my pagination function

am using following function to display pagination
public function paginationLinks(){
$outputString = "";
$q = $this->db->query('SELECT COUNT(*) FROM videos');
$res = $q->fetch();
$newsNumber = $res[0];
$q->closeCursor();
for($i = 1; $i <= ceil($newsNumber / $this->newsByPage); $i++){
$outputString .="<li><a href='?page=" . $i . "'>" . $i . "</a></li> ";
}
return $outputString;
}
and this is how i display
<ul class="pagination pagination-sm">
<?php echo $news->paginationLinks(); ?>
</ul>
now bootstrap doesn't display active page its because of function
how do i add some more option like next, previous and :active
just replace your function with this one
public function paginationLinks(){
$outputString = "";
$crpage = isset($_GET['page']) && trim($_GET['page']) != ''?trim($_GET['page']):1;
$q = $this->db->query('SELECT COUNT(*) FROM videos');
$res = $q->fetch();
$newsNumber = $res[0];
$q->closeCursor();
for($i = 1; $i <= ceil($newsNumber / $this->newsByPage); $i++){
if($crpage == $i){
$outputString .="<li class='active'><a href='?page=" . $i . "'>" . $i . "</a></li> ";
}else{
$outputString .="<li><a href='?page=" . $i . "'>" . $i . "</a></li> ";
}
}
return $outputString;
}
For next or previous see that link
great example for PHP pagination.
Please try this .I hope this help you
public function paginationLinks(){
$num_rec_per_page = 10;
$outputString = "";
$page = (isset($_GET['page']) && $_GET['page'] != '') ? trim($_GET['page']) : 1;
$start_from = ($page-1) * $num_rec_per_page;
$q = $this->db->query('SELECT COUNT(*) FROM videos');
$res = $q->fetch();
$total_records = $res[0];
$q->closeCursor();
$total_pages = ceil($total_records / $num_rec_per_page);
$outputString .= "<li><a href='?page=1'>".'|<'."</a></li>"; // Goto 1st page
if($page > 1){
$prev = $page - 1;
$outputString .= "<li><a href='?page=".$prev."'>Prev</a></li>"; // Goto previous page
}
for ($i=1; $i<=$total_pages; $i++) {
$activeClass = ($page == $i) ? 'active' : '';
$outputString .="<li class=".$activeClass."><a href='?page=" . $i . "'>" . $i . "</a></li> ";
}
if($page < $total_pages){
$next = $page + 1;
$outputString .= "<li><a href='?page=".$next."'>Next</a></li>"; // Goto Next page
}
if($page > 1){
$outputString .= "<li><a href='?page=".$total_pages."'>".'|>'."</a></li>"; // Goto last page
}
return $outputString;
}

Using Pagination on product view using Category ID

I feel I have asked alot of questions recently and Im sorry if this is relatively simple but I am struggling to get this working correctly.
I want to add pagination to my product view but when I add the pagination numbers code as seen at the bottom of the page they do not show.
I'm also aware that I should be using mysqli but I want to get this working first before moving over.
Thanks.
Show Product
<div class="link" style="width:100%; height:100%; background-color: white">
<?php
include("../script/dbconnect.php");
include("../script/get_product.php");
$posts = get_posts(null, $_GET['id']);
foreach ( $posts as $post ) {
if ( ! category_exists('name', $post['name']) ) {
$post['name'] = 'Uncategorised';
}
?>
<ul class='featured'>
<li class='headhighlight'><?php echo $post['title']; ?></li>
<li class='pricehigh'><?php echo $post['price']; ?></li>
<li class='imagefeat'><img class='imagelink' src='<?php echo $post['picture']; ?>' alt='$name'></li>
</ul>
<?php
}
?>
</div>
get_product.php
<?php
function get_posts($id = null, $cat_id = null) {
$posts = array();
//Pagination Code
$perpage = 10;
if(isset($_GET["page_num"]))
{
$page_num = intval($_GET["page_num"]);
}
else
{
$page_num = 1;
}
if ($page_num < 1)
{
$page_num = 1;
}
$calc = $perpage * $page_num;
$start = $calc - $perpage;
//End pagination code
$query ="SELECT `products`.`id` AS `name` , `products_cat`.`id` AS `category_id` , `products`.`name` AS `title` , `description` , `price` , `sale` , `picture`
FROM `products`
INNER JOIN `products_cat` ON `products`.`prod_id` = `products_cat`.`id` ";
if ( isset($id) ) {
$id = (int) $id;
$query .= " WHERE `products`.`id` = {$id}";
}
if ( isset($cat_id) ) {
$cat_id = (int) $cat_id;
$query .= " WHERE `products_cat`.`id` = {$cat_id}";
}
$query .= " ORDER BY `products`.`price` DESC Limit $start, $perpage";
$query = mysql_query($query);
echo mysql_error();
while ( $row = mysql_fetch_assoc($query) ) {
$posts[] = $row;
}
return $posts;
}
Pagination Code - to add page numbers - would be placed in showproduct.php
<p class="pagination">
<?php
if(isset($page_num))
{
$result = mysql_query("SELECT COUNT(*) As Total FROM products");
$rows = mysql_num_rows($result);
if($rows)
{
$rs = mysql_fetch_array($result);
$total = $rs["Total"];
}
$totalPages = ceil($total / $perpage);
if($page_num <=1 )
{
echo '<span id="page_links" style="font-weight:bold;"> < </span>';
}
else
{
$j = $page_num - 1;
echo '<span><a id="page_a_link" href="../admin/admin.master.php?page=list_products.php&page_num=' . $j . '"> < </a></span>';
}
for($i=1; $i <= $totalPages; $i++)
{
if($i<>$page_num)
{
echo '<span>' . $i . '</span>';
}
else
{
echo '<span id="page_links" style="font-weight:bold;">' . $i . '</span>';
}
}
if($page_num == $totalPages )
{
echo '<span id="page_links" style="font-weight:bold;">Next ></span>';
}
else
{
$j = $page_num + 1;
echo '<span> > </span>';
}
}
?>
</p>
$posts = get_posts(null, $_GET['id']);
$result = mysql_query($posts);
In the get_posts function you declare this variable as an array, fill it from a query result and then you return it... so it will never work. What were you trying to do here? You have to pass a string to the mysql_query function with correct MySQL query structure.
EDIT: adding the part of the code that is giving problems, and a possible fix.
$posts = get_posts(null, $_GET['id']);
$i = 0;
foreach ($posts as $post){
....
does it work this way? You already did the query with the LIMIT inside the get_posts function and returned the data.
$posts = get_posts(null, $_GET['id']);
returns array, of something. After it you trying to make mysql_query(array); Try to make
var_dump($posts); and copy print here. Or try next:
$posts = get_posts(null, $_GET['id']);
foreach($posts as $post){
$result[] = mysql_query($posts);
}

pagination using php with mysql data

i am trying to create a pagination in which there are 5 items at a time depending on the number of items in DB. i wrote this code . but i dono how to go further .its buggy..any better pagination or alteration for this
<?php
$myresult .= "<div class='pagination' >";
if ($pagenum == 1)
{
}
else
{
$pagenum = 1;
$myresult .= "<a href='javascript:newPage(\"".$pagenum."\")'> first </a>";
$myresult .= " ";
$previous = $pagenum-1;
$myresult .= "<a href='javascript:newPage(\"".$previous."\")'> Prev </a>";
if ($pagenum == $last)
{
$previous3 = $pagenum-4;
$myresult .= "<a href='javascript:newPage(\"".$previous3."\")'> $previous3 </a>";
$previous2 = $pagenum-3;
$myresult .= "<a href='javascript:newPage(\"".$previous2."\")'> $previous2 </a>";
}
if ($pagenum > 2)
{
$previous1 = $pagenum-2;
$myresult .= "<a href='javascript:newPage(\"".$previous1."\")'> $previous1 </a>";
}
if ($pagenum > 1)
{
$previous2 = $pagenum-1;
$myresult .= "<a href='javascript:newPage(\"".$previous2."\")'> $previous2 </a>";
$myresult .= " ";
}
}
$myresult .= "<span class=\"disabled\"> $pagenum </span>";
if ($pagenum == $last)
{
}
else {
if($pagenum < $last - 1)
{
$next = $pagenum+1;
$myresult .= "<a href='javascript:newPage(\"".$next."\")'> $next </a>";
}
if($pagenum < $last - 2)
{
$next1 = $pagenum+2;
$myresult .= "<a href='javascript:newPage(\"".$next1."\")'> $next1 </a>";
}
if($pagenum == 1 )
{
$next2 = $pagenum+3;
$myresult .= "<a href='javascript:newPage(\"".$next2."\")'> $next2 </a>";
$next3 = $pagenum+4;
$myresult .= "<a href='javascript:newPage(\"".$next3."\")'> $next3 </a>";
}
if($pagenum == 2 )
{
$next2 = $pagenum+3;
$myresult .= "<a href='javascript:newPage(\"".$next2."\")'> $next2 </a>";
}
$next = $pagenum+1;
$myresult .= "<a href='javascript:newPage(\"".$next."\")'> Next </a>";
$myresult .= "<a href='javascript:newPage(\"".$last."\")'> Last</a>";
}
$myresult .= "</div>";
$myresult .= "</br>";
?>
Maybe not the best answer ever but here is something I have used:
<?php
class Pagination {
function __construct() {
}
function getPaginatinationNavigation($page = 1, $num_rows, $rows_per_page, $page_name)
{
$lastpage = ceil($num_rows/$rows_per_page);
$page = (int)$page;
if ($page > $lastpage)
{
$page = $lastpage;
}
if ($page < 1) {
$page = 1;
}
$content='<p style="text-align: center;">';
if ($page == 1) {
$content.= " FIRST PREV ";
} else {
$content.= " <a href='$page_name?page=1'>FIRST</a> ";
$prevpage = $page-1;
$content.= " <a href='$page_name?page=$prevpage'>PREV</a> ";
}
$content.= " ( Page $page of $lastpage ) ";
if ($page == $lastpage) {
$content.= " NEXT LAST ";
} else {
$nextpage = $page+1;
$content.= " <a href='$page_name?page=$nextpage'>NEXT</a> ";
$content.= " <a href='$page_name?page=$lastpage'>LAST</a> ";
}
$content.= '</p>';
return $content;
}
}
?>
Import the Object
require_once('classes/Pagination.php');
Get the amount of total rows:
$query= "SELECT COUNT(*) FROM TABLE_NAME";
$row = mysql_fetch_array($getResults);
$numRecords = $row[0];
Make the LIMIT to limit the rows based upon page number:
$limit = ' LIMIT ' . ($page - 1) * 25 .', 25';
Query USING LIMIT
$query = 'SELECT * FROM TABLE_NAME' . $limit;
$getResults=mysql_query($query) or die(mysql_error());
Display the Results as you normally would:
while($row = mysql_fetch_array($getResults))
{
DISPLAY RESULTS HERE
}
Use the Class to spit out Navigation:
$pagination->getPaginatinationNavigation($page, $numRecords, 25, 'display_page_name.php');
So Overall, for pagination you need to make 2 queries:
1) To get the total amount of records in your search
2) To get the Records LIMITed to those items in a range: say 25 to 50 or 1000 to 1050
You display records from the limited query and to go to the next set of records you increase the page number by one.
Hope this helps. Don't forget to scrub any data you get from the url querystring.
Let me know if you would like me to explain further.

Categories