Nida Akram
Nida Akram

Reputation: 362

How to correct pagination in oracle?

I am showing user record of how many forms he/she has accepted and for that purpose I am using pagination in php. Data is fetched from oracle database and shown in the table. I am using pagination, but when I go on next page it shows the previous record's new records are not shown.

<table>
<tbody>
    <?php
    include('connect.php');
    $user_name = $_SESSION['username'];
    $sql = oci_parse($conn,"select * from userM where user_name = '$user_name'");
    $result = oci_execute($sql);

    while(($row = oci_fetch_array($sql,OCI_BOTH)) != false ){
        $approvedBy = $row[1];
    }
    if(!isset($_GET['page'])){
        $_GET['page'] = 0;
    }else{
        // Convert the page number to an integer
        $_GET['page'] = (int)$_GET['page'];
    }
    $varPage = $_GET['page'];
    if($varPage == "" || $varPage == "1"){
        $page1 = 0;
    }else{
        $page1 = ($varPage*6)-6;
    }
    $approvedBy = strtoupper($approvedBy);

    $sql = oci_parse($conn,"SELECT * FROM formC where UPPER(approvedBy) = '$approvedBy' AND rownum < 7");
    oci_execute($sql);

    while(($row = oci_fetch_array($sql,OCI_BOTH)) != false )  {
        $accountingNo = $row[0];
        ?>
        <tr>
            <td style="color:black"><?php echo  $row[0] ?></td>
            <td style="color:black"><?php echo  $row[1] ?></td>
            <td style="color:black"><?php echo  $row[2]  ?></td>
            <td style="color:black"><?php echo $row[3]?></td>
            <td style="color:black"><?php echo $row[4] ?></td>
            <td style="color:black"><?php echo $row[7] ?></td>         
        </tr>
        <?php 
    }
    ?>
</tbody>
</table>

<?php
$sql1 = oci_parse($conn,"SELECT * FROM formC where UPPER(approvedBy) = '$approvedBy'");
oci_execute($sql1);
$count = oci_fetch_all($sql1,$abc);
$a=$count/6;
$a = ceil($a);

for($b=1;$b<=$a;$b++){
    ?>
    <a href="approvedList.php?page=<?php echo $b?>" style="text-decoration;align:center: none;" class="btn btn-success "><?php echo $b." "; ?></a>
    <?php
}
oci_close($conn);
?>
</div> 

Upvotes: 1

Views: 7659

Answers (2)

timclutton
timclutton

Reputation: 13014

This is explained in the The Underground PHP and Oracle Manual, page 181:

The canonical paging query for Oracle8i onwards is given on http://asktom.oracle.com:

select *
  from ( select a.*, rownum as rnum
         from (YOUR_QUERY_GOES_HERE -- including the order by) a
         where rownum <= MAX_ROW )
  where rnum >= MIN_ROW

Here, MIN_ROW is the row number of first row and MAX_ROW is the row number of the last row to return.

There is even a full example script:

<?php
$c = oci_connect('hr', 'welcome', 'localhost/XE');
$mystmt = "select city from locations order by city";
$minrow = 4; // row number of first row to return
$maxrow = 8; // row number of last row to return
$pagesql = "select *
            from ( select a.*, rownum as rnum
                   from ( $mystmt ) a
                   where rownum <= :maxrow)
            where rnum >= :minrow";
$s = oci_parse($c, $pagesql);
oci_bind_by_name($s, ":maxrow", $maxrow);
oci_bind_by_name($s, ":minrow", $minrow);
oci_execute($s);
oci_fetch_all($s, $res);
var_dump($res);
?>

Edit:

As mentioned by Christopher Jones, in Oracle >=12.1 there is a cleaner way to do this using the offset clause:

select *
from mytable
order by myfield
offset X rows fetch next Y rows only

Upvotes: 5

aidinMC
aidinMC

Reputation: 1436

I'm not oracle guy but I'm not suggest you select all rows and count them,I think it's better to use this code to count rows:

$sql1 = oci_parse($conn,"SELECT COUNT(*) FROM formC where UPPER(approvedBy) = '$approvedBy'");
    oci_execute($sql1);
$count = oci_fetch_all($sql1,$abc);
$a=$count/6;

oracle count documentation

to resolve your problem see this question:How do I limit the number of rows returned by an Oracle query after ordering?

you calculate current page and saved it in $page1 but did not use it in your query!!

Upvotes: 0

Related Questions