php - 如果数据库中的表不断变化,我应该如何更新

标签 php jquery html mysql

我有一项任务,我将解释我的工作应该是什么样子。

首先,我使用$.get 方法从数据库中的表中获取总行数,并将该值显示到html 文件中。

1 小时2 小时2 天 或更长的时间,表中的总行数发生变化...

除了刷新页面或重新加载页面之外,我应该如何更新 html 文件中的总行数?

我的代码如下:

calculatetotalattack.php

<?php

   include 'config.php';
   $con = mysqli_connect ($dbhost, $dbusername, $dbpassword) or die ('Error in connecting: ' . mysqli_error($con));

   //Select the particular database and link to the connection
   $db_selected = mysqli_select_db($con, $dbname ) or die('Select dbase error '. mysqli_error());
   //Make A SQL Query and link to the connection
   $totalattackview = mysqli_query($con, 'SELECT * FROM attackview'); // get number of row from attackview

   //Calculate the total number of rows from the table
   $totalofrowsattack = mysqli_num_rows($totalattackview);

   echo $totalofrowsattack;

   mysqli_close($con);

?>

HTML

<html>
<head>
<title>Updating the Total Number of the total rows</title>
<script type="text/javascript" src="/Cesium-1.34/ThirdParty/jquery-1.11.3.min.js"></script> 
</head>
<body>
<div id="totalattack"></div>
<script type="text/javascript">
function totalAttacks(val) {
        $('#totalattack').html(val);
    }

    $.get({
        url: 'calculateTotalAttack.php',
        dataType: 'text',
        success: totalAttacks
    });
</script>
</body>
</html>

有办法吗?

最佳答案

您可以在 html 文件中向表格动态添加行或列,然后显示数据。

<!DOCTYPE html>
    <html>
    <head>
    <style>
    table, td {
        border: 1px solid black;
    }
    </style>
    </head>
    <body>

    <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

    <table id="myTable">
      <tr>
        <td>Row1 cell1</td>
        <td>Row1 cell2</td>
      </tr>
      <tr>
        <td>Row2 cell1</td>
        <td>Row2 cell2</td>
      </tr>
      <tr>
        <td>Row3 cell1</td>
        <td>Row3 cell2</td>
      </tr>
    </table>
    <br>

    <button onclick="myFunction()">Try it</button>

    <script>
    function myFunction() {
        var table = document.getElementById("myTable");
        var row = table.insertRow(0);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell1.innerHTML = "NEW CELL1";
        cell2.innerHTML = "NEW CELL2";
    }
    </script>

    </body>
    </html>

The insertRow() method creates an empty element and adds it to a table.

使用 setInterval()/setTimeout() 方法不断调用函数使其动态化。

此外,由于您不知道行/列何时会增加,因此您可以持续监控编号的任何变化。使用 setInterval() 计算表中的行数,如果有任何更改会使表增长。

关于php - 如果数据库中的表不断变化,我应该如何更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45934586/

相关文章:

php - 将 CSV 中的中文字符插入 MySQL 时遇到问题

php - 如何将 codeigniter 数据库的数据与 wordpress 数据库同步?有可用的 API 吗?

PHP 将多维数组中的值转换为数组

javascript - 是否可以在浮点图的顶部放置一些东西?

jquery - 有没有办法向 jqgrid Treeview 添加摘要行

php - 如何在不带括号的 Zend_Db_Select 语句中使用 "HAVING"子句?

javascript - 覆盖 CSS : Display none property with jquery

html - 重新启用字段后使用欧芹进行验证

html - spring如何用thymeleaf添加静态资源

javascript - 使用 Javascript 更新 CSS 中的文本颜色