2012-03-14 75 views
0

我一直在努力想出我的web應用程序的分頁腳本的最佳解決方案。我已經研究得很好,知道在使用rownum的SQL Server中有LIMIT的替代方案,但是我無法得到我想要的結果。這是我這要是一臺包含數百個產品,這將是一個非常糟糕的方式做到這一點舊的代碼,所有我做的是改變了查詢rownums內的範圍:有沒有更好的方法來做類似於SQL Server 2008中的LIMIT?

$page = $_GET['pageinfo']; 

    if (!(isset($page))) 

    { 
     $page=1; 
    } 


     if($page==1) 
     { 

     $query1 = " SELECT * 
     FROM (SELECT ROW_NUMBER() OVER(ORDER BY range) AS 
       rownum, productID, productName, category, range, price, picture, description FROM products) AS products1 
     WHERE rownum >= 0 AND rownum <= 9"; 


     $result1 = sqlsrv_query($conn, $query1, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET)); 



      if($result1 === false) 
      { 
       echo "Error in query preparation/execution.\n"; 
       die(print_r(sqlsrv_errors(), true)); 
      } 


       while($row = sqlsrv_fetch_array ($result1, SQLSRV_FETCH_ASSOC)) 
      { 
       echo '<p class="productsContent">'.$row['productName']; 
       echo '<br/>'.$row['description']; 
       echo '<br/>'.$row['category']; 
       echo '<br/>'.$row['range']; 
       echo '<br/>&pound;'.$row['price']; 
       echo '<br/><br/><a href="products.php?productID='.$row['productID'].'"><img src="' . $row['picture'] .'" alt="' . $row['productName'] .'" title="' . $row['productName'] .'" /></a>'; 

       echo '<br/><br/></p>'; 
      } 

     echo'<a href="products.php?pageinfo=2&amp;main=go">Page two</a>'; 
    } 

    else if($page==2) 
     { 

     $query2 = " SELECT * 
     FROM (SELECT ROW_NUMBER() OVER(ORDER BY range) AS 
       rownum, productID, productName, category, range, price, picture, description FROM products) AS products1 
     WHERE rownum >= 10 AND rownum <= 20"; 


     $result2 = sqlsrv_query($conn, $query2, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET)); 


    if($result2 === false) 
    { 
     echo "Error in query preparation/execution.\n"; 
     die(print_r(sqlsrv_errors(), true)); 
    } 


     while($row = sqlsrv_fetch_array ($result2, SQLSRV_FETCH_ASSOC)) 
    { 
     echo '<p class="productsContent">'.$row['productName']; 
     echo '<br/>'.$row['description']; 
     echo '<br/>'.$row['category']; 
     echo '<br/>'.$row['range']; 
     echo '<br/>&pound;'.$row['price']; 
     echo '<br/><br/><a href="products.php?productID='.$row['productID'].'"><img src="' . $row['picture'] .'" alt="' . $row['productName'] .'" title="' . $row['productName'] .'" /></a>'; 
     echo '<br/><br/></p>'; 

    } 

     echo'<a href="products.php?pageinfo=1&amp;main=go">Page one</a>'; 

     } 

    } 

我跟了一個教程似乎有一個更好的解決方案,但這是使用MySQL完成的,而且我被卡在SQL Server中。這裏是我的最新代碼:

if (!(isset($pagenum))) 

    { 

    $pagenum = 1; 

    } 

     $page_rows = 9; 


     $stmt = sqlsrv_query($conn, "select * from products $max" , array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET)); 

      $rows = sqlsrv_num_rows($stmt); 

     $last = ceil($rows/$page_rows); 

       if ($pagenum < 1) 

        { 

        $pagenum = 1; 

        } 

        elseif ($pagenum > $last) 

        { 

        $pagenum = $last; 

        } 


     $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 




       while($info = sqlsrv_fetch_array($stmt)) 

       { 

       Print $info['description']; 

       echo "<br>"; 

       } 

       echo "<p>"; 


        echo " --Page $pagenum of $last-- <p>"; 


        echo " --Page $pagenum of $last-- <p>"; 


        if ($pagenum == 1) 

        { 

        } 

        else 

        { 

        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> "; 

        echo " "; 

        $previous = $pagenum-1; 

        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> "; 

        } 


        if ($pagenum == $last) 

          { 

          } 

          else { 

          $next = $pagenum+1; 

          echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> "; 

          echo " "; 

          echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> "; 

          } 

你可以看到它使用「限制」,它之前,我意識到這不是SQL Server支持我花了一段時間。

所以我的問題是混合我的兩種方法,我只是無法弄清楚如何有效地做到這一點,任何建議將不勝感激。

回答

1

這是一個非常通用的模板。

with rnkd as (select row_number() over (order by [yoursortfield]) rn, * 
       from [yourtable]) 
select * 
from rnkd 
where rn >= @start and rn < @end 
order by rn 

或者,您可以改用top運算符。通過對RN的順序很重要的位置:

with rnkd as (select row_number() over (order by [yoursortfield]) rn, * 
       from [yourtable]) 
select top (@pagesize) * 
from rnkd 
where rn >= @start 
order by rn 

或者,您可避免CTE和做的派生表,但我個人比較喜歡的CTE。

select top (@pagesize) * 
from (select row_number() over (order by [yoursortfield]) rn, * 
     from [yourtable]) rnkd 
where rn >= @start 
order by rn 
+0

非常感謝,這讓我感謝數字,感謝您的意見! – deucalion0 2012-03-15 22:00:18

相關問題