目前,我有一个页面,列出了我的员工提交的费用。问题是它从已提交的费用表中拉出了每一行...因此,需要花费很多时间来加载页面(我们的数据可以追溯到2006年!!!)我会清除该表,但是由于我们的政策,我们需要保持10年的价值记录。无论如何,我都希望该页面列出25个条目,并希望选择具有>之类的页面,每个页面将显示25个条目。与讨论相关的PHP代码是:<?php$forms = array();if ($checkr[4]==2) $dtfq = mysql_query("SELECT `index`,`time`,`status`,`user` FROM `dtf` ORDER BY `index` desc;");else $dtfq = mysql_query("SELECT `index`,`time`,`status`,`user` FROM `dtf` WHERE `user`=".$checkr[0]." ORDER BY `index` desc;");while($dtf = mysql_fetch_row($dtfq)) { $newentry = array(1,$dtf[0],$dtf[1],$dtf[2],$dtf[3]); $forms[] = $newentry;}if ($checkr[4]==2) $crfq = mysql_query("SELECT `index`,`time`,`status`,`user` FROM `crf` ORDER BY `index` desc;");else $crfq = mysql_query("SELECT `index`,`time`,`status`,`user` FROM `crf` WHERE `user`=".$checkr[0]." ORDER BY `index` desc;");while ($crf = mysql_fetch_row($crfq)) { $newentry = array(2,$crf[0],$crf[1],$crf[2],$crf[3]); $forms[] = $newentry;}for ($i=0; $i<count($forms); $i++) { for ($j=0; $j<count($forms); $j++) { if ($forms[$j][2]<$forms[$i][2]) { $temp = $forms[$j]; $forms[$j] = $forms[$i]; $forms[$i] = $temp; } }}for ($i=0; $i<count($forms); $i++) {?>我发现此资源很有帮助,但不能满足我的需求:Need to pull last x number of rows based on date谢谢,传统知识 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 您在谈论分页。做这个 :<?phpinclude 'conn.php'; /* Assume that this has your connection to your DB and put your connection to $con variable */$dtfq = "SELECT `index`,`time`,`status`,`user` FROM `dtf` ORDER BY `index` DESC";$result = mysqli_query($con, $dtfq); /* Do the Query */$count=mysqli_num_rows($result); /* Count the number of rows */$r = mysqli_fetch_row($result);$numrows = $r[0];echo '<b>&nbsp;'.$count.' results found</b>';$rowsperpage = 25; /* Your request to have 25 entries per page */$totalpages = ceil($count / $rowsperpage);if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage'];} else { $currentpage = 1;}if ($currentpage > $totalpages) { $currentpage = $totalpages;}if ($currentpage < 1) { $currentpage = 1;}$offset = ($currentpage - 1) * $rowsperpage;$dtfq = "SELECT `index`,`time`,`status`,`user` FROM `dtf` ORDER BY `index` DESC LIMIT $offset, $rowsperpage";$result=mysqli_query($con, $dtfq);/*start of the table*/echo "<table border='1'><tr><th>Index</th><th>Time</th><th>Status</th><th>User</th></tr>";/*start of the record display*/while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['index'] . "</td>"; echo "<td>" . $row['time'] . "</td>"; echo "<td>" . $row['status'] . "</td>"; echo "<td>" . $row['user'] . "</td>"; echo "</tr>"; }echo "</table>";echo '<table border="0"><tr><td>';/****** build the pagination links ******/$range = 2;if ($currentpage > 1) { $prevpage = $currentpage - 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a> ";}for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { if (($x > 0) && ($x <= $totalpages)) { if ($x == $currentpage) { echo " <font color='#546f3e'><b>$x</b></font> "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } }}if ($currentpage != $totalpages) { $nextpage = $currentpage + 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> ";} // end if/****** end build pagination links ******/echo '</td></tr></table>'; ?>并且请使用mysqli而不是其前身mysql。您可能还需要考虑在系统中具有过滤器/搜索功能。或按年份分类。关于php - 限制从sql数据库提取并组织成页面的行数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22240904/ (adsbygoogle = window.adsbygoogle || []).push({});
10-10 03:27