gpt4 book ai didi

php - 如何使用 HTML 将表格加载到多个网页中?

转载 作者:搜寻专家 更新时间:2023-10-31 21:52:55 24 4
gpt4 key购买 nike

我正在创建一个用于显示报告的网站。所有报告均采用表格格式,并使用 PHP 从 SQL 中提取。我的页面可以正常工作,只是当有超过几百行时它们需要很长时间才能提取数据。这些表中通常有 30 多个列。我想知道是否有办法使表格加载速度更快,或者最好将表格拆分为多个页面,这些页面将在底部具有导航按钮以在表格中向前或向后移动。

我正在尝试仅使用 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">&laquo;</a> <a href="?page=' . ($currentpage - 1) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>'; //the back link
$nextlink = ($currentpage < $pages) ? '<a href="?page' . ($currentpage + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</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 行。我知道这不是很大,但加载需要 5 到 10 分钟,因为有 61 列,其中一些是非常长的评论。

如果您需要更多信息,请告诉我,我们将不胜感激。

最佳答案

主要思想是使用MySQL Limit:例如:

$sql = "SELECT * FROM mytable LIMIT 10 OFFSET 15";

您可以将页数作为查询传递给下一个和上一个链接:

$rec_limit=50; //Your favorite paging number
if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$sql = "SELECT * from mytable LIMIT $offset, $rec_limit";

检查 here有关创建下一个和上一个链接等的更多详细信息。

关于php - 如何使用 HTML 将表格加载到多个网页中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37883731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com