I am working on a php ajax pagination.
The problem is that it wont ad the class="highlightActivePage" proper.
If I click on 1 - 2 - 3 , it works fine and highlight the number 3 if im on page 3.
But soon as I click on page 4 it highlights page 5 instead, etc.
Not sure if my for loop is wrong, but here us my code:
if (paginationHTML == "")
{
paginationHTML += "<ul>";
if (adResultsData.show_first_text == 1)
{
paginationHTML += "<li><a href='#' onclick='fetchResults(1);'>First</a></li>";
}
if (adResultsData.show_previous_text == 1)
{
paginationHTML += "<li><a href='#' onclick='fetchResults(" + (adResultsData.current_page - 1) + ");'>Prev</a></li>";
}
for (var i = 0; i < adResultsData.pages.length; i++)
{
if (adResultsData.current_page == (i + 1))
{
paginationHTML += "<li><a href='#' class='highlightActivePage' onclick='fetchResults(" + adResultsData.pages[i] + ");'>" + adResultsData.pages[i] + "</a></li>";
}
else
{
paginationHTML += "<li><a href='#' onclick='fetchResults(" + adResultsData.pages[i] + ");'>" + adResultsData.pages[i] + "</a></li>";
}
}
if (adResultsData.show_next_text == 1)
{
paginationHTML += "<li><a href='#' onclick='fetchResults(" + (adResultsData.current_page + 1) + ");'>Next</a></li>";
}
if (adResultsData.show_last_text == 1)
{
paginationHTML += "<li><a href='#' onclick='fetchResults(" + adResultsData.number_of_pages + ");'>last</a></li>";
}
paginationHTML += "</ul>";
paginationHTML += pageSpan.innerHTML = "<br>Page " + adResultsData.current_page + " of " + adResultsData.number_of_pages;
}
php
$numberOfPages = $results['pages'];
$currentPage = $results['currentPage'];
if ($currentPage != 1 && $currentPage != 2)
{$showFirst = 1;}
else $showFirst = 0;
if ($currentPage != 1)
{$showPrevious = 1;}
else $showPrevious = 0;
if ($currentPage != $numberOfPages)
{$showNext = 1;}
else $showNext = 0;
if ($currentPage != $numberOfPages && $currentPage != ($numberOfPages - 1))
{$showLast = 1;}
else $showLast = 0;
if ($currentPage <= 5 && $numberOfPages <= 5 || $numberOfPages <= 5)
{$startingPage = 1;}
else if ($currentPage == 1 || $currentPage == 2)
{$startingPage = 1;}
else
{$startingPage = $currentPage - 2;}
$pageNumbers = [];
for ($i = $startingPage; $i < ($startingPage + 5) && $i <= $numberOfPages; $i++)
{
$pageNumbers[] = $i;
}
$pagesString = implode(", ", $pageNumbers);
$listingsString = implode(", ", $listingsArray);
$jsonString = <<< END
{
"resultsTotal" : $numberOfResults,
"listings" : [$listingsString],
"number_of_pages" : $numberOfPages,
"current_page" : $currentPage,
Thanks!
Related
I have a strange problem with my navigation next/previous code. Everything works fine but random after I click on the next page, the whole navigation is gone. And when I change the pagina variable from page 4 to 5 for example, does everything work again, but pagina 4 keeps removing the navigation. I don't know why it occurs?
On the top of my page do I have:
if (isset($_GET['pagina']) && $_GET['pagina']!="") {
$page_no = $_GET['pagina'];
}
else {
$page_no = 1;
}
$total_records_per_page = 100;
$offset = ($page_no-1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$adjacents = "2";
And on the bottom of my page
$taal = htmlspecialchars(addslashes($_GET['taal']));
if($page_no > 1){
echo <a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$previous_page."'>Vorige</a>";
}
if ($total_no_of_pages <= 10){
for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
if ($counter == $page_no) {
echo "<a class='active'>".$counter."</a>";
}
else
{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
}
elseif ($total_no_of_pages > 10){
if($page_no <= 4) {
for ($counter = 1; $counter < 8; $counter++){
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}
else {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
echo "<a>...</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$second_last."'>".$second_last."</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$total_no_of_pages."'>".$total_no_of_pages."</a>";
}
elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=1'>1</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=2'>2</a>";
//echo "<li><a>...</a></li>";
for (
$counter = $page_no - $adjacents;
$counter <= $page_no + $adjacents;
$counter++
) {
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}else{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
echo "<a>...</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$second_last."'>".$second_last."</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$total_no_of_pages."'>".$total_no_of_pages."</a>";
}
else {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=1'>1</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=2'>2</a>";
echo "<a>...</a>";
for (
$counter = $total_no_of_pages - 6;
$counter <= $total_no_of_pages;
$counter++
)
{
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}
else{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
}
}
if($page_no < $total_no_of_pages) {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$next_page."'>Volgende</a>";
}
$taal is language,
$page_no is which page where I'm on
$zoekveld is search field to search on
$pagina is pagenumber in url
I think it has something to do with my GET search field (Zoekveld) but why occurs it after a couple of pages?
Thanks in advance
I want to know how can i add next page, previous page, first page and last page in this pagination. Below code is only showing the page number it doesn't show next page, previous page, first page and last page.
<?php
$pagesToShow = 10;
$pageSize = 20;
$numPages = ceil($numResults / $pageSize);
$pageLefts = min($pagesToShow, $numPages);
$currentPage = $page - floor( $pagesToShow / 2 );
if($currentPage < 1){
$currentPage = 1;
}
if($currentPage + $pageLefts > $numPages + 1) {
$currentPage = $numPages + 1 - $pageLefts;
}
while($pageLefts != 0 && $currentPage <= $numPages) {
if($currentPage == $page){
echo "<div class='pageNumberContainer'>
<span class='pageNumber'>$currentPage</span>
</div>";
}else{
echo "<div class='pageNumberContainer'>
<a href='search.php?term=$term&type=$type&page=$currentPage'>
<span class='pageNumber'>$currentPage</span>
</a>
</div>";
}
$currentPage++;
$pageLefts--;
}
?>
I dont know if this helps .. but maybe you can do something with it :)
<?php $pagesToShow = 10;
$pageSize = 20;
$numPages = ceil($numResults / $pageSize);
$pageLefts = min($pagesToShow, $numPages);
$currentPage = $page - floor( $pagesToShow / 2 );
if($currentPage < 1){
$currentPage = 1;
}
if($currentPage + $pageLefts > $numPages + 1) {
$currentPage = $numPages + 1 - $pageLefts;
}
if ($numPages > 1){echo 'FirstPage = 1';}
while($pageLefts != 0 && $currentPage <= $numPages) {
if($currentPage == $page){
echo "<div class='pageNumberContainer'>
<span class='pageNumber'>Current Page Number Is: ".$currentPage</span>
</div>";
}else{
if ($currentPage != 1){$previousPage = $currentPage - 1; echo
'PREVIOUS PAGE:' . $previousPage; }
echo "<div class='pageNumberContainer'>
<a href='search.php?term=".$term."&type=".$type."&page=".$currentPage."'>
<span class='pageNumber'>CURRENT PAGE: ".$currentPage." </span>
</a>
</div>";
if ($currentPage != $numPages){$nextPage = $currentPage + 1; echo 'NEXT PAGE:' . $nextPage; }
}
$currentPage++;
$pageLefts--;
}
if ($numPages > 1){echo 'LastPage = '.$numPages;}
?>
Found a working code on PHP pagination here and i am trying to integrate this code into my search engine. I tried but the pagination function doesn't seem to work please can anybody tell me what's wrong with my code and any possible fixes.
<?php
if(isset($_GET['search']))
{
$search = $_GET['search'];
$condition = '';
$query = explode(" ", $_GET["search"]);
foreach($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR keywords LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$sql_query = "SELECT * FROM questions WHERE " . $condition;
$result = $db->query($sql_query);
echo $sql_query;
$queryCount = "SELECT COUNT(*) as count FROM questions WHERE " . $condition;
$countRet = $db->querySingle($queryCount);
if ($countRet > 0)
{
$perpage = 1;
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages= ceil($countRet / $perpage);
if (!$start) {
$start = 0;
$getQuery = $db->query("SELECT * FROM questions WHERE $condition LIMIT $start, $perpage");
while($row = $getQuery->fetchArray(SQLITE3_ASSOC))
{
echo '<tr><td>'.$row["question"].'</td></tr>';
}
//Pagination Codes
echo "<center>";
$prev = $start - $perpage;
$next = $start + $perpage;
$adjacent = 3;
$last = $max_pages - 1;
if ($max_pages > 1) {
//prev button
if (!$start <= 0)
{
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
}
if ($max_pages < 7 + ($adjacent * 2)) {
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacent * 2); $counter++) {
if ($i == $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
elseif ($max_pages - ($adjacent * 2) > ($start / $perpage) && ($start / $perpage) > ($adjacent * 2)) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
$i = $start;
for ($counter = ($start / $perpage)+1; $counter < ($start /$perpage) + $adjacent + 2; $counter++) {
if ($i == $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
$i = $start;
for ($counter = ($start / $perpage) + 1; $counter <= $max_pages; $counter++) {
if ($i = $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
}
//next button
if (!($start >= $countRet - $perpage)) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";
}
}
else
{
echo '<label>No data found</label>';
}
}
?>
The code above doesn't seem to work for some reason. I have tried to debug but cant't seem to find the error or errors. Any reasonable help would do with this. Thanks in advance.
First you set $start in your ternary logic:
$start = isset($_GET['start']) ? $_GET['start']: '';
After this, you check whether $start exists or not, and if it doesn't, you set it 0:
if (!$start) {
$start = 0;
$getQuery ...
while($row = $getQuery ...
The problem is all of your query and pagination logic is inside of this if conditional; if $GET['start'] is set, then your query and pagination logic will never trigger. And on top of this, all of your outputted <a> links do indeed send the start parameter:
<a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>
To resolve this, simply set your ternary to 0 for the failure condition, and omit the if conditional entirely:
$start = isset($_GET['start']) ? $_GET['start']: 0;
This can be seen in the following:
$start = isset($_GET['start']) ? $_GET['start']: 0;
$max_pages= ceil($countRet / $perpage);
$getQuery = $db->query("SELECT * FROM questions WHERE $condition LIMIT $start, $perpage");
while($row = $getQuery->fetchArray(SQLITE3_ASSOC))
{
echo '<tr><td>'.$row["question"].'</td></tr>';
}
//Pagination Codes
echo "<center>";
$prev = $start - $perpage;
$next = $start + $perpage;
$adjacent = 3;
$last = $max_pages - 1;
if ($max_pages > 1) {
//prev button
if (!$start <= 0)
{
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
}
if ($max_pages < 7 + ($adjacent * 2)) {
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacent * 2); $counter++) {
if ($i == $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
elseif ($max_pages - ($adjacent * 2) > ($start / $perpage) && ($start / $perpage) > ($adjacent * 2)) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
$i = $start;
for ($counter = ($start / $perpage)+1; $counter < ($start /$perpage) + $adjacent + 2; $counter++) {
if ($i == $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
$i = $start;
for ($counter = ($start / $perpage) + 1; $counter <= $max_pages; $counter++) {
if ($i = $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
}
//next button
if (!($start >= $countRet - $perpage)) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";
Help me, i was writing my code, all done succesfully but my problem is when the "..." (separator) for my curerent page and last page showed after the last page, not between my current page and my last page
how can that possible ?
this is my script for showing the page :
<?php
$paging2 = mysqli_query($koneksi,"select * from laporan where seksi='{$seksi}'");
$jmldata = mysqli_num_rows($paging2);
$jmlhalaman = ceil($jmldata/$batas);
echo "<ul class='pagination'>";
if ($halaman !== 1){
echo "<li><a href='index.php?load=table_laporan&halaman=1'>First</a></li>";
}
if ($halaman > 1){
echo "<li><a href='index.php?load=table_laporan&halaman=".($halaman-1)."'>Previous</a></li>";
}
for($i =1; $i <= $jmlhalaman; $i++){
if ((($i >= $halaman - 3) && ($i <= $halaman + 3)) || ($i == 1) || ($i ==$jmlhalaman))
{
if ($i == $halaman){
$class="active";
} else{
$class="disable";
} //echo "<b>$i</b>";
echo "<li class='$class'>".$i."<span class='sr-only'>(current)</span></li>";
if($i==$jmlhalaman && $halaman <= $jmlhalaman-5) echo "<li><a>...</a></li>";
if($i==1 && $halaman >= 6) echo "<li><a>...</a></li>";
}
}
if ($halaman >= 1){
echo "<li><a href=index.php?load=table_laporan&halaman=". ($halaman+1)."'>Next</a></li>";
} if ($halaman >= 1){
echo "<li><a href='index.php?load=table_laporan&halaman=". ($jmlhalaman)."'>Last</a></li>";
}
echo "</ul>";
echo "<p>Total Data : <b>$jmldata</b> Data</p>";
?>
Im working on a codeigniter project which takes results and adds it to the page with pagination.
The problem that im having is,
the page shows all the page numbers but the first page always seems empty. When i move to the second page it shows the first page results along with the second page results. Even if i move to the first page again its empty.
when i move further, all the results seems to be stacked rather than paginating.
so first page 0 results. the second page shows 30, 3rd page shows 45 results.
below is my script
<script type="text/javascript">
$(document).ready(function(){
function loading_show(){
$('#loading').html("<img src='<?php echo $url?>images/loading.gif'/>").fadeIn('fast');
}
function loading_hide(){
$('#loading').fadeOut('fast');
}
function loadData(page){
loading_show();
$.ajax
({
type: "POST",
url: "<?php echo base_url()?>index.php/controls/ajaxload",
data: "page="+page,
success: function(msg)
{
$("#container").ajaxComplete(function(event, request, settings)
{
loading_hide();
$("#container").html(msg);
});
}
});
}
loadData(1);
$('#container .pagination li.active').live('click',function(){
var page = $(this).attr('p');
loadData(page);
});
$('#go_btn').live('click',function(){
var page = parseInt($('.goto').val());
var no_of_pages = parseInt($('.total').attr('a'));
if(page != 0 && page <= no_of_pages){
loadData(page);
}else{
alert('Enter Number '+no_of_pages);
$('.goto').val("").focus();
return false;
}
});
});
</script>
php code
function ajaxload()
{
if($_POST['page'])
{
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 15;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
$city ='London';
$this->load->model('control_model');
$query_pag_datas = $this->control_model->data_for_pagination($start, $per_page,$city);
$msg = "";
foreach ($query_pag_datas as $single):
$htmlmsg = htmlentities($single['Description']);
$msg .= "<li><b>" . $single['offID'] . "</b> " . $htmlmsg . "</li>";
endforeach;
$msg = "<div class='data'><ul>" . $msg . "</ul></div>";
$count = $this->control_model->count_pages($city);
$no_of_paginations = ceil($count / $per_page);
if ($cur_page >= 7) {
$start_loop = $cur_page - 3;
if ($no_of_paginations > $cur_page + 3)
$end_loop = $cur_page + 3;
else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
$start_loop = $no_of_paginations - 6;
$end_loop = $no_of_paginations;
} else {
$end_loop = $no_of_paginations;
}
} else {
$start_loop = 1;
if ($no_of_paginations > 7)
$end_loop = 7;
else
$end_loop = $no_of_paginations;
}
$msg .= "<div class='pagination'><ul>";
if ($first_btn && $cur_page > 1) {
$msg .= "<li p='1' class='active'>First</li>";
} else if ($first_btn) {
$msg .= "<li p='1' class='inactive'>First</li>";
}
if ($previous_btn && $cur_page > 1) {
$pre = $cur_page - 1;
$msg .= "<li p='$pre' class='active'>Previous</li>";
} else if ($previous_btn) {
$msg .= "<li class='inactive'>Previous</li>";
}
for ($i = $start_loop; $i <= $end_loop; $i++) {
if ($cur_page == $i)
$msg .= "<li p='$i' style='color:#fff;background-color:#006699;' class='active'>{$i}</li>";
else
$msg .= "<li p='$i' class='active'>{$i}</li>";
}
if ($next_btn && $cur_page < $no_of_paginations) {
$nex = $cur_page + 1;
$msg .= "<li p='$nex' class='active'>Next</li>";
} else if ($next_btn) {
$msg .= "<li class='inactive'>Next</li>";
}
if ($last_btn && $cur_page < $no_of_paginations) {
$msg .= "<li p='$no_of_paginations' class='active'>Last</li>";
} else if ($last_btn) {
$msg .= "<li p='$no_of_paginations' class='inactive'>Last</li>";
}
$goto = "<input type='text' class='goto' size='1' style='margin-top:-1px;margin-left:60px;'/><input type='button' id='go_btn' class='go_button' value='Go'/>";
$total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>";
$msg = $msg . "</ul>" . $goto . $total_string . "</div>";
echo $msg;
}
}
any help will be appreciated.
So if the page you are posting is 1, this is what you get:
Page 1:
$page = $_POST['page'];//let's say we're posting page 1, so $page === 1
...
$page -= 1;//so now, page === 0
...
$start = $page * $per_page;//which, since math, will be 0
...
$query_pag_datas = $this->control_model->data_for_pagination($start, $per_page,$city);
// you just called data_for_pagination(0, 15, 'London')
Since we can't see the data_for_pagination method, I'm going to assume that this first parameter is the number of results you are requesting. This makes sense because as the page number increases, the number of results will increase:
Page 2:
$page = $_POST['page'];//let's say we're posting page 2, so $page === 2
...
$page -= 1;//so now, page === 1
...
$start = $page * $per_page;//which, since math, will be 15
...
$query_pag_datas = $this->control_model->data_for_pagination($start, $per_page,$city);
// you just called data_for_pagination(15, 15, 'London')
Page 3:
$page = $_POST['page'];//let's say we're posting page 3, so $page === 3
...
$page -= 1;//so now, page === 2
...
$start = $page * $per_page;//which, since math, will be 30
...
$query_pag_datas = $this->control_model->data_for_pagination($start, $per_page,$city);
// you just called data_for_pagination(30, 15, 'London')
Hope that helps.
change
$page -= 1;
to
$page = ($page > 0 ) ? $page -1 : 0 ;
in the first click page is 0
so
0-1 => -1
-1 * 15 => -15 ;
your first offset is -15 !