在检查页数后,我尝试在 SELECT 语句中使用 LIMIT 关键字对 30 条记录的结果进行分页,我使用了 2 个循环,用于标题的 FOR 循环和用于结果行的 WHILE 循环,并使用 LIMIT 进行限制,如果我需要知道可以在 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>";