我想使用SQL中的数据为PHP中的成本创建一个运行小计
我在这里找不到答案。希望有人能帮忙 我希望该表显示Xproducts到Xcost的运行总数。 我从一个SQL数据库中获取数据,已经在这个数据库上呆了几个月了我想使用SQL中的数据为PHP中的成本创建一个运行小计,php,sql,multiple-columns,Php,Sql,Multiple Columns,我在这里找不到答案。希望有人能帮忙 我希望该表显示Xproducts到Xcost的运行总数。 我从一个SQL数据库中获取数据,已经在这个数据库上呆了几个月了 <!-- begin snippet: js hide: false console: true babel: false language: lang-css --> <?php //Create a table to fill in echo("<table border=1>"); echo("<
<!-- begin snippet: js hide: false console: true babel: false
language: lang-css -->
<?php
//Create a table to fill in
echo("<table border=1>");
echo("<tr><th>Image</th><th>Name</th><th>Product Description</th><th>Inventory</th><th>Cost</th><th>Order</th><th>Total</th></tr>");
require_once("serverCode/connect.php");
$sql = "Select * FROM products;";
$result = $mysqli->query($sql);
while($row = $result->fetch_assoc()) {
// Jquery associate number with the textbox for each product
// dynamic on the fly (after is simpler )
$id = $row['productsID'];
/*Start columns*/ echo("<tr>");
/*Image column*/ echo("<td><img src='images/" . $id . ".jpg'" . "</td>");
/*Name column*/ echo("<td>" . $row['productsName'] . "</td>");
/*Image column*/ echo("<td>" . $row['productsDesc'] . "</td>");
/*Inv column*/ echo("<td>" . $row['productsInv'] . "</td>");
/*Cost column*/ echo("<td>" . $row['productsCost'] . "</td>");
/*orderAmt column*/ echo("<td><input type=text name = 'product_$id'></td>");
/*Subtotals column*/ echo("<td>" "</td>");
/*end columns*/ echo("</tr>");
}
echo("<tr><th></th><th></th><th></th><th></th><th></th><th></th><th>FINALTotal</th></tr>");
/*end table*/ echo("</table>")
?>
<!-- end snippet -->
你好,莉莉,欢迎来到stackoverflow
你的代码乱七八糟,我试着修复一下,但我没有检查所有东西。请试试这个:
<?php
require_once("serverCode/connect.php");
//Create a table to fill in
echo "<table border=1>";
echo "<tr><th>Image</th><th>Name</th><th>Product Description</th><th>Inventory</th><th>Cost</th><th>Order</th><th>Total</th></tr>";
$sql = "Select * FROM products;";
$result = $mysqli->query($sql);
// add a variable to store the running subtotal in:
$subtotal = 0;
while($row = $result->fetch_assoc()) {
// add the cost of the current product to $subtotal
// and make sure it is a float value:
$subtotal += floatval($row['productsCost']);
$id = $row['productsID'];
echo "<tr>"; // Start columns*/
echo "<td><img src=\"images/$id.jpg\"/></td>"; // Image column
echo "<td>" . $row['productsName'] . "</td>"; // Name column
echo "<td>" . $row['productsDesc'] . "</td>"; // Image column
echo "<td>" . $row['productsInv'] . "</td>"; // Inv column
echo "<td>" . $row['productsCost'] . "</td>"; // Cost
echo "<td><input type=\"text\" name=\"$id\"></td>"; // orderAmt column
// output the subtotal:
echo "<td>$subtotal</td>"; // Subtotals column
echo "</tr>"; // end columns
}
echo "<tr><th></th><th></th><th></th><th></th><th></th><th></th><th>FINALTotal</th></tr>";
echo "</table>"; // end table
?>
在while循环之外,声明一个变量$subtotal。在循环中,您将当前产品成本添加到它(+=运算符),并将其打印到表中。我在发生这种情况的地方添加了评论
此外,我会建议你在开始更大的项目之前,先学习英语。玩得开心 在while之外添加一个变量,以保存总数。在循环中,将当前项目价格添加到变量中。将变量输出到页面我不确定我是否理解你的意思。我对这些东西反应很慢。对不起,这帮了大忙!