Reputation: 27
hy, i have a problem to set the pagination using PHP and oracle database, the page only show the first page value. when i click button next, the page change from page 1 to page 2, page 3, etc, but the value still same with page 1. i dont know and i dont have any idea to fix this error..
here is my code for set up record set ..
<?php
// Set up recordset
define("ewSqlSelectCount", "SELECT count(*) count FROM sid_mst_dealer", true);
$sSql_count = BuildSqlang(ewSqlSelectCount, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, $sDbWhere, $sOrderBy);
//echo "$sSql_count" . "<br/ >";
$rs_count = moi_query($sSql_count , $conn) or die("Failed to execute query at line " . __LINE__ . ": " . moi_error($conn) . '<br>SQL: ' . $sSql);
//echo $rs_count;
oci_execute($rs_count);
$nTotalRecs = oci_fetch_array($rs_count);
$nTotalRecs = $nTotalRecs['COUNT'];
$rs = moi_query($sSql, $conn) or die("Failed to execute query at line " . __LINE__ . ": " . moi_error($conn) . '<br>SQL: ' . $sSql);
//echo $rs;
oci_execute($rs);
if ($nDisplayRecs <= 0) { // Display all records
$nDisplayRecs = $nTotalRecs;
}
$nStartRec = 1;
SetUpStartRec(); // Set up start record position
?>
this is the function..
function SetUpStartRec()
{
// Check for a START parameter
global $nStartRec;
global $nDisplayRecs;
global $nTotalRecs;
if (strlen($_GET[ewTblStartRec]) > 0)
{
$nStartRec = $_GET[ewTblStartRec];
$_SESSION[ewSessionTblStartRec] = $nStartRec;
} elseif (strlen($_GET["pageno"]) > 0)
{
$nPageNo = $_GET["pageno"];
if (is_numeric($nPageNo))
{
$nStartRec = ($nPageNo-1)*$nDisplayRecs+1;
if ($nStartRec <= 0)
{
// echo 'jangan ke sini';
$nStartRec = 1;
}
elseif ($nStartRec >= (($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1)
{
$nStartRec = (($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1;
}
$_SESSION[ewSessionTblStartRec] = $nStartRec;
}
else
{
$nStartRec = $_SESSION[ewSessionTblStartRec];
if (!(is_numeric($nStartRec)) || ($nStartRec == ""))
{
$nStartRec = 1; // Reset start record counter
$_SESSION[ewSessionTblStartRec] = $nStartRec;
}
}
}
else
{
$nStartRec = @$_SESSION[ewSessionTblStartRec];
if (!(is_numeric($nStartRec)) || ($nStartRec == "")) {
$nStartRec = 1; // Reset start record counter
$_SESSION[ewSessionTblStartRec] = $nStartRec;
}
}
}
and here is the query
<?php
define("ewTblVar", "sid_mst_dealer", true);
define("ewTblRecPerPage", "RecPerPage", true);
define("ewSessionTblRecPerPage", "sid_mst_dealer_RecPerPage", true);
define("ewTblStartRec", "start", true);
define("ewSessionTblStartRec", "sid_mst_dealer_start", true);
define("ewTblShowMaster", "showmaster", true);
define("ewSessionTblMasterKey", "sid_mst_dealer_MasterKey", true);
define("ewSessionTblMasterWhere", "sid_mst_dealer_MasterWhere", true);
define("ewSessionTblDetailWhere", "sid_mst_dealer_DetailWhere", true);
define("ewSessionTblAdvSrch", "sid_mst_dealer_AdvSrch", true);
define("ewTblBasicSrch", "psearch", true);
define("ewSessionTblBasicSrch", "sid_mst_dealer_psearch", true);
define("ewTblBasicSrchType", "psearchtype", true);
define("ewSessionTblBasicSrchType", "sid_mst_dealer_psearchtype", true);
define("ewSessionTblSearchWhere", "sid_mst_dealer_SearchWhere", true);
define("ewSessionTblSort", "sid_mst_dealer_Sort", true);
define("ewSessionTblOrderBy", "sid_mst_dealer_OrderBy", true);
define("ewSessionTblKey", "sid_mst_dealer_Key", true);
// Table level SQL
define("ewSqlSelect", "SELECT * FROM sid_mst_dealer", true);
if($_REQUEST[dealer_id]==""){
if($_REQUEST[x_status]!=""){
define("ewSqlWhere", " active_flag ='".$_REQUEST[x_status]."'", true);
}else{
define("ewSqlWhere", "active_flag='0'", true);
}
}else{
define("ewSqlWhere", "", true);
}
define("ewSqlGroupBy", "", true);
define("ewSqlHaving", "", true);
define("ewSqlOrderBy", "", true);
define("ewSqlOrderBySessions", "", true);
define("ewSqlKeyWhere", "dealer_id = '@dealer_id'", true);
define("ewSqlUserIDFilter", "", true);
?>
i really need help to fix this.. thank u :D
Upvotes: 1
Views: 178
Reputation: 16963
As stated by @RamRaider your methodology for pagination is somewhat vague. As of Oracle 12c, you could use the following example query to implement pagination.
SELECT fieldA,fieldB
FROM table
ORDER BY fieldA
OFFSET 5 ROWS FETCH NEXT 14 ROWS ONLY;
Visit this page to learn more about row limiting clause for top-N queries in Oracle Database 12c Release 1 (12.1)
Upvotes: 0
Reputation: 27
here is the pagination..
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><span >Page </span> </td>
<!--first page button-->
<?php if ($nStartRec == 1) { ?>
<td><img src="images/firstdisab.gif" alt="First" width="16" height="16" border="0"> </td>
<?php } else { ?>
<td><a href="sid_mst_dealerlist.php?start=1&x_status=<?php echo $_REQUEST[x_status]; ?>"><img src="images/first.gif" alt="First" width="16" height="16" border="0"></a> </td>
<?php } ?>
<!--previous page button-->
<?php if ($PrevStart == $nStartRec) { ?>
<td><img src="images/prevdisab.gif" alt="Previous" width="16" height="16" border="0"> </td>
<?php } else { ?>
<td><a href="sid_mst_dealerlist.php?start=<?php echo $PrevStart; ?>&x_status=<?php echo $_REQUEST[x_status]; ?>"><img src="images/prev.gif" alt="Previous" width="16" height="16" border="0"></a> </td>
<?php } ?>
<!--current page number-->
<td><input type="text" name="pageno" value="<?php echo intval(($nStartRec-1)/$nDisplayRecs+1); ?>" size="4"> </td>
<!--next page button-->
<?php if ($NextStart == $nStartRec) { ?>
<td><img src="images/nextdisab.gif" alt="Next" width="16" height="16" border="0"> </td>
<?php } else { ?>
<td><a href="sid_mst_dealerlist.php?start=<?php echo $NextStart; ?>&x_status=<?php echo $_REQUEST[x_status]; ?>"><img src="images/next.gif" alt="Next" width="16" height="16" border="0"></a> </td>
<?php } ?>
<!--last page button-->
<?php if ($LastStart == $nStartRec) { ?>
<td><img src="images/lastdisab.gif" alt="Last" width="16" height="16" border="0"> </td>
<?php } else { ?>
<td><a href="sid_mst_dealerlist.php?start=<?php echo $LastStart; ?>&x_status=<?php echo $_REQUEST[x_status]; ?>"><img src="images/last.gif" alt="Last" width="16" height="16" border="0"></a> </td>
<?php } ?>
<td><span > of <?php echo intval(($nTotalRecs-1)/$nDisplayRecs+1);?></span> </td>
</tr>
</table>
<?php if ($nStartRec > $nTotalRecs) { $nStartRec = $nTotalRecs; }
$nStopRec = $nStartRec + $nDisplayRecs - 1;
$nRecCount = $nTotalRecs - 1;
if ($rsEof) { $nRecCount = $nTotalRecs; }
if ($nStopRec > $nRecCount) { $nStopRec = $nRecCount; } ?>
<span >Records <?php echo $nStartRec; ?> to <?php echo $nStopRec; ?> of <?php echo $nTotalRecs; ?></span>
<?php } else { ?>
<?php if ($sSrchWhere == "0=101") {?>
<span ></span>
<?php } else { ?>
<span >No records found</span>
<?php } ?>
<?php } ?> </td>
</tr>
</table>
Upvotes: 0
Reputation: 33813
I think the problem is the lack of a limit within that obscure sql which is the key piece to the puzzle in terms of pagination. If you consider the following:
select * from `users` where `name`='fred' limit 0,10;
That would show the first 10 records where the user is called "Fred" and then
select * from `users` where `name`='fred' limit 10,10;
The second statement would show the next 10 records where the user is called "Fred"
I simply don't understand your methodology in constructing the sql but IMO you need to add a limit - and the logic associated to calculate which page
in the recordset you are on so that you can add next/previous links.
Upvotes: 0