php paging with odbc

Discussion in 'PHP' started by shahrinn, May 18, 2008.

  1. #1
    hi friend,

    below is my code that i get from internet originally used sybase function and i change it to odbc function,the data list nicely but when i click at pages (1,2,3,4....) or next and last the page doesnt jump to next page can anyone help me.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>RC4PHP TEST</title>
    </head>

    <body>
    <?php
    $conn = odbc_connect("mydsn", "dba", "sql");

    $sql="select * from tablename";
    $qry = odbc_exec($conn,$sql);

    $max = odbc_num_rows($qry);
    $maxrec = 15; # control the no. of records per page
    $pgmax = 10; # control the paging number to be displayed
    $const = $maxrec * $pgmax; # maximum no. of record count in a page (used in hyperlinks)
    $remainder = bcmod($max,$maxrec); #determines if the maximum no. of query result is divisible by $maxrec

    $max_no_page = ($max-$remainder)/$maxrec; #determines the maximum no. of pages in a query result
    if($remainder > 0 and $max > $maxrec) ++$max_no_page;

    #Record Loop Controller
    #$startrec -- the starting position of the data or record to be fetched
    #$endrec -- determines the last data or record to be fetched
    $currpg='';
    if($max < $maxrec){ # checks if the no. of query result is less than $maxrec
    $startrec = 0; #sets the initial value of the $startrec and $endrec
    $endrec = $max;
    }
    elseif($max > $maxrec){# checks if the no. of query result is greater than $maxrec
    if($currpg == ""){ # sets the initial value of the $startrec and $endrec
    $startrec = 0;
    $endrec = $maxrec;
    }
    elseif($currpg != "" and $currpg != $max_no_page) { # sets the new value of the $startrec and $endrec
    $startrec = $postn;
    $endrec = $postn + $maxrec;
    }
    elseif ($currpg == $max_no_page){ # for correct display of the Last Records
    $startrec = $postn;
    $endrec = $max;
    }
    }
    #End of Record Loop Controller




    for ($i = $startrec; $i < $endrec; ++$i) {
    if (!odbc_cursor ($qry, $i)) {
    echo "Cannot seek to row $i\n";
    continue;
    }

    if(!($row = odbc_fetch_object($qry)))
    continue;

    echo "$row->FullName $row->HomeAddress <br />\n"; # Displays the record selected from Database (i.e. depends on your SQL Query).
    }

    if ($max > $maxrec) { # checks if the query results return more than $maxrec

    #Page Controller
    #$start -- specify the starting point of Paging Display
    #$end -- specify the end point of the Paging Display
    #$currpg -- refers to the current page selected
    #$prev -- previous 10 pages
    #$next -- next 10 pages
    #$first -- go to first page
    #last -- go to last page

    if ($currpg == "" and $max_no_page >= $pgmax){
    $start = 1; # sets initial value of $start and $end
    $end = $pgmax;
    }
    elseif ($currpg == "" and $max_no_page < $pgmax){
    $start = 1; #sets initial value of $start and $end
    $end = $max_no_page;
    }
    elseif ($currpg != "" and $currpg > $pgmax){ # sets the value $start and $end dynamically
    #sets the $start value
    $temp = bcmod($currpg,$pgmax);
    if ($temp == 0){$start = ($currpg + 1) - $pgmax;}
    else{$start = ($currpg + 1) - $temp;}

    #sets the $end value
    $temp = $max_no_page - bcmod($max_no_page,$pgmax); # was also used in Next & Last Page Link
    if($currpg <= $temp) $end = $start + ($pgmax - 1);
    elseif(($max_no_page - $currpg) < $pgmax) $end = $max_no_page; #value for the last batch of records
    }
    else{ # currpg is not null and less than $pgmax
    $start = 1; #sets initial value for $start and $end
    $end = $pgmax;
    }

    # End of Page Controller


    #######Retains the current record position that is to be fetched from the database
    $postn=1;
    if ($postn != "" and $postn >= $const)
    {
    $temp1 = bcmod($postn,$const);
    $newtemp1 = ($postn-$temp1)/$const;
    $pos = ($newtemp1 * $const) - $maxrec;
    }
    elseif ($postn != ""){$pos = $postn - $maxrec;}
    elseif($postn == "" and $postn >= $const)
    {
    $temp1 = bcmod($postn,$const);
    $newtemp1 = ($postn-$temp1)/$const;
    $pos = ($newtemp1 * $const) - $maxrec;
    }
    #######End the current record retainer

    #Previous & First Page Link

    if($currpg > $pgmax){
    $leaves = $start - 1; #value to be assigned to the currpg of Previous Link
    echo " <a href=test.php>First</A> ";
    echo " <a href=test.php?currpg=$leaves&postn=$pos>Previous</A> ";
    }

    #End of Previous & First Page Link

    # Paging Display (i.e. [ 1 2 3 . . .])

    echo "[ ";

    for ($pg = $start; $pg <= $end; ++$pg){

    if($pg == 1){$pos = 0;}
    else $pos = $pos + $maxrec;

    if($start == $pg and $currpg == "") echo " $pg ";
    elseif($pg == $currpg) echo " $pg ";
    else{
    echo " <a href=test.php?currpg=$pg&postn=$pos>$pg</A> ";
    }
    }

    echo " ]";
    # end of paging Display

    #Next & Last Page Link

    $temp = bcmod($currpg,$pgmax);
    if ($temp == 0){$temp = ($currpg + 1) - $pgmax;}
    else{$temp = ($currpg + 1) - $temp;}
    $temp = $max_no_page - $temp; # determines the starting position of the navigatinal control (i.e. [*1* 2 3 ...] )
    # *1* -- denotes the starting position

    if($temp >= $pgmax){
    $pos = $pos + $maxrec;
    echo " <a href=test.php?currpg=$pg&postn=$pos>Next</A> ";
    $pos = $max - $remainder;
    echo " <a href=test.php?currpg=$max_no_page&postn=$pos>Last</A> ";
    }

    #End of Next & Last Page Page Link

    }

    odbc_close($conn);

    ?>
    </body>
    </html>

    thanks in advance
     
    shahrinn, May 18, 2008 IP
  2. sameehyousef

    sameehyousef Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You must chane the following code

    if(!($row = odbc_fetch_object($qry)))
    continue;
    to be

    if(!($row = odbc_fetch_row($qry,$i)))
    continue;

    and every thing will run correcllty

    sameeh Yousef
     
    sameehyousef, Oct 8, 2008 IP