【问题标题】:What I should do to update if the table from the database keeps on changing如果数据库中的表不断变化,我应该做些什么来更新
【发布时间】:2017-08-29 08:48:39
【问题描述】:

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

首先,我使用$.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>

有办法吗?

【问题讨论】:

  • $.get 获取行数或 setInterval/timeout 获取行数。
  • 在这种情况下,您需要向表中动态添加行。
  • @Script47,除了 $.get 部分,我不明白你的句子..

标签: php jquery html mysql


【解决方案1】:

您可以在 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>

insertRow() 方法创建一个空元素并将其添加到 表。

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

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

【讨论】:

    【解决方案2】:

    你可以使用一个时间间隔,每X次调用totalAttacks()

    这里是文档:W3C

    【讨论】:

      【解决方案3】:

      您可以通过计时器运行 $.get 函数,如下所示:

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <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);
          }
      
          $(document).ready(function() {
            var timer = setInterval(function() {
              $.get({
                url: 'calculateTotalAttack.php',
                dataType: 'text',
                success: totalAttacks
              });
          console.log('requesting  changes');
            }, 5000);       
          });
        </script>
      </body>
      
      </html>

      【讨论】:

        【解决方案4】:

        你可以使用 ajax 和 jquery 来动态上传你的数据

        $document.ready(){
        
        $.ajax({
                type: "POST",
                url: "",
                data: {
                    first_name: $("#namec").val(),
                    last_name: $("#surnamec").val(),
                    email: $("#emailc").val(),
                    mobile: $("#numberc").val(),
                    password: $("#passwordc").val()
                },
                success: function(response) {
                    //dynamically add your data from here like make the new data equal to old one
        
                },
                error: function(response) {
                    console.log(response);
                }
            });
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-06
          • 1970-01-01
          • 2016-03-24
          • 1970-01-01
          • 2021-03-21
          • 2011-05-21
          • 2012-05-12
          • 1970-01-01
          相关资源
          最近更新 更多