我正在显示他/她已接受多少表单的用户记录,为此我在php中使用分页。数据从oracle数据库中获取并显示在表中。我正在使用分页,但当我进入下一页时,它显示前一条记录的新记录未显示。
<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>
答案 0 :(得分:3)
The Underground PHP and Oracle Manual,第181页
中对此进行了解释Oracle8i以后的规范分页查询在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
这里,MIN_ROW是行号 第一行和MAX_ROW是要返回的最后一行的行号。
甚至还有一个完整的示例脚本:
<?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); ?>
<强> 编辑: 强>
作为mentioned by Christopher Jones,在Oracle&gt; = 12.1中,有一种更简洁的方法可以使用offset clause执行此操作:
select *
from mytable
order by myfield
offset X rows fetch next Y rows only
答案 1 :(得分:0)
我不是oracle人,但我不建议您选择所有行并计算它们,我认为最好使用此代码来计算行数:
$page1
要解决您的问题,请参阅此问题:How do I limit the number of rows returned by an Oracle query after ordering?
您计算当前页面并将其保存在{{1}}中但未在查询中使用!!