使用javascript从html表中提取数据

时间:2019-05-22 01:06:27

标签: javascript php

我有一个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循环,以便输入字段中的数字乘以价格。我可以使用简单的代码部分来计算总价吗?

1 个答案:

答案 0 :(得分:3)

您无需使用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();

?>