Jump to content

Recommended Posts

Hi gurus!

Recently i am facing a tiny problem. I want to do is pagination like (<< PREVIOUS | INDEX | NEXT >>).

My codes are below.

 

Regards

 

<?php
    $SQL = mysql_query("SELECT id FROM blogmaster ORDER BY id DESC");
    while($ROW = mysql_fetch_array($SQL))
    {
        $MYARRAY[] = $ROW['id'];
    }
    
    $ARRAYCOUNT = count($MYARRAY);
    if($MYARRAY[0] != $_GET['id'])
    {
        $DESC = $MYARRAY[0] + 1;
        
        echo '<a href="/blog/detail.php?id=' .$MYARRAY[$DESC]. '" class="p-link"><< PREV</a>  ';
    }
    echo '<a href="/blog/index.php" class="p-link">INDEX</a>  ';
    if($MYARRAY[$ARRAYCOUNT] != $_GET['id'])
    {
        $ASC = $MYARRAY[$ARRAYCOUNT] - 1;
        
        echo '<a href="/blog/detail.php?id=' .$MYARRAY[$ASC]. '" class="p-link">NEXT >></a>  ';
    }
?>

 

Also how can i paginate something like <<PREV | 1 2 3 4 5 | NEXT>> when user reach page #5 and click NEXT link then page number should change <<PREV | 6 7 8 9 10 | NEXT>>

Link to comment
https://forums.phpfreaks.com/topic/236724-pagination-help/
Share on other sites

// Current Page
$page = 1;

// How many records are we showing on a single page?
$records_per_page = 10;

// Calculate the lowest row of data to fetch from the database
$minimum_row = ($page-1) * $records_per_page; // = 0

$query = "SELECT * FROM table LIMIT $minimum_row, $records_per_page";
$result = mysql_query($query) or die("MySQL Error");
if (mysql_num_rows($result) > 0) {
// Deal with your data
}
mysql_free_result($result);

if ($page > 1) {
echo 'Previous Page';
}

// To work out if we should display a next page link we have to find out how many records there are.
$query = "SELECT COUNT(*) AS 'total' FROM table";
$result = mysql_query($query) or die("MySQL Error");
$num_records = mysql_fetch_object($result)->total;

// Work out maximum amount of pages
$max_page = floor($num_records/$records_per_page);

if ($page < $max_page) {
echo 'Next Page';
}

 

I haven't tested the code, and you could probably optimize it a little more.

 

The benefit of running it this way is that you don't fetch every record from the database meaning you use a lot less memory.

Link to comment
https://forums.phpfreaks.com/topic/236724-pagination-help/#findComment-1216902
Share on other sites

Dear micmania 1

Thanks for your afford and code. Unfortunately this is not what i am looking for.

Let's say i have 10 records in my DB and those records are:

 

4

5

9

10

11

19

28

47

48

62

 

And again let's say my visitor arrived index.php at first time. So, visitor will see record #62(because ORDER BY id DESC LIMIT 1).

When visitor click the NEXT >> page i want to get record #48 (as you can see between #48 and #62 is empty).

How can i skip those empty record lines and assign #48 into anchor <a href="index.php?id=48">NEXT >></a>

 

My displaying order is newer to older so NEXT means older record and PREV means newer record.

Link to comment
https://forums.phpfreaks.com/topic/236724-pagination-help/#findComment-1216912
Share on other sites

<script type="text/javascript">

// click functions are here which will controls pagination buttons enable and disable

