简体   繁体   English

使用javascript从html表中提取数据

[英]Pull data from html table using javascript

I have a php website that makes a table using data fetched from a database. 我有一个php网站,它使用从数据库中获取的数据制作表格。 In the table I have a input so that the user can select how many of each item they want to buy. 在表中,我有一个输入,以便用户可以选择要购买的每种商品有多少。 I have successfully made the ids of each input different by concatenating the ids. 通过串联ID,我已经成功地使每个输入的ID不同。

Here is the php that makes the table: 这是制作表格的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();

?>

I need to write the total amount at the end of my table, but i don't know how to make a for loop in javascript so that the number in the input field is multiplied by the price. 我需要在表末尾写总金额,但是我不知道如何在javascript中进行for循环,以便输入字段中的数字乘以价格。 Is there an easy part of code that i could use to calculate the grand total price? 我可以使用简单的代码部分来计算总价吗?

You don't need to do this in Javascript. 您无需使用Javascript执行此操作。 You could just do it in your PHP code: 您可以在您的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();

?>

Also, here's a cleaner way to output your HTML: 另外,这是一种输出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();

?>

If you want to use the quantity field in the table itself, you could do something like this. 如果要使用表本身中的数量字段,则可以执行以下操作。 It's a pretty quick solution. 这是一个非常快速的解决方案。 You'd probably want to refine it. 您可能想要对其进行优化。 But it's at least something to work with. 但这至少是可以使用的。

<?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();

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM