I have my website that I converted to bootstrap and all is well but pagers. The system my site uses now in functions.php
function pager($rpp, $count, $href, $opts = array())
{
global $language, $btit_settings;
$pager_type = "new";
if($btit_settings["fmhack_pager_type_select"] == "enabled")
$pager_type = $btit_settings["pager_type"];
if($pager_type == "new")
{
$pages = ($rpp == 0)?1:ceil($count / $rpp);
if(!isset($opts['lastpagedefault']))
$pagedefault = 1;
else
{
$pagedefault = floor(($count - 1) / $rpp);
if($pagedefault < 1)
$pagedefault = 1;
}
$pagename = 'pages';
if(isset($opts['pagename']))
{
$pagename = $opts['pagename'];
if(isset($_GET[$opts['pagename']]))
$page = max(1, intval($_GET[$opts['pagename']]));
else
$page = $pagedefault;
}
elseif(isset($_GET['pages']))
{
$page = max(1, intval(0 + $_GET['pages']));
if($page < 0)
$page = $pagedefault;
}
else
$page = $pagedefault;
$pager = '';
if($pages > 1)
{
$pager .= "\n".'<form name="change_page'.$pagename.'" method="post" action="index.php">'."\n".'<select class="drop_pager" name="pages" onchange="location=document.change_page'.$pagename.
'.pages.options[document.change_page'.$pagename.'.pages.selectedIndex].value" size="1">';
for($i = 1; $i <= $pages; $i++)
$pager .= "\n<option ".($i == $page?'selected="selected"':'')."value=\"$href$pagename=$i\">$i</option>";
$pager .= "\n</select>";
}
$mp = $pages; // - 1;
$begin = ($page > 3?($page < $pages - 2?$page - 2:$pages - 2):1);
$end = ($pages > $begin + 2?($begin + 2 < $pages?$begin + 2:$pages):$pages);
if($page > 1)
{
$pager .= "\n <ul class=\"pagination\"><li> «</li>></ul>";
$pager .= "\n<ul class=\"pagination\"><li>< </li></ul>";
}
if($count)
{
for($i = $begin; $i <= $end; $i++)
{
if($i != $page)
$pager .= "\n <ul class=\"pagination\"><li>$i</li></ul>";
else
$pager .= "\n <li class=\"active\">$i</li>";
}
if($page < $mp && $mp >= 1)
{
$pager .= "\n <ul class=\"pagination\"><li> ></li></ul>";
$pager .= "\n <ul class=\"pagination\"><li> »</li></ul>";
}
$pagertop = "$pager\n</form>";
$pagerbottom = str_replace("change_page", "change_page1", $pagertop)."\n";
}
else
{
$pagertop = "$pager\n</form>";
$pagerbottom = str_replace("change_page", "change_page1", $pagertop)."\n";
}
$start = ($page - 1) * $rpp;
if($pages < 2)
{
// only 1 page??? don't need pager ;)
$pagertop = '';
$pagerbottom = '';
}
return array(
$pagertop,
$pagerbottom,
"LIMIT $start,$rpp");
}
Is there a easy way or plugin i can use to incorporate bootstraps?
Here is sample from a page requests.php
list($pagertop,$pagerbottom,$limit) = pager(intval($btit_settings['req_page']),$count,$dir);
then sets it to a tag
$requeststpl->set("bottom_pager",$pagerbottom);
$requeststpl->set("top_pager",$pagertop);
which is then used as a tag to display it via the requests.tpl
<tag:bottom_pager />
<tag:top_pager />
I just want to have it so it looks like bootstrap style..... but comes out like this....
SAMPLE
SAMPLE2
Related
I am not a coder by any means, just dabbling with PHP. On our website we have this function:
public static function mypagination($page, $result)
{
$totalPages = ceil($result / 100);
$links = "";
$links .= "<a href='/index.php/flightsearch/findflights?p=1'><div class='flightsearch-pagination-link'>First</div></a>";
for ($i = 1; $i <= $totalPages; $i++) { $linkpage = $i; $curpage = $page;
if($i != $curpage) {
if($i < ($curpage + 10) && $i > ($curpage - 10))
{
$links .= "<a href='/index.php/flightsearch/findflights?p=$linkpage'><div class='flightsearch-pagination-link'>$i</div></a>";
}
}
else
{
$links .= "<div class='flightsearch-pagination-link flightsearch-pagination-active'>$curpage</div>";
}
}
$links .= "<a href='/index.php/flightsearch/findflights?p=$totalPages'><div class='flightsearch-pagination-link'>Last</div></a>";
return $links;
}
}
?>
When I call the function on another template the pagination does not work and redirects me to the homepage. We have no htt.access file active.
Could anyone please point me in the right direction?
My pagination is showing a page zero (0) when you go to page 2. Not sure why. I don't want to show a page zero.
I'll try to only show the necessary code.
Here is my code:
<?php
$rec_limit = 100;
$targetpage = "dispatch.php";
if (isset($_GET['page']))
{
$page = $_GET['page'];
$offset = $rec_limit * ($page - 1);
}
else
{
$page = 1;
$offset = 0;
}
*** $left_rec = countRecords() - ($page * $rec_limit); ***
$total_records = countRecords(); // countRecords() should be self-explanatory
$total_pages = ceil($total_records / $rec_limit); // $rec_limit is 100
$adjacents = 2;
$previousPage = $page - 1;
$nextPage = $page + 1;
$querystring = "";
$start = ($page < $adjacents ? 1 : $page - $adjacents); // <-- i think the issue is here
$beginning = 1;
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
foreach ($_GET as $key => $value)
{
if($key != "page") $querystring .= "$key=$value&";
}
echo "<div class="row-fluid"><div class="span2"><ul class="pager"><li>First</li>";
if ($left_rec < $rec_limit)
{
$last = $page - 1;
echo #"<li>Previous</li>";
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
}
else if($page == 0)
{
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
}
else if ($page > 0)
{
$last = $page - 2;
echo "<li>Previous</li> ";
for($i= $start; $i <= $end; $i++)
{
echo #"<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
}
echo "<li>Last</li>";
echo '</ul></div></div>';
?>
I would really appreciate the help in removing page 0 from the application. Please disregard any typos or missing quotes. The code works with the exception of it showing page 0.
I added a picture of what the application showing page 0. It only shows page 0 when I go to page 2. After that, I no longer see page 0.
Please let me know what I have to do.
Thanks.
Some advice:
You really shouldn't suppress errors using #, instead you should be instantiating all of your variables and writing proper code.
Don't hard-code pagination into each page. Instead, wrap it in a reusable function.
Example:
// draws a menu for navigating multiple pages of content
function paginate($page, $display, $total) {
if(isset($_SERVER['QUERY_STRING']) && trim($_SERVER['QUERY_STRING']) != '') {
if(stristr($_SERVER['QUERY_STRING'], 'page=')) {
$query = '?' . preg_replace('/page=\d+/', 'page=', $_SERVER['QUERY_STRING']);
} else {
$query = '?' . $_SERVER['QUERY_STRING'] . '&page=';
}
} else {
$query = '?page=';
}
$pages = $total <= $display ? 1 : ceil($total / $display);
$self = htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8');
$first = 'first';
$prev = 'prev';
$next = 'next';
$last = 'last';
echo '<p>';
echo ($page > 1) ? "$first | $prev |" : 'first | prev |';
echo '(page ' . $page . ' of ' . $pages . ')';
echo ($page < $pages) ? "| $next | $last" : '| next | last';
echo '</p>';
}
// output example
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$display = 100;
$start = $display * $page - $display;
$total = countRecords($start, $display);
paginate($page, $display, $total);
I agree with #mister martin, but if you use this code fundamentally, try to change
$start = ($page < $adjacents ? 1 : $page - $adjacents);
To:
$start = ($page < $adjacents ? $page : $page - $adjacents);
Edited: maybe problem in undefined $left_rec, check demo. You can change $s like $_GET['page'] and as I see all works correctly.
Demo.
<?php
$rec_limit = 100;
$targetpage = "dispatch.php";
$total_records = countRecords();
// Nav part
$page = intval($_GET['page'])? $_GET['page']: 1;
$offset = $rec_limit * ($page - 1);
$total_pages = ceil($total_records / $rec_limit); // $rec_limit is 100
$adjacents = 2;
$previousPage = $page - 1;
$nextPage = $page + 1;
$start = ($page < $adjacents ? $page : $page - $adjacents); // <-- I think the issue is this line
$beginning = 1;
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
$uri = $_GET;
unset($uri['page']);
$querystring = http_build_query($uri);
echo '<div class="row-fluid"><div class="span2"><ul class="pager"><li>First</li>';
if($left_rec < $rec_limit && ($page > 1))
{
echo "<li>Previous</li>";
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
} else if ($page > 1)
{
echo "<li>Previous</li> ";
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
} else {
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
}
echo "<li>Last</li>";
echo '</ul></div></div>';
?>
public function paging($limit,$numRows,$page){
$allPages = ceil($numRows / $limit);
$start = ($page - 1) * $limit;
$querystring = "";
foreach ($_GET as $key => $value) {
if ($key != "page") $paginHTML .= "$key=$value&";
}
$paginHTML = "";
$paginHTML .= "Pages: ";
for ($i = 1; $i <= $allPages; $i++) {
$paginHTML .= "<a " . ($i == $page ? "class=\"selected\" " : "");
$paginHTML .= "href=\"?{$querystring}page=$i";
$paginHTML .= "\">$i</a> ";
}
return $paginHTML;
}
This is my pagination function for MVC pattern implementation.But this function has not displayed next and prev links.
I need to return HTML variable for pagination with previous and next link to controller.
I passed these variable to this function from controller.
$limit,$numRows,$page
How can I get next and prev links to above function.
I have added some conditions in the loop itself.
Hope they work.
Try the following:
<?php
public function paging($limit,$numRows,$page){
$allPages = ceil($numRows / $limit);
$start = ($page - 1) * $limit;
$querystring = "";
foreach ($_GET as $key => $value) {
if ($key != "page") $paginHTML .= "$key=$value&";
}
$paginHTML = "";
$paginHTML .= "Pages: ";
for ($i = 1; $i <= $allPages; $i++) {
if ($i>1) {
$prev = $i-1;
$paginHTML .= 'Previous';
}
$paginHTML .= "<a " . ($i == $page ? "class=\"selected\" " : "");
$paginHTML .= "href=\"?{$querystring}page=$i";
$paginHTML .= "\">$i</a> ";
if ($i<$allPages) {
$next = $i+1;
$paginHTML .= 'Next';
}
}
return $paginHTML;
}
?>
I'm using this pagination class like bellow inside my controller
case '' :
$page = isset ( $_REQUEST ['page'] ) ? $_REQUEST ['page'] : 1;
$limit = 5;
$allStudent = $student->getAllStudents();
$numRows = count($allStudent);
$start = ($page - 1) * $limit;
$students = $student->getStudentsWithLimit($start,$limit);
$paginHTML = $pagin->paging($limit,$numRows,$page);
$view->render('view/allStudent.php', array('allStudent' => $students,'pagin' => $paginHTML ));
break;
Get record function in Model class
public function getStudentsWithLimit($start,$limit){
$stmt = $this->db->con->query("SELECT * FROM student LIMIT $start, $limit");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
This should page with next and previous links with a max of 5 on either side of current page.
Pass in a function if you want the link formatted differently.
I've only given this very limited testing, and its been pulled out of a class, so you could replace some hard coded values in here with parameters of references to $this
function get_paging_links($result_count, callable $format_function=null)
{
if(!$format_function){
$format_function = function($url,$page,$qs){
$qs['page'] = $page;
return $url.'?'.http_build_query($qs);
};
}
$per_page = 5;
$total_pages = ceil($result_count / $per_page);
$return = [];
parse_str($_SERVER['QUERY_STRING'],$qs);
$url = $_SERVER['REQUEST_URI'];
//Remove existing query_string.
if($pos = strpos($url,'?')){
$url = substr($url,0,$pos);
}
$current_page = isset($qs['page']) ? $qs['page'] : 1;
$previous = $current_page -1;
if ($previous) {
$return['previous'] = $format_function($url,$previous,$qs);
}
for($i = max(1,$current_page-5); $i <= min($total_pages,$current_page+5); $i++) {
$return["$i"] = $format_function($url,$i,$qs);
}
$next_page = $current_page + 1;
if ($next_page < $total_pages){
$return['next'] = $format_function($url,$next_page,$qs);
}
return $return;
}
Some code from me.
$files = glob("pardod/*.html");
$record_count = 5;
$total_pages = ceil(count($files)/$record_count);
$page = $_GET['page'];
$offset = ($page-1)*$record_count;
$files_filter = array_slice($files, $offset,$record_count);
for ($i = 0; $i<$filecount; $i++){
if ($page){
$start = ($page - 1) * $record_count;
}else{
$start = 0;
}
}
if($total_pages > 1){
if($page != 1){
echo 'Atpakal';
}
if($page != $total_pages){
echo 'Uz priekšu';
}
}
The php pagination dont work, i am just learning how to make, where is a problem?
The *.html files didn't shows :(
Try this code for pagination
<?php
$con=mysql_connect("localhost","root","");
$page=$_REQUEST['page'];
if ($page < 1)
{
$page = 1;
}
$resultsPerPage =15;
$startResults = ($page - 1) * $resultsPerPage;
$numberOfRows = mysql_num_rows(mysql_query('SELECT * FROM tablename'));
$totalPages = ceil($numberOfRows / $resultsPerPage);
echo"<center><table border='1' bordercolor='blue' height='90%' width='90%'> <tr><th bgcolor='silver'>Name</th><th bgcolor='silver'>Password</th><th bgcolor='silver'>Question</th><th bgcolor='silver'> Answer</th><th bgcolor='silver'>Image</th> </tr>";
$i=1;
$result= mysql_query("SELECT * FROM password LIMIT $startResults, $resultsPerPage");
while($row=mysql_fetch_array($result))
{
}
echo"</tr></table></center>";
echo '<center>First ';
if($page > 1)
echo 'Back ';
for($i = 1; $i <= $totalPages; $i++)
{
if($i == $page)
echo '<strong>'.$i.'</strong> ';
else
echo ''.$i.' ';
}
if ($page < $totalPages)
echo 'Next ';
echo 'Last</center>';
?>
Try This Code
$limit = ( isset($_GET['limit'])) ? $_GET['limit'] : 5;
if (strtolower($limit) == 'all') {
$limit = 'all';
} else {
$limit = filter_var($limit, FILTER_SANITIZE_NUMBER_INT);
if (trim($limit) == '') {
$limit = 5;
}
}
$page = ( isset($_GET['page'])) ? $_GET['page'] : 1;
$page = filter_var($page, FILTER_SANITIZE_NUMBER_INT);
$links = ( isset($_GET['links'])) ? $_GET['links'] : 1;
$links = filter_var($links, FILTER_SANITIZE_NUMBER_INT);
Here is the link where step by step described all the process in details.
Simple Pagination in PHP By Learning Ocean Team
if you are using bootstrap, you can use the following
function pagination($page, $count) {
global $options;
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$actual_link = trim(str_replace("page=".$page, "", $actual_link), "&");
$lenght = ceil($count/$options->get("result_per_page"));
if ($page <= 3) {
if ($lenght < 5) {
$first = 0;
$last = $lenght-1;
} else {
$first = 0;
$last = 4;
}
} else if ($page < ($lenght-2)) {
$first = $page-2;
$last = $page+2;
} else if ($page <= $lenght) {
$first = $page-5;
$last = $lenght-1;
}
if ($page > 0) {
$prev = $page-1;
$next = $page+1;
} else {
$prev = 0;
$next = $page+1;
}
echo '<span>Viewing page '.($page+1).' of '.$lenght.'</span><br>';
echo '<nav aria-label="Page navigation example">';
echo '<ul class="pagination justify-content-center">';
if ($prev > 0) {
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page=0">««</a></li>';
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.$prev.'">«</a></li>';
}
for ($i = $first; $i<=$last; $i++) {
if ($i == $page) {
$active = ' active';
} else {
$active = '';
}
echo '<li class="page-item'.$active.'"><a class="page-link" href="'.$actual_link.'&page='.$i.'">'.($i+1).'</a></li>';
}
if ($next < $last) {
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.$next.'">»</a></li>';
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.($lenght-1).'">»»</a></li>';
}
echo '</ul>';
echo '</nav>';
}
to call the function
$pageNumber = 1;
$totalRowCount = 5000;
pagination($pageNumber, $totalRowCount);
How would I do so that the page I'm on won't be clickable?
$page = (isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1);
$limit = ($page - 1) * 15;
$sql = mysql_query("SELECT * FROM log LIMIT $limit, 15");
$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM log"),0);
$totalpages = ceil($totalres / 15);
for ($i = 1; $i <= $totalpages; $i++) {
$pagination .= "$i ";
}
<p><?php echo $pagination ?></p>
if ($i == $page)
$pagination .= "$i ";
else
$pagination .= "$i ";
Just need to modify the for loop to check what the current page is.
for ($i = 1; $i <= $totalpages; $i++)
{
if ($i != $page)
$pagination .= "$i ";
else
$pagination .= " " . $i . " ";
}
With an if inside the for,
if ( $i != $page) {
//Your code.
}
else
{
//Same page.
$pagination .= " " . $page . " "
}
#RedBlueThing Has soon has I saw your post I got it to work, although I got the idea from a different stackoverflow question with a little modifying of the code.
<?php
echo "<section>";
$link = mysqli_connect("localhost", "root", "root", "user_db");
$page = (isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1);
$offset = 4;
$x = 5;
$limit = ($page - 1) * $offset;
$sql = mysqli_query($link,"SELECT * FROM cfp_blogs;");
$totalres = mysqli_num_rows($sql);
$totalpages = ceil($totalres / $offset);
$stmt = mysqli_query($link, "SELECT * FROM blogs LIMIT $limit, $offset;");
if($stmt->num_rows > 0){
while($obj = mysqli_fetch_object($stmt)) {
echo "<h1>$obj->title</h1>";
echo "<p>$obj->content</p>";
$stmt2 = $link->query("SELECT COUNT(id) AS Tot FROM blogs");
if($stmt2->num_rows > 0){
while ($obj2 = $stmt2->fetch_object()){
echo "<p>($obj2->Tot)</p>";
}
}
else
echo "<p>(0)</p>";
$stmt2->close();
}
}
else {
echo "<section class='error'><h1>There was a problem with the database.</h1></section>";
}
mysqli_free_result($stmt);
for ($i = $page - $x; $i < $page; $i++)
{
if ($i >= 1) {
$pagination .= "$i ";
}
}
$pagination .= " " . $i . " ";
for ($i = $page + 1; $i <= $page + $x; $i++)
{
if ($i <= $totalpages) {
$pagination .= "$i ";
}
}
/*for ($i = 1; $i <= $totalpages; $i++)
{
if ($i != $page){
$pagination .= "$i ";
}
else{
$pagination .= " " . $i . " ";
}
}*/
mysqli_close($link);
echo $pagination;
echo "</section>";
?>
The only thing I couldn't get was having a "next" or "previous" button. If anyone is up to the task of adding on those buttons, give it a shot. Other wise..... Enjoy..... :) This is my way of giving back to the stackoverflow community for helping me.