Nida Akram Nida Akram - 11 months ago 34
PHP Question

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>

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download