1

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

user3661054
  • 27
  • 1
  • 1
  • 5
  • That code is very hard to read - is there a `limit` clause lurking in there somewhere? – Professor Abronsius Nov 10 '15 at 08:20
  • @RamRaider i also have the same problem with you, this is not my code, i only do the enhancement for this. so i dont have any idea for this.. really confused.. i tried to understand this, but it's hard. – user3661054 Nov 10 '15 at 08:32
  • @user3661054 If you need to implement pagination query, then look at [**How pagination query works**](http://stackoverflow.com/questions/30321483/how-rownum-works-in-pagination-query/30321788#30321788) – Lalit Kumar B Nov 10 '15 at 08:35

3 Answers3

0

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.

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • Is that supposed to be Oracle SQL syntax? Because `LIMIT` is not supported in Oracle. – Lalit Kumar B Nov 10 '15 at 08:31
  • which - the sql above? No - it is not supposed to be Oracle SQL, perhaps there is no `limit` in `Oracle` ~ never written sql for an Oracle db – Professor Abronsius Nov 10 '15 at 08:33
  • What is mean is, OP is using Oracle database. If that query is going to be executed on an Oracle database, it would fail due to syntax error since `LIMIT` is not supported by Oracle. There are different ways of pagination in Oracle. For **pre-12c releases**, use `ROWNUM` for pagination query. **12c** provides the Top-n query using `FECTH`. http://stackoverflow.com/questions/30321483/how-rownum-works-in-pagination-query/30321788#30321788 – Lalit Kumar B Nov 10 '15 at 08:34
  • so, what is the best answer for this error? is there any mistake with the sql query ?? – user3661054 Nov 10 '15 at 08:37
  • @user3661054 I don't see any pagination in your code at all. – Lalit Kumar B Nov 10 '15 at 08:38
0

here is the pagination..

<table border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td><span >Page&nbsp;</span>&nbsp;</td>
                      <!--first page button-->
                      <?php if ($nStartRec == 1) { ?>
                      <td><img src="images/firstdisab.gif" alt="First" width="16" height="16" border="0">&nbsp;</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>&nbsp;</td>
                      <?php } ?>
                      <!--previous page button-->
                      <?php if ($PrevStart == $nStartRec) { ?>
                      <td><img src="images/prevdisab.gif" alt="Previous" width="16" height="16" border="0">&nbsp;</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>&nbsp;</td>
                      <?php } ?>
                      <!--current page number-->
                      <td><input type="text" name="pageno" value="<?php echo intval(($nStartRec-1)/$nDisplayRecs+1); ?>" size="4">&nbsp;</td>
                      <!--next page button-->
                      <?php if ($NextStart == $nStartRec) { ?>
                      <td><img src="images/nextdisab.gif" alt="Next" width="16" height="16" border="0">&nbsp;</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>&nbsp;</td>
                      <?php  } ?>
                      <!--last page button-->
                      <?php if ($LastStart == $nStartRec) { ?>
                      <td><img src="images/lastdisab.gif" alt="Last" width="16" height="16" border="0">&nbsp;</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>&nbsp;</td>
                      <?php } ?>
                      <td><span >&nbsp;of <?php echo intval(($nTotalRecs-1)/$nDisplayRecs+1);?></span>&nbsp;</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 } ?>&nbsp;</td>
              </tr>
            </table>
user3661054
  • 27
  • 1
  • 1
  • 5
0

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)

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37