javascript - 如何获取表格中的总行数?

标签 javascript php mysql

如何获取购物车的总金额并放在表格下方的总金额上?我应该使用 JavaScript 还是只使用 PHP?请给我一些建议。谢谢。

      <thead>
        <tr>
          <th class="text-center">Product ID</th>
          <th class="text-center">Product Name</th>
          <th class="text-center">Description</th>
          <th class="text-center">Quantity</th>
          <th class="text-center">Price per Unit</th>
          <th class="text-center">Total Amount</th>
        </tr>
      </thead>
      <tbody>


      <?php 

        $selectCart = "SELECT * FROM cart INNER JOIN products ON products.product_id = cart.product_id";
        $execSelectCart = mysqli_query($connection, $selectCart);

        while ($row = mysqli_fetch_array($execSelectCart)) {

          $cartProId = $row['product_id'];
          $cartProName = $row['product_name'];
          $cartProDesc = $row['description'];
          $cartSellPrice = $row['sell_price'];
          $cartQty = $row['quantityCart'];

          $compute = $cartSellPrice * $cartQty;
          $totalAmount = number_format((float)$compute, 2, '.', '');
       ?>

        <tr>
          <td class="text-center"><?php echo $cartProId; ?></td>
          <td class="text-center"><?php echo $cartProName; ?></td>
          <td class="text-center"><?php echo $cartProDesc; ?></td>
          <td class="text-center"><?php echo $cartQty; ?></td>
          <td class="text-center"><?php echo $cartSellPrice; ?></td>
          <td class="text-center"><?php echo $totalAmount ?></td>
          </td>
        </tr>

      <?php } ?>
      </tbody>
    </table>
    <hr>
    <div class="row text-right">
      <div class="col-xs-2 col-xs-offset-8">
        <p>
          <strong>
            Sub Total : <br>
            VAT 12% : <br>
            Total : <br>
          </strong>
        </p>
      </div>
      <div class="col-xs-2">
        <strong>
          $36.00 <br>
          N/A <br>
          <?php echo $totalAmount; ?> <br>
        </strong>
      </div>
    </div>
   </div>

这是我的 table 。你可以看到,当我在我的 while 循环外回显 $totalamount 时,它只得到最后一行。

最佳答案

这将适用于您的情况。

<?php 

        $selectCart = "SELECT * FROM cart INNER JOIN products ON products.product_id = cart.product_id";
        $execSelectCart = mysqli_query($connection, $selectCart);

        $totalAmount = 0;
        while ($row = mysqli_fetch_array($execSelectCart)) {

          $cartProId = $row['product_id'];
          $cartProName = $row['product_name'];
          $cartProDesc = $row['description'];
          $cartSellPrice = $row['sell_price'];
          $cartQty = $row['quantityCart'];

          $compute = $cartSellPrice * $cartQty;
          $totalAmount += number_format((float)$compute, 2, '.', '');
       ?>

关于javascript - 如何获取表格中的总行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39188143/

相关文章:

mysql - sql如何使用这种语法更轻量?

mysql - 表创建逻辑

mysql - 在mysql中使用group_concat显示按类别分隔的调查结果

javascript - Google Analytics Embed API - 未找到 OAuth 客户端

javascript - 从 ui-grid 中的可扩展行中删除列标题

javascript - 单页应用程序发布/更新后如何回发到服务器

php - 使用 PHP 交叉编译的 THTTPD 服务器运行良好,但没有 PHP

php - 从字符串中删除特殊字符

URL 的 Javascript 正则表达式

php - Laravel Eloquent 区分大小写的关系