页面加载缓慢
page is slow to load
我遇到了一个问题,页面加载速度有点慢,因为它必须使用此代码检索大量数据。我写了其他代码,速度更快,但比这个代码页数更多。有没有其他方法可以写这个以获得更快的速度。
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
<link href="../css/custom.css" rel="stylesheet">
</head>
<body>
<nav>
<table border='1'>
<?php
/* For the 2 different types of tables */
$dataArray = array("one"=>"status='Received'","two"=>"Department='Claims'","three"=>"Department='flat 1'","four"=>"Department='flat 2'","Five"=>"Department='Inbound'");
require_once("../db_connect.php");
foreach ($dataArray as $i=>$v)
{
?>
<tr><td>
<a href="#" data-id="<?php echo $i; ?>">
<?php
$stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE ".$v);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['rows_cnt'];
}
?>
</a>
</td>
<td>
<?php echo$v;?></td>
</tr>
<?php
}
?>
</table>
</nav>
<?php
foreach ($dataArray as $i=>$v)
{
?>
<div id="<?php echo $i; ?>" class="toggle_content">
<?php
//prepared statement with PDO to query the database
$stmt = $db->prepare("SELECT * FROM receivingrequests WHERE ".$v);
$stmt->execute();
?>
<?php //start of the while loop ?>
<?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
<table border="1" style="border: thin #000000; table-layout: fixed; width: 100%; background-color: #FFFFFF; display: table;" class="style1">
<tr>
<th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
<strong>Request#</strong></th>
<th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
<strong>Status</strong></th>
<th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Comments</strong></th>
<th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Date Requested</strong></th>
<th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Name</strong></th>
<th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Department</strong></th>
<th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>VasLblDate</strong></th>
</tr>
<tr>
<?php $id = $row['RequestNumber'];?>
<?php echo "<td> <a href='../update.php?id=$id'>$id</a></td>"?>
<td class="style2" style="width: 62px"><strong><?php echo $row['Status']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Comments']; ?></strong></td>
<td class="style2"><strong><?php echo $row['DATEREQUESTED']; ?></strong></td>
<td class="style2"><strong><?php echo $row['EmpName']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Department']; ?></strong></td>
<td class="style2"><strong><?php echo $row['VasLbDate']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>Work Requested</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Cases Missing</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Stray Cases Found/To Move</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Cases To Consume</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>PO #</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>IS #
</strong> </th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Vendor Name
</strong> </th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['Effortrequest']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Missing']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Located']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Dissassociated']; ?>
</strong></td>
<td class="style2"><strong><?php echo $row['PONumber']; ?></strong></td>
<td class="style2"><strong><?php echo $row['IBS']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Vendor']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>Case 1</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Case 2</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Case 3</strong></th>
<th style="background-color: #F4F4F4;"class="style2"></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Description</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Comments</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Carrier</strong></th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['CaseOne']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CaseTwo']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CaseThree']; ?></strong></td>
<td class="style2"><strong><?php echo $row['']; ?></strong></td>
<td class="style2"><strong><?php echo $row['ReqDescription']; ?></strong></td>
<td class="style2"><strong><?php echo $row['MoreComments']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Carrier']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>Trip</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>DC Remarks Update by</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>DC Remarks Updated</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Actual Cases Consumes</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>EE Performing Consume Cases</strong></th>
<th style="background-color: #F4F4F4;"class="style3" class="style2">
<strong>Store Number</strong></th>
<th style="background-color: #F4F4F4; width:60px; height: 25px;" class="style3">
<strong>Reason</strong></th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['Trip']; ?></strong></td>
<td class="style2"><strong><?php echo $row['RemarksBy']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CompDT']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CaseCount']; ?></strong></td>
<td class="style2"><strong><?php echo $row['WHODIS']; ?></strong></td>
<td class="style2"><strong><?php echo $row['StoreNumber']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Reason']; ?></strong></td>
</tr>
<br>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>New IS#</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>New Carrier Shipment#</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Deleted Case#</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Notes</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Inbound User ID</strong></th>
<th style="background-color: #F4F4F4;"class="style3" class="style2">
<strong>Was Shipment</strong></th>
<th style="background-color: #F4F4F4; width:60px; height: 25px;" class="style3">
<strong>Verified BY</strong></th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['NewIS']; ?></strong></td>
<td class="style2"><strong><?php echo $row['NewCSN']; ?></strong></td>
<td class="style2"><strong><?php echo $row['DCN']; ?></strong></td>
<td class="style2"><strong><?php echo $row['MoreComments']; ?></strong></td>
<td class="style2"><strong><?php echo $row['AreaID']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Verified']; ?></strong></td>
<td class="style2"><strong><?php echo $row['VerifiedID']; ?></strong></td>
</tr>
</table>
<?php } //end of the while loop?>
</div>
<?php
}
?>
<!-- Placed at the end of the document so the pages load faster -->
<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<script>
$( document ).ready(function() {
$('a').on('click', function() {
var div_id = $(this).data('id');
$('.toggle_content').hide();
$('#' + div_id).toggle();
});
});
</script>
</body>
</html>
首先:去掉内联 CSS。对于大型数据集,这将显着增加 HTML 的大小,这意味着传输时间也会增加。请改用样式表。
然后:您经常在某些单元格中使用 strong
元素。尝试使用 CSS 的 font-weight: bold;
来做一些聪明的事情。这也会减少您的 HTML 的大小。
最后:检查您的服务器是否已打开压缩,以减少数据流量。
然后是一些最佳实践:
它很小,但您没有充分利用 PDO 的准备好的语句。你在循环中做:
$stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE ".$v);
相反,准备循环外的语句,并在循环内执行它:
$stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE ?");
// start loop... {
$stmt->execute(array($v));
// } ... end loop
这样就不用每次都准备了
不过有了这个小阵子就不会出事了。
最后一句话:<?php echo $i; ?>
可以缩写为 <?=$i?>
(如果您的 PHP 安装启用了短开始标签选项)。
我遇到了一个问题,页面加载速度有点慢,因为它必须使用此代码检索大量数据。我写了其他代码,速度更快,但比这个代码页数更多。有没有其他方法可以写这个以获得更快的速度。
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
<link href="../css/custom.css" rel="stylesheet">
</head>
<body>
<nav>
<table border='1'>
<?php
/* For the 2 different types of tables */
$dataArray = array("one"=>"status='Received'","two"=>"Department='Claims'","three"=>"Department='flat 1'","four"=>"Department='flat 2'","Five"=>"Department='Inbound'");
require_once("../db_connect.php");
foreach ($dataArray as $i=>$v)
{
?>
<tr><td>
<a href="#" data-id="<?php echo $i; ?>">
<?php
$stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE ".$v);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['rows_cnt'];
}
?>
</a>
</td>
<td>
<?php echo$v;?></td>
</tr>
<?php
}
?>
</table>
</nav>
<?php
foreach ($dataArray as $i=>$v)
{
?>
<div id="<?php echo $i; ?>" class="toggle_content">
<?php
//prepared statement with PDO to query the database
$stmt = $db->prepare("SELECT * FROM receivingrequests WHERE ".$v);
$stmt->execute();
?>
<?php //start of the while loop ?>
<?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
<table border="1" style="border: thin #000000; table-layout: fixed; width: 100%; background-color: #FFFFFF; display: table;" class="style1">
<tr>
<th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
<strong>Request#</strong></th>
<th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
<strong>Status</strong></th>
<th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Comments</strong></th>
<th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Date Requested</strong></th>
<th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Name</strong></th>
<th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Department</strong></th>
<th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>VasLblDate</strong></th>
</tr>
<tr>
<?php $id = $row['RequestNumber'];?>
<?php echo "<td> <a href='../update.php?id=$id'>$id</a></td>"?>
<td class="style2" style="width: 62px"><strong><?php echo $row['Status']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Comments']; ?></strong></td>
<td class="style2"><strong><?php echo $row['DATEREQUESTED']; ?></strong></td>
<td class="style2"><strong><?php echo $row['EmpName']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Department']; ?></strong></td>
<td class="style2"><strong><?php echo $row['VasLbDate']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>Work Requested</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Cases Missing</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Stray Cases Found/To Move</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Cases To Consume</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>PO #</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>IS #
</strong> </th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Vendor Name
</strong> </th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['Effortrequest']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Missing']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Located']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Dissassociated']; ?>
</strong></td>
<td class="style2"><strong><?php echo $row['PONumber']; ?></strong></td>
<td class="style2"><strong><?php echo $row['IBS']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Vendor']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>Case 1</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Case 2</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Case 3</strong></th>
<th style="background-color: #F4F4F4;"class="style2"></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Description</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Comments</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Carrier</strong></th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['CaseOne']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CaseTwo']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CaseThree']; ?></strong></td>
<td class="style2"><strong><?php echo $row['']; ?></strong></td>
<td class="style2"><strong><?php echo $row['ReqDescription']; ?></strong></td>
<td class="style2"><strong><?php echo $row['MoreComments']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Carrier']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>Trip</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>DC Remarks Update by</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>DC Remarks Updated</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Actual Cases Consumes</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>EE Performing Consume Cases</strong></th>
<th style="background-color: #F4F4F4;"class="style3" class="style2">
<strong>Store Number</strong></th>
<th style="background-color: #F4F4F4; width:60px; height: 25px;" class="style3">
<strong>Reason</strong></th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['Trip']; ?></strong></td>
<td class="style2"><strong><?php echo $row['RemarksBy']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CompDT']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CaseCount']; ?></strong></td>
<td class="style2"><strong><?php echo $row['WHODIS']; ?></strong></td>
<td class="style2"><strong><?php echo $row['StoreNumber']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Reason']; ?></strong></td>
</tr>
<br>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>New IS#</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>New Carrier Shipment#</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Deleted Case#</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Notes</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Inbound User ID</strong></th>
<th style="background-color: #F4F4F4;"class="style3" class="style2">
<strong>Was Shipment</strong></th>
<th style="background-color: #F4F4F4; width:60px; height: 25px;" class="style3">
<strong>Verified BY</strong></th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['NewIS']; ?></strong></td>
<td class="style2"><strong><?php echo $row['NewCSN']; ?></strong></td>
<td class="style2"><strong><?php echo $row['DCN']; ?></strong></td>
<td class="style2"><strong><?php echo $row['MoreComments']; ?></strong></td>
<td class="style2"><strong><?php echo $row['AreaID']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Verified']; ?></strong></td>
<td class="style2"><strong><?php echo $row['VerifiedID']; ?></strong></td>
</tr>
</table>
<?php } //end of the while loop?>
</div>
<?php
}
?>
<!-- Placed at the end of the document so the pages load faster -->
<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<script>
$( document ).ready(function() {
$('a').on('click', function() {
var div_id = $(this).data('id');
$('.toggle_content').hide();
$('#' + div_id).toggle();
});
});
</script>
</body>
</html>
首先:去掉内联 CSS。对于大型数据集,这将显着增加 HTML 的大小,这意味着传输时间也会增加。请改用样式表。
然后:您经常在某些单元格中使用 strong
元素。尝试使用 CSS 的 font-weight: bold;
来做一些聪明的事情。这也会减少您的 HTML 的大小。
最后:检查您的服务器是否已打开压缩,以减少数据流量。
然后是一些最佳实践:
它很小,但您没有充分利用 PDO 的准备好的语句。你在循环中做:
$stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE ".$v);
相反,准备循环外的语句,并在循环内执行它:
$stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE ?");
// start loop... {
$stmt->execute(array($v));
// } ... end loop
这样就不用每次都准备了
不过有了这个小阵子就不会出事了。
最后一句话:<?php echo $i; ?>
可以缩写为 <?=$i?>
(如果您的 PHP 安装启用了短开始标签选项)。