2012-08-15 175 views
-5

我想檢查頁數後,使用LIMIT關鍵字在SELECT語句中使用LIMIT關鍵字進行分頁,我已經使用了2個循環,FOR循環用於標題,WHILE循環用於結果行,並限制使用LIMIT, 需要知道,如果我可以在LIMIT關鍵字後使用變量。我的代碼如下:mysql LIMIT關鍵字分頁工作不起作用

<?php 
    include 'dbconnect.php'; 

    ?> 

    //--CHECKING number of records from STOCK table------------------------------------ 
    $di=mysql_query("SELECT * FROM CYLSTOCK_draft WHERE (DC_Date BETWEEN '$startdat' AND '$enddat' AND Ac_code='$custno' AND Product_Desc='$gas') OR (Ecr_dt BETWEEN '$startdat' AND '$enddat' AND Ac_code='$custno' AND Product_Desc='$gas') ORDER BY DC_Date"); 

    $numrows=mysql_num_rows($di); 

    // number of rows to show per page 
    $rowsperpage = 30; 
    // find out total pages 
    $totalpages = ceil($numrows/$rowsperpage); 

    if ($numrows>0) 
    { 
     $sn=0; 
     //--------for loop for Paging------------- 
     for ($x=0; $x < $totalpages; $x++) 
     { 
      echo "<br><table width='622' border='1' cellspacing='0' cellpadding='5' align='center'>"; 
      echo "<tr>"; 
      echo "<td width='30' align='center' class='toprow'>SNo.</td>"; 
      echo "<td width='86' align='center' class='toprow'>DC Date</td>"; 
      echo "<td width='94' align='center' class='toprow'>DC No.</td>"; 
      echo "<td width='160' align='center' class='toprow'>Product Description</td>"; 
      echo "<td width='89' align='center' class='toprow'>Cylinder No.</td>"; 
      echo "<td width='91' align='center' class='toprow'>ECR Date</td>"; 
      echo "<td width='72' align='center' class='toprow'>ECR No.</td>"; 
      echo "</tr>"; 

      //---while loop with select statement limit to 30 records--------------------------------- 
      $dis=mysql_query("SELECT * FROM CYLSTOCK_draft WHERE (DC_Date BETWEEN '$startdat' AND '$enddat' AND Ac_code='$custno' AND Product_Desc='$gas') OR (Ecr_dt BETWEEN '$startdat' AND '$enddat' AND Ac_code='$custno' AND Product_Desc='$gas') ORDER BY DC_Date LIMIT '$sn', 30"); 
      while ($di2=mysql_fetch_assoc($dis)) 
      { 
       $sn++; 
       $k3=$di2['DC_Date']; 
       $date_array = explode("-", $k3); 
       $kk3 = sprintf('%02d-%02d-%4d', $date_array[2], $date_array[1], $date_array[0]); 
       $kk3a=strtotime($kk3); 

       $k2=$di2['DC_No']; 
       $k6=$di2['Product_Desc']; 
       $k7=$di2['Cylinder_No']; 
       $k5=$di2['Ecr_dt']; 
       $kk5=strtotime($k5); 
       $k4=$di2['Ecr_No']; 

       echo "<tr>"; 
       echo "<td width='30' align='center' class='sty1'>$sn</td>"; 
       echo "<td width='86' align='center' class='sty1'>"; 
       if ($kk3a>0) 
       { 
        echo $kk3; 
       } 
       echo "</td>"; 

       echo "<td width='94' align='center' class='sty1'>$k2</td>"; 
       echo "<td width='160' align='center' class='sty1'>$k6</td>"; 
       echo "<td width='89' align='center' class='sty1'>$k7</td>"; 
       echo "<td width='91' align='center' class='sty1'>"; 
       if($kk5>0) 
       { 
        $date_array = explode("-", $k5); 
        $kk5 = sprintf('%02d-%02d-%4d', $date_array[2], $date_array[1], $date_array[0]); 
        echo $kk5; 
       } 
       echo "</td>"; 

       echo "<td width='72' align='center' class='sty1'>"; 
       if (substr($k4,5) > 0) 
       { 
        echo $k4; 
       } 
       echo "</td>"; 
       echo "</tr>"; 
      } 
     //---End of for loop 
     } 
    } 
    echo "</table><br>"; 
+1

你把'$ sn'變種成'LIMIT'在那一刻這個變種等於'0'。 – Serjio 2012-08-15 10:50:16

+0

您也可以在打開它後立即關閉您的PHP代碼。發現格式化您的代碼時。 – Fluffeh 2012-08-15 10:52:33

+0

它只是一段代碼因此關閉?>丟失了!謝謝 – user1114409 2012-08-15 11:12:00

回答

2
.. LIMIT '$sn', 30"); .. 

約$ SN沒有引號

+0

非常感謝,它工作正常 – user1114409 2012-08-15 11:07:45