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

时间:2016-06-17 14:08:09

标签: php html css sql-server

我正在创建一个用于显示报告的网站。所有报告都是表格格式,并使用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列,其中一些是非常长的评论。

如果您需要更多信息,请告诉我们,我们将非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

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

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

您可以将页面号码作为查询传递给NEXT和Previous链接:

$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以获取有关创建下一个和上一个链接等的更多详细信息。

答案 1 :(得分:0)

我已经在@markb在上面评论中发送的链接(sql-Simple PHP Pagination Script)的帮助下找到了答案。我最终使用了答案中的链接:

Basic pagination tutorial

我尝试了接受问题的答案,但无法让它发挥作用。以下是我为我工作的内容:

    <!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; //Create the max limit per page

    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()));
    }
    //How many rows in the table
    $TotalRows = $conn->query('select count(*) from pmdb.v_QDefs')->fetchColumn();
    //How many pages will there be
    $pages = ceil($TotalRows / $limit);
    //echo $pages;
    if(isset($_GET['currentpage']))
    {
        //var_dump($_GET);
        $currentpage = $_GET['currentpage'];
    }
    else
    {
        $currentpage = 1;
    }

    // if current page is greater than total pages...
    if ($currentpage > $pages) 
    {
       $currentpage = $pages;
    } 

    // if current page is less than first page...
    if ($currentpage < 1) 
    {
       $currentpage = 1;
    } 
    //Calculate Offset
    $offset = abs(($currentpage - 1) * $limit);

    $range = 3;
    echo "<p>";
    if($currentpage > 1)
    {
        echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=1'> << </a> ";
        $prevpage = $currentpage - 1;
        echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'> < </a> ";
    }
    //Setting the number of links for pages around the current page
    for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++)
    {
        if (($x > 0) && ($x <= $pages))
        {
            if ($x == $currentpage)
            {
                echo "[<b>$x</b>]";
            }
            else 
            {
                echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a>";
            }
        }
    }

    if ($currentpage != $pages)
    {
        $nextpage = $currentpage + 1;
        echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'> > </a>";
        echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$pages'> >> </a>";
    }
    echo "</p>";
    //Some info to display to the user?
    $start = $offset + 1;
    $end = min(($offset + $limit),$TotalRows);
    echo '<div id="paging"><p>',' 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 ONLY";

    $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 "<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'];
        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>

现在正在运行,目前每页显示15行。我使用这个数字只是为了快速。在我完成网站之前,我会提高行数。