【问题标题】:Pull data from html table using javascript使用javascript从html表中提取数据
【发布时间】:2019-05-22 09:36:10
【问题描述】:

我有一个 php 网站,它使用从数据库中获取的数据制作表格。在表格中,我有一个输入,以便用户可以选择他们想要购买的每件商品的数量。我通过连接 id 成功地使每个输入的 id 不同。

这是制作表格的 php:

<?php

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
      echo "<tr>";
      echo "<td>" . $row['id'] . "</td>";
      echo "<td>" . $row['Article'] . "</td>";
      echo "<td>" . $row['Prix'] . "</td>";
      echo "<td>" . $row['PrixRetour'] . "</td>";
      echo "<td>" . $row['QuantiteeMaximale'] . "</td>";
      echo "<td>" . $row['Projet'] . "</td>";
      echo "<td id=\"quantity" . $row['id'] . "\"><input type=\"number\" name=\"quantity\" id=\"quantity\"></td>";
      echo "</tr>";
    }
} else {
    echo "0 results";
}
$conn->close();

?>

我需要在表格末尾写下总金额,但我不知道如何在 javascript 中创建一个 for 循环,以便输入字段中的数字乘以价格。有没有简单的代码可以用来计算总价?

【问题讨论】:

    标签: javascript php


    【解决方案1】:

    您不需要在 Javascript 中执行此操作。你可以在你的 PHP 代码中做到这一点:

    <?php
    
    if ($result->num_rows > 0) {
        $grandTotal = 0;
        // output data of each row
        while($row = $result->fetch_assoc()) {
          $grandTotal += $row['Prix'] * $row['QuantiteeMaximale'];
    
          echo "<tr>";
          echo "<td>" . $row['id'] . "</td>";
          echo "<td>" . $row['Article'] . "</td>";
          echo "<td>" . $row['Prix'] . "</td>";
          echo "<td>" . $row['PrixRetour'] . "</td>";
          echo "<td>" . $row['QuantiteeMaximale'] . "</td>";
          echo "<td>" . $row['Projet'] . "</td>";
          echo "<td id=\"quantity" . $row['id'] . "\"><input type=\"number\" name=\"quantity\" id=\"quantity\"></td>";
          echo "</tr>";
        }
    
        echo "Grand Total: {$grandTotal}"; // you might want to end your table before this. I'll leave formatting up to you.
    } else {
        echo "0 results";
    }
    $conn->close();
    
    ?>
    

    另外,这里有一种更简洁的方式来输出您的 HTML:

    <?php
    
    if ($result->num_rows > 0) {
        $grandTotal = 0;
        // output data of each row
        while($row = $result->fetch_assoc()) {
          $grandTotal += $row['Prix'] * $row['QuantiteeMaximale'];
    
          ?>
    
          <tr>
              <td><?= htmlentities($row['id']); ?></td>
              <td><?= htmlentities($row['Article']); ?></td>
              <td><?= htmlentities($row['Prix']); ?></td>
              <td><?= htmlentities($row['PrixRetour']); ?></td>
              <td><?= htmlentities($row['QuantiteeMaximale']); ?></td>
              <td><?= htmlentities($row['Projet']); ?></td>";
              <td id="quantity<?= $row['id']; ?>"><input type="number" name="quantity"></td>
          </tr>
    
          <?php
        }
    
        echo "Grand Total: {$grandTotal}"; // you might want to end your table before this. I'll leave formatting up to you.
    } else {
        echo "0 results";
    }
    $conn->close();
    
    ?>
    

    如果您想使用表格本身的数量字段,您可以这样做。这是一个非常快速的解决方案。你可能想改进它。但至少可以使用它。

    <?php
    
    if ($result->num_rows > 0) {
    
        echo "<table id='items'>";
        // output data of each row
        while($row = $result->fetch_assoc()) {
          $grandTotal += $row['Prix'] * $row['QuantiteeMaximale'];
    
          ?>
    
          <tr>
              <td><?= htmlentities($row['id']); ?></td>
              <td><?= htmlentities($row['Article']); ?></td>
              <td><?= htmlentities($row['Prix']); ?></td>
              <td><?= htmlentities($row['PrixRetour']); ?></td>
              <td><?= htmlentities($row['QuantiteeMaximale']); ?></td>
              <td><?= htmlentities($row['Projet']); ?></td>";
              <td><input data-price='<?= floatval($row['Prix']); ?>' data-max-quantity='<?= intval($row['QuantiteeMaximale']); ?>' type="number" name="quantity"></td>
          </tr>
    
          <?php
        }
    
        ?>
    
        </table>
    
        <p>Grand Total: $<span id='grandTotal'></span></p>
    
        <script>
            (() => {
                const updateGrandTotal = (grandTotalEl, inputEls) => {
                    grandTotalEl.innerText = inputEls.reduce((total, inputEl) => {
                        const maxQuantity = parseInt(inputEl.dataset.maxQuantity)
    
                        if(parseInt(inputEl.value) > maxQuantity) inputEl.value = maxQuantity
                        if(parseInt(inputEl.value) < 0) inputEl.value = 0
    
                        const price = parseFloat(inputEl.dataset.price)
                        const quantity = parseInt(inputEl.value)
    
                        if(isNaN(quantity)) return total
    
                        return total + (price * quantity)
                    }, 0)
                }
    
                const tableEl = document.getElementById('items')
                const grandTotalEl = document.getElementById('grandTotal')
                const quantityInputEls = tableEl.querySelectorAll('input[name=quantity]')
    
                quantityInputEls.forEach(el => el.addEventListener('keyup', () => updateGrandTotal(grandTotalEl, inputEls)))
            })()
        </script>
    
        <?php
    } else {
        echo "0 results";
    }
    $conn->close();
    
    ?>
    

    【讨论】:

    • 好吧,我认为这会很完美,但实际上我必须将价格乘以表格末尾输入字段中给出的数字。我需要 javascript,这样我就不必重新加载页面了。
    • @Louis 啊,我明白了。您应该编辑您的问题并说明您希望将价格乘以数量输入字段中的值。
    • @Louis 我更新了答案以使用数量字段计算总计。
    • 现在我已经尝试了它不起作用的代码。我在控制台中没有任何错误,但价格没有显示在总计的范围内。你有什么想法吗?
    • @Louis 我只是写了它作为一个例子。您可能必须调试和改进它。尝试使用 Chrome 开发工具,或添加一些 console.log 语句以查看发生了什么。
    猜你喜欢
    • 1970-01-01
    • 2011-10-16
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 2012-11-05
    • 2012-08-01
    相关资源
    最近更新 更多