我正在创建一个用于显示报告的网站。所有报告都是以表格形式呈现的,并使用PHP从SQL中提取。除了当行数超过几百行时需要较长时间来提取数据外,我的页面已经可以正常工作。这些表格通常有30多列。我想知道是否有办法使表格加载速度更快,或者更好的方法是将表格分成多个页面,并在底部添加导航按钮以向前或向后移动表格。
我尝试只使用PHP、HTML和CSS来完成这个任务。我没有直接访问代码所在服务器的权限,只能访问我的代码。因此更新除我的代码之外的任何内容都会非常困难。
以下是我目前用于提取和显示表格的代码:
我尝试只使用PHP、HTML和CSS来完成这个任务。我没有直接访问代码所在服务器的权限,只能访问我的代码。因此更新除我的代码之外的任何内容都会非常困难。
以下是我目前用于提取和显示表格的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>QDef</title>
<link rel="StyleSheet" href="StyleSheet.css" type="text/css">
</head>
<body>
<div class="menu-wrap">
<nav class="menu">
<ul class="clearfix">
<li><a href="default.aspx">Home</a></li>
<li><a href="#">Material Tracking</a>
<ul class="sub-menu">
<li><a href="SearchStateProject.php">Search by State or Contract Number</a></li>
<li><a href="MaterialTrackingAllStates.php">All Contracts</a></li>
</ul>
</li>
<li><a href="#">OPR Reports</a>
<ul class="sub-menu">
<li><a href="COEI_OPR_Filtered.php">COEI OPR Filtered</a></li>
<li><a href="OSP_OPR_Filtered.php">OSP OPR Filtered</a></li>
</ul>
</li>
<li><a href="#">Admin</a>
<ul class="sub-menu">
<li><a href="QDef.php">QDef</a></li>
<li><a href="CheckPHP.php">PHP Check</a></li>
<li><a href="EditQDefForm.php">Edit QDef form</a></li>
<li><a href="FormToEditMaterial.php">Form to Edit Material</a></li>
<li><a href="TableUpdates.php">Table Updates</a></li>
</ul>
</li>
</ul>
</nav>
</div>
<br>
<br>
<h1>QDef Table</h1>
<?php
$configs = include('DBConn.php');
$servername = $configs['ServerName'];
$username = $configs['UserName'];
$password = $configs['Password'];
$dbname = $configs['DBName'];
$limit = 15;
try
{
$conn = new PDO("sqlsrv:server=$servername;database=$dbname", $username,$password);
//set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::SQLSRV_ATTR_QUERY_TIMEOUT, 1);
//echo "Connected Successfully<br>" /*. $conn*/;
/*If conncected see if we can pull any data!*/
}
catch(Exception $e)
{
die( print_r( $e->getMessage()));
}
$TotalRows = $conn->query('select count(*) from pmdb.v_QDefs')->fetchColumn(); //How many rows in the table
$pages = ceil($TotalRows / $limit); //How many page will there be
$currentpage = 1;
$currentpage = min($pages, filter_input(input_get, 'page', filter_validate_int, array('options' => array('default' => 1,'min_range' => 1,),))); //What page are you currently on
var_dump($currentpage);
$offset = abs(($currentpage - 1) * $limit); //Calculate Offset
//Some info to display to the user?
$start = $offset + 1;
$end = min(($offset + $limit),$TotalRows);
$prevlink = ($currentpage > 1) ? '<a href="?page=1" title="First page">«</a> <a href="?page=' . ($currentpage - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>'; //the back link
$nextlink = ($currentpage < $pages) ? '<a href="?page' . ($currentpage + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>'; //the Forward link
echo '<div id="paging"><p>', $prevlink, ' Page ',$currentpage, ' of ',$pages,' pages, displaying ',$start,'-',$end, ' of ',$TotalRows,' results ',$nextlink,' </p></div>'; //display the paging information
$tsql = "select Id,QSrc,QName,isActive,RunReport,FilePath,QDef from pmdb.v_QDefs order by Id OFFSET $offset ROWS FETCH NEXT $limit ROWS";
//echo $tsql . "<br>";
$getqueries = $conn->query($tsql);
$queries = $getqueries->fetchALL(PDO::FETCH_ASSOC);
$countqueries = count($queries);
if(isset($countqueries))
{
if($countqueries > 0)
{
//echo "There are queries returned";
BeginQueriesTable($countqueries);
$CountValues = 1;
foreach($queries as $query)
{
PopulateQueryTable($query,$CountValues);
$CountValues = !$CountValues;
}
EndQueriesTable();
}
else
{
echo "<br>Values returned: $countqueries";
}
}
else
{
echo "No count";
}
function BeginQueriesTable($rowCount)
{
$headings = array("Edit","Id","QSrc","QName","isActive","RunReport","FilePath","QDef");
echo "<p class=" . chr(34) . "headings" . chr(34) . ">$rowCount Results</p>";
echo "<table class=" . chr(34) . "tab" . chr(34) . "id=" . chr(34) . "OuterTable" . chr(34) . ">";
echo "<tr>";
foreach($headings as $heading)
{
echo "<th class=" . chr(34) . "cell" . chr(34) . ">$heading</th>";
}
echo "</tr>";
}
function PopulateQueryTable($values,$Number)
{
$queryID = $values['Id'];
//var_dump($values);
//echo "<br/>";
//var_dump ($queryID);
echo "<tr class=" . chr(34) . "row" . ($Number) . chr(34) . "><td><a href=" . chr(34) . "EditQDefForm.php?id=" . $values['Id'] . chr(34) . ">Edit</a></td>";
foreach($values as $key=>$value)
{
if(!is_null($value))
{
echo "<td>$value</td>";
}
else
{
echo "<td></td>";
}
}
echo "</tr>";
}
function EndQueriesTable()
{
echo "</table><br/>";
}
?>
</body>
</html>
目前,这个表将会提取大约15000行数据。我知道这并不算太多,但是因为有61列,其中一些是非常长的注释,所以加载需要5-10分钟的时间。
如果您需要更多信息,请告诉我,任何帮助都将不胜感激。
include
函数。 - Bubble Hacker