views:

62

answers:

1

Hey guys I am really messing up with this.

This is I'm doing: If there are 100 users to fetch from Database, I'm using pagination and showing 10 users at a time. when user will click on next page, he will get next 10 users through ajax(called ajax on click) and so on. I'm showing 10 page-links right now with first, next last and previous links.

This is how flow will go: On a.php created links and called ajax function with every link, passing url(b.php) & target(where I will get result), with url also passing clicked pageno., this pageno. will go to b.php and next 10 users will be shown with the help of ajax.

This is the problem: Currently I'm showing 1-10 links with first and last links, unable to show next and previous links because to redirect to next or previous, I am not getting the current page number on a.php i.e i'm passing to b.php. also links are created in foreach loop. I am trying hard to get this done, but no success yet.

waiting for valuable reply.

A: 

Ok, I think I understand...

I think what you need to do is store the current page number as a JavaScript variable. Use the function that is doing the Ajax to update this variable and also update the next/previous links.

Hope this helps.

Update: On second thoughts, here's a better idea. Having your ajax call directly in your link means you have to put together a url and update it, which is awkward and annoying. This isn't ideal. Instead it is better to store the variables and have some set functions work with them in a set way. So, you could have something like:

<a href="javascript:void(0)" onclick="gotoNextPage()">

Rather than passing them the number of the page to go to, you can store the current page number in a javascript variable and do something like this:

gotoNextPage(){
    // lets assume the current page number is stored in a 
    // variable defined outside of this function called curr_page
    curr_page++; // increment the current
    call_ajax('user_info.php?pageno='+curr_page);
}

This way the code for the link doesn't ever change.

You can still pass other variables (such as 'order', 'perpage' etc) to gotoNextPage() but you might find you don't need to.

A couple of things to bear in mind: 1. Using jQuery or something similar would probably make things easier for you. 2. Anyone without javascript enabled will not be able to use your site. Consider changing it to something like

<?php
    echo '<a href="your_main_page.php?page_number=', ($curr_page + 1), '" onclick="gotoNextPage()">';
?>

This way it would work for via the ajax method but would also work (if not as smoothly and with a page reload) for people without javascript. It's a bit more work for you though...so hat's your choice!

Hope this makes sense. It's been a long day!

Mr_Chimp
Hey, It's quite confusing here, as you said to udate the next and previous links with page no. I tried to do that with calling function, but I dont know how can i update the php variable(in next and previous variable) in javascript function. see this is the code for link:echo "<a id =\"$p\" class=\"gav_pn\" href=\"javascript:void(0)\" onclick=\"call_ajax('userinfo.php?pageno1=$p\" style='text-decoration:none'><font-size = '14'><strong>$p</strong></font></a>"." "; so this is the link can you shed dome light please.
Rishi2686
I've updated my post with a better example.
Mr_Chimp
Did you ever solve this?
Mr_Chimp