Pagination problem

Discussion in 'Programming/Scripts' started by fedora777, Jan 30, 2010.

  1. fedora777

    fedora777 New Member

    Hello friends,

    I have another problem. This is a php pagination in my administration control of my php script. I added a pagination code and I can see the:

    <Previous 2 | 4 | 6 | Next >>>

    But I the admin page that I want to have the paging applied does not take the code to generate the paging accordingly. Above problem:

    1. It is showing only the even numbers.
    2. You click "previous" nothing is shown on the page but the same long page (I have scroll way down and search for the stuff I am looking).
    3. The paging do not follow the results I have in my administration page.

    Can somebody please help me?

    Thanks again.
  2. falko

    falko Super Moderator ISPConfig Developer

    Can you post the PHP code?
  3. fedora777

    fedora777 New Member

    Helllo Falko,

    Below is the php script. I am making a record of the list of articles in my database. I can access all the list articles in the admin panel however, the pagination which is supposed to show the list of articles in a minimum of 50 rows per page with the following pagination style:
    Main | Last Page
    <previous | 2 | 3 | 4 | 5 | Next >>>>

    is not doing it.
    Below is the script:

    function showarticles($message) {

    global $mysql_table_prefix;

    $result = mysql_query("SELECT article_id, url, title, date from ".$mysql_table_prefix."articles ORDER By date, title");

    echo mysql_error();
    //added pagination here -- Need to let the page number to show accordingly
    if (isset($_GET["page"])) {
    // $NumberOfPages=ceil($result/$Limit);
    //if ($page == "") {
    $page = $_GET['page'];
    //$start = ($page - 1) * $Limit; //If no page number is set, the default page is 1
    } else {

    $page = 1;

    //Get the number of results
    $result=mysql_query("SELECT article_id, url, title, date from ".$mysql_table_prefix."article ORDER By date, title") or die(mysql_error());
    $query_data = mysql_fetch_row($result);
    $numrows = $query_data[0];

    //Get the number of pages
    $row = $row['site_id'];
    $numrows = $query_data[0];
    $LastPage = ceil($numrows/$rows);

    //Print the Titles
    $page = (int)$page;
    if ($page > $LastPage) {
    $page = $LastPage;
    //Get only the relevant info for the current page using LIMIT
    $limit = 'LIMIT' .($page - 1) * $rows .'.' .$rows;
    $query = @mysql_query("SELECT * FROM article WHERE article_Id LIKE '.$mysql_table_prefix.' ORDER BY article_id");
    $result = mysql_query("SELECT article_id, url, title, indexdate from ".$mysql_table_prefix."article ORDER By date, title");

    $query_data = @mysql_fetch_row($result);
    echo mysql_error();
    if($page == 1) {
    } else {
    echo " <a href=\"admin.php?page=" . ($page=1) . ".$mysql_table_prefix.=" .urlencode($mysql_table_prefix) . "\">MAIN PAGE</a> ";
    .urlencode($$mysql_table_prefix) . "\">FIRST PAGE</a> ";
    $prevpage = $page-1;
    .urlencode($$mysql_table_prefix) . "\">PREVIOUS</a> ";
    $Nav .= "<A HREF=\"admin.php?page=" . ($page-1) . ".$mysql_table_prefix.=" .urlencode($mysql_table_prefix) . "\"> << Previous |</A>";
    for($i = 1 ; $i <= $LastPage ; $i++) {
    if($i == $page/ $i++) {
    $Nav .= "<B>$i</B>";

    else {
    $Nav .= "<A HREF=\"admin.php?page=" . ".$mysql_table_prefix.=" .urlencode($mysql_table_prefix) . "\">| $i |</A>"; }
    if($page < $LastPage) {
    $Nav .= "<A HREF=\"admin.php?page=" . ($page+1) . ".$mysql_table_prefix.=" .urlencode($mysql_table_prefix) . "\"> | Next >></A>";
    echo " <a href=\"admin.php?page=" . ($page=$LastPage) . ".$mysql_table_prefix=" .urlencode($mysql_table_prefix) . "\"> | LAST PAGE</a> ";
    echo "<BR><BR>" . $Nav;

    //Pagination ends here

    I am making of a lot changes here and there, and I seem to lose my way of fixing it up. I definitely appreciate your help on this.

    Thank you very much.

    Last edited: Jan 31, 2010
  4. falko

    falko Super Moderator ISPConfig Developer

    This could be the problem:

    for($i = 1 ; $i <= $LastPage ; $i++) {
    if($i == $page/[COLOR="Red"][B] $i++[/B][/COLOR]) {
    $Nav .= "<B>$i</B>";
    else {
    $Nav .= "<A HREF=\"admin.php?page=" . ".$mysql_table_prefix.=" .urlencode($mysql_table_prefix) . "\">| $i |</A>"; }
    What happens if you just use
    if($i == $page) {
  5. fedora777

    fedora777 New Member


    Hello Falko,

    I posted my response and I think it got deleted.

    Anyway, your php code:
    if($i == $page)

    resulted to correct consecutive numbers as:

    1 | 2 | 3 | 4

    However, the actual result in my admin page shows as this:

    <<previous |1 | 2 | 3 | 4 | next >>>

    1. so I am just wondering why previous would appear ahead of "1"?:confused:

    Also, other problems persist such as:

    1. when i click the numbers it doesn't direct me to the actual page whatever the number is that is higher than 1.:confused:
    2. "Main Page" is not showing anymore.:confused:

    Thanks again for your further assistance.
  6. falko

    falko Super Moderator ISPConfig Developer

    Shouldn't this
    else {
    $Nav .= "<A HREF=\"admin.php?page=" . ".$mysql_table_prefix.=" .urlencode($mysql_table_prefix) . "\">| $i |</A>"; }
    else {
    $Nav .= "<A HREF=\"admin.php?page=" .[B][COLOR="Red"]$i.[/COLOR][/B] ".$mysql_table_prefix.=" .urlencode($mysql_table_prefix) . "\">| $i |</A>"; }
  7. fedora777

    fedora777 New Member


    Hello Falko,

    I applied the changes from this code:

    else {
    $Nav .= "<A HREF=\"admin.php?page=" . ".$mysql_table_prefix.=" .urlencode($mysql_table_prefix) . "\">| $i |</A>"; }

    to this code:

    else {
    $Nav .= "<A HREF=\"admin.php?page=" . ".$mysql_table_prefix.=" .urlencode($mysql_table_prefix) . "\">| $i |</A>"; }

    Still I didn't see any changes. The problem still persist::(

    1. "<<Previous" is still showing ahead of " 1| 2 | 3 | Next >>>":confused:
    2. I click the links of "2, 3, or Next" it is not doing anything.
    I hope you don't mind giving further assistance on this.

    Thanks always.
  8. falko

    falko Super Moderator ISPConfig Developer

    What exactly did you change? Both codes look the same to me.
  9. fedora777

    fedora777 New Member

    Pagination issues

    Hello Falko,

    Oh, sorry. I changed it to:

    else {
    $Nav .= "<A HREF=\"admin.php?page=" .$i. ".$mysql_table_prefix.=" .urlencode($mysql_table_prefix) . "\">| $i |</A>"; }

    I applied the "$i" you added.

    Thank you for your patience.
  10. stuart4487

    stuart4487 New Member

    It should be shown automatically. Can you print out $sql variable, just before execution. This way you can see if it is constructed correctly.

    $sql= "select ISBN, name, class_number from book where name like '%$test%' LIMIT $offset, $rowsperpage";
    print "$sql<br>";
    $result= mysql_query ($sql);

    Than you can see sql statment for every page (you could post that sql here, it would help).

    I have noticed one more problem, and it is probably the cause of all this.
    You use COUNT(*) to get number of records in your table. Then you use query with condition '%$test%' for displaying actual records. Count returns all records in your table, but there are probably much less records that match the condition. That way you will get more pages then it should be. If you need condition (this is some kind of search), then you should put it in COUNT also.
    Something like this:

    $sql = "SELECT COUNT(*) FROM book WHERE name like '%$test%'";

    This way you will get number of pages that match the condition. I believe this is why you get empty page.

  11. timtimcarly

    timtimcarly New Member

    I'd probably agree with Stuart's reasoning here. It looks like the issue is with the COUNT function, which could be to blame for the blank pages. Have you tried and tested this to see if this is the problem and whether it works or not?

Share This Page