function pagebyclick1(dom,page){

count = dom.next().children().children().children('input').val();

if(count > 5 )

{

if(page>1)

{

dom.next().children().children().children('span[class=previous]').attr('disabled', '').attr('style',"opacity:1;");

dom.next().children().children().children('span[class=first]').attr('disabled', '').attr('style',"opacity:1;");

}else{

dom.next().children().children().children('span[class=previous]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

dom.next().children().children().children('span[class=first]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

}

if(page==count)

{

dom.next().children().children().children('span[class=next]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

dom.next().children().children().children('span[class=last]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

}else{

dom.next().children().children().children('span[class=next]').attr('disabled', '').attr('style',"opacity:1;");

dom.next().children().children().children('span[class=last]').attr('disabled', '').attr('style',"opacity:1;");

}

}

 

if(page == 1)

{

dom.next().children().children().children('span[class=previous]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

dom.next().children().children().children('span[class=first]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

}

 

var sl=parseInt(count)-1;var last=count;

if(page == sl || page ==last )

{

if(page==last){

dom.next().children().children().children('span[class=next]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

dom.next().children().children().children('span[class=last]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

}

if(count > 5 ){

for(var i=count-4,x=2;i<=count;i++,x++)

{

dom.next().children().children().children().eq(x).html(i);

if(i==page)

{ dom.next().children().children().children().removeClass('active');

dom.next().children().children().children().eq(x).addClass('active');

}

}

}

 

}else if(page==1 || page==2 )

{

if(page==1){

dom.next().children().children().children('span[class=previous]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

dom.next().children().children().children('span[class=first]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

}

if(count > 5 ){

for(var i=1,x=2;i<=5;i++,x++)

{

dom.next().children().children().children().eq(x).html(i);

if(i==page)

{

dom.next().children().children().children().removeClass('active');

dom.next().children().children().children().eq(x).addClass('active');

}

}

}

 

}else

{

var start;var end;

start=parseInt(page)-2;end=parseInt(page)+2;

for(var i=start,x=2;i<=end;i++,x++)

{

dom.next().children().children().children().eq(x).html(i);

if(i==page)

{ dom.next().children().children().children().removeClass('active');

dom.next().children().children().children().eq(x).addClass('active');

}

}

}

}

 

function previouspage1(dom,page)

{

if(page !=2){

dom.next().children().children().children('span[class=active]').prev('span').addClass('active');

dom.next().children().children().children('span[class=active]').last().removeClass('active');

}

dom.next().children().children().children('span[class=next]').attr('disabled', '').attr('style',"opacity:1;");

dom.next().children().children().children('span[class=last]').attr('disabled', '').attr('style',"opacity:1;");

page=parseInt(page)-1;

count = dom.next().children().children().children('input').val();

var sl=count-1;var last=count;

if(page == sl || page ==last )

{

//$("#"+dom+" span[class=active]").removeClass('active').prev().addClass('active');

}else if(page==1)

{

dom.next().children().children().children('span[class=previous]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

dom.next().children().children().children('span[class=first]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

dom.next().children().children().children().removeClass('active');

dom.next().children().children().children().eq(2).html('1').addClass('active');

for(var i=2,x=3;i<=5;i++,x++){

dom.next().children().children().children().eq(x).html(i);

}

}else if( page==2 ){

 

}

else

{

var start;var end;

start=page-2;end=page+2;

for(var i=start,x=2;i<=end;i++,x++)

{

dom.next().children().children().children().eq(x).html(i);

if(i==page)

{

dom.next().children().children().children().removeClass('active');

dom.next().children().children().children().eq(x).addClass('active');

}

}

}

}

function nextpage1(dom,page)

{

dom.next().children().children().children('span[class=active]').next('span').addClass('active');

dom.next().children().children().children('span[class=active]').first().removeClass('active');

 

dom.next().children().children().children('span[class=previous]').attr('disabled', '').attr('style',"opacity:1;");

dom.next().children().children().children('span[class=first]').attr('disabled', '').attr('style',"opacity:1;");

page=parseInt(page)+1;

count = dom.next().children().children().children('input').val();

var sl=count-1;var last=count;

if(page == last){

dom.next().children().children().children('span[class=next]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

dom.next().children().children().children('span[class=last]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

}

else if(page == sl )

{

 

}else if(page==1 || page==2 )

{

 

}else

{

var start;var end;

start=page-2;end=page+2;

for(var i=start,x=2;i<=end;i++,x++)

{

dom.next().children().children().children().eq(x).html(i);

if(i==page)

{ dom.next().children().children().children().removeClass('active');

dom.next().children().children().children().eq(x).addClass('active');

}

}

}

}

function firstpage1(dom)

{

dom.next().children().children().children('span[class=next]').attr('disabled', '').attr('style',"opacity:1;");

dom.next().children().children().children('span[class=last]').attr('disabled', '').attr('style',"opacity:1;");

dom.next().children().children().children('span[class=first]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

dom.next().children().children().children('span[class=previous]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

dom.next().children().children().children().removeClass('active');

count = dom.next().children().children().children('input').val();

for(var i=1,x=2;i<=5;i++,x++)

{

dom.next().children().children().children().eq(x).html(i);

if(i==1)

{

dom.next().children().children().children().eq(x).addClass('active');

}

}

}

function lastpage1(dom)

{

dom.next().children().children().children('span[class=first]').attr('disabled', '').attr('style',"opacity:1;");

dom.next().children().children().children('span[class=previous]').attr('disabled', '').attr('style',"opacity:1;");

dom.next().children().children().children('span[class=next]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

dom.next().children().children().children('span[class=last]').attr('disabled', 'true').attr('style',"opacity:0.5;cursor: auto;");

dom.next().children().children().children().removeClass('active');

count = dom.next().children().children().children('input').val();

for(var i=count-4,x=2;i<=count;i++,x++)

{

dom.next().children().children().children().eq(x).html(i);

if(i==count)

{

dom.next().children().children().children().eq(x).addClass('active');

}

}

}

function pagebyclick(count,dom,html,first,prev,next,last){

var pagenumber=$("#"+dom+" span[class=active]").html();

if(count > 5 )

{

if(html>1)

{

$("."+prev+"").attr('disabled', '').attr('style',"opacity:1;");

$('.'+first+"").attr('disabled', '').attr('style',"opacity:1;");

}else{

$("."+prev+"").attr('disabled', 'true').attr('style',"opacity:0.5;");

$("."+first+"").attr('disabled', 'true').attr('style',"opacity:0.5;");

}

if(html==count)

{

$("."+next+"").attr('disabled', 'true').attr('style',"opacity:0.5;");

$("."+last+"").attr('disabled', 'true').attr('style',"opacity:0.5;");

}else{

$("."+next+"").attr('disabled', '').attr('style',"opacity:1;");

$("."+last+"").attr('disabled', '').attr('style',"opacity:1;");

}

}

var sl=count-1;var last=count;

if(pagenumber == sl || pagenumber ==last )

{

if(pagenumber==last){

$("."+next+"").attr('disabled', 'true').attr('style',"opacity:0.5;");

$("."+last+"").attr('disabled', 'true').attr('style',"opacity:0.5;");

}

if(count > 5 ){

for(var i=count-4,x=2;i<=count;i++,x++)

{

$("#"+dom+"").children().eq(x).html(i);

if(i==pagenumber)

{

$("#"+dom+" span").removeClass('active');

$("#"+dom+"").children().eq(x).addClass('active');

}

}

}

}else if(pagenumber==1 || pagenumber==2 )

{

if(pagenumber==1){

$("."+prev+"").attr('disabled', 'true').attr('style',"opacity:0.5;");

$("."+first+"").attr('disabled', 'true').attr('style',"opacity:0.5;");

}

if(count > 5 ){

for(var i=1,x=2;i<=5;i++,x++)

{

$("#"+dom+"").children().eq(x).html(i);

if(i==pagenumber)

{

$("#"+dom+" span").removeClass('active');

$("#"+dom+"").children().eq(x).addClass('active');

}

}

}

}else

{

var start;var end;

start=pagenumber-2;end=parseInt(pagenumber)+2;

for(var i=start,x=2;i<=end;i++,x++)

{

$("#"+dom+"").children().eq(x).html(i);

if(i==pagenumber)

{

$("#"+dom+" span").removeClass('active');

$("#"+dom+"").children().eq(x).addClass('active');

}

}

}

 

}

/////////////////////Five click functions needs to write :-

 

var dom="listpages",firstp="first",prevp="previous",nextp="next",lastp="last";var count;var html;

 

function datacall(url)

{ $.getJSON(o_url,function(lead_data) {

$('.leadlist').html('');

$.each(lead_data, function(i,ldata){

                              // jsonlist data add to your div

                              // the above function datalist called on each click to update list

});

 

}

 

}

$("#leadpages span").not(".previouslead, .nextlead, .firstlead, .lastlead").click(function() {

countlead=$("#leadcount").val();

$("#leadpages span").removeClass('active');

$(this).addClass('active');

leadhtml=$(this).html();

pagebyclick(countlead,leaddom,leadhtml,leadfirst,leadprev,leadnext,leadlast);

$('.leadlist').html('<img alt="" src="/css/images/loader.gif" style="margin:18px 0  0 450px;">');

var page = $(this).html();

var url='/json.php?query=page='+page;

datacall(url);

});

 

$('.first').live("click",function(){

var check=$(this).attr('disabled');

if(check =='true')

{}else{

firstpage(dom,lastp,nextp);

$('.prospectlist').html('<img alt="" src="/css/images/loader.gif" style="margin:18px 0  0 450px;">');

enq_url='/json.php?page=1';

prospectList(enq_url);

$('.previous').attr('disabled', 'true').attr('style',"opacity:0.5;");

$('.first').attr('disabled', 'true').attr('style',"opacity:0.5;");

}

});

$('.last').live("click",function(){

var check=$(this).attr('disabled');

if(check =='true')

{}else{

count=$("#enqcount").val();

lastpage(dom,firstp,prevp,count);

$('.prospectlist').html('<img alt="" src="/css/images/loader.gif" style="margin:18px 0  0 450px;">');

var lastp=$("#prospectpages span[class=next]").prev().html();

enq_url='/json.php?page='+lastp;

prospectList(enq_url);

$('.next').attr('disabled', 'true').attr('style',"opacity:0.5;");

$('.last').attr('disabled', 'true').attr('style',"opacity:0.5;");

}

});

$('.previous').click(function() {

var check=$(this).attr('disabled');

if(check =='true')

{}else{

count=$("#enqcount").val();

previouspage(dom,count,firstp,prevp,nextp,lastp);

$('.prospectlist').html('<img alt="" src="/css/images/loader.gif" style="margin:18px 0  0 450px;">');

var pagenumber =$("#prospectpages  span[class=active]").html();

enq_url='/json.php?page='+pagenumber;

prospectList(enq_url);

if($("#prospectpages span[class=active]").prev().html()=="Prev")

{

$('.previous').attr('disabled', 'true').attr('style',"opacity:0.5;");

$('.first').attr('disabled', 'true').attr('style',"opacity:0.5;");

}

}

});

$('.next').click(function() {

var check=$(this).attr('disabled');

if(check =='true')

{}else{

count=$("#enqcount").val();

nextpage(dom,count,firstp,prevp,nextp,lastp);

$('.prospectlist').html('<img alt="" src="/css/images/loader.gif" style="margin:18px 0  0 450px;">');

var pagenumber =$("#prospectpages  span[class=active]").html();

enq_url='/json.php?page='+pagenumber;

prospectList(enq_url);

if($("#prospectpages span[class=active]").next().html()=="Next")

{

$(".next").attr('disabled', 'true').attr('style',"opacity:0.5;");

$(".last").attr('disabled', 'true').attr('style',"opacity:0.5;");

}

}

});

 

</script>

/////////////////////

 

now on test.php

 

<div class="leadlist stable">

 

// div/area containing list

 

 

</div>

<div id="listpages">

 

 

<?php                      $per_page = 10 ;

$add=0;

$nt= date('Y-m-d H:i:s', strtotime('+7 days'));

$selectLead = "query";

$resultleads=mysql_query($selectLead);

$countleads = mysql_num_rows($resultleads);

if($countleads < $per_page)

{

$add=1;

}else{

$ex=$countleads%$per_page;

if($ex < 10 && $ex!=0)

{ $add=1;

}else {$add=0;}

}

$leadpages =floor($countleads/$per_page);

$leadpages=$leadpages+$add;

if($leadpages > 5){?>

<span class="first>First</span>

<span class="previous">Prev</span>

<?php for($i=1;$i<=5; $i++){

if($i==1){

?>

<span class="active"><?php echo $i; ?></span><?php }else{ ?>

<span><?php echo $i; ?></span>

<?php }

}?>

<span class="next">Next</span>

<span class="last">Last</span>

<?php }else if($leadpages <= 5){?>

<span class="first" disabled="true" style="opacity:0.5;">First</span>

<span class="previous" disabled="true" style="opacity:0.5;">Prev</span>

<?php for($i=1;$i<=$leadpages; $i++){

if($i==1){

?>

<span class="active"><?php echo $i; ?></span><?php }else{?>

<span ><?php echo $i; ?></span>

<?php }

}?><span class="next" disabled="true" style="opacity:0.5;">Next</span>

<span class="last" disabled="true" style="opacity:0.5;">Last</span>

<?php } ?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/236724-pagination-help/#findComment-1216924
Share on other sites

$id = 10;

$query = "SELECT id, (SELECT id FROM table WHERE id>'$id' ORDER BY id DESC LIMIT 1) AS 'prev',  (SELECT id FROM table WHERE id<'$id' ORDER BY id DESC LIMIT 1) AS 'next' WHERE id='$id' LIMIT 1";

 

EDIT: Changes to subqueries.

Link to comment
https://forums.phpfreaks.com/topic/236724-pagination-help/#findComment-1216936
Share on other sites

My first problem solved.. Thanks for who provide code....

however, i want to know how can i paginate something like <<PREV | 1 2 3 4 5 | NEXT>> when user reach page #5 and click NEXT link then page number should change <<PREV | 6 7 8 9 10 | NEXT>>

Link to comment
https://forums.phpfreaks.com/topic/236724-pagination-help/#findComment-1217375
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.