我有这段代码,
$sqlstr = mysql_query(
"SELECT * FROM sales where passport = '{$therecord['passport']}'");
if (mysql_numrows($sqlstr) != 0) {
echo "<b>Sales for {$therecord['firstname']} {$therecord['lastname']}</b>";
while ($row = mysql_fetch_array($sqlstr)) {
echo "<table><tr>";
echo "<td>{$row['product']}</td>";
echo "<td>{$row['quantity']}</td>";
echo "<td>{$row['cost']}</td>";
echo "</tr>";
echo "</table>";
}
}
$sqltotal = mysql_query(
"SELECT SUM(cost) FROM sales where passport = '{$therecord['passport']}'");
echo "<b>Total Owing: {$sqltotal}</b>";
我想为每个表格列添加一个标题,也许它们之间可能有一些间距。
我该怎么做?
此外,我正在尝试使用SQL SUM函数并输出总数,但此时它会输出类似#id24的内容......
答案 0 :(得分:2)
在开始while
循环之前回复它:
echo '<tr><th>Name:</th><th>Quantity:</th><th>Cost:</th></tr>';
至于你的总和问题,这是因为你正在回显资源ID。您还需要在该资源上调用mysql_fetch_array()
(或类似函数),就像使用另一个资源一样。
答案 1 :(得分:2)
对于总计,而不是运行第二个查询,只需将成本转储到临时变量并在循环查询数组时递增...
$sqlstr = mysql_query(
"SELECT * FROM sales where passport = '{$therecord['passport']}'");
if (mysql_numrows($sqlstr) != 0) {
echo "<b>Sales for {$therecord['firstname']} {$therecord['lastname']}</b>";
while ($row = mysql_fetch_array($sqlstr)) {
echo "<table><tr>";
echo "<td>{$row['product']}</td>";
echo "<td>{$row['quantity']}</td>";
echo "<td>{$row['cost']}</td>";
echo "</tr>";
echo "</table>";
$_t += $row['cost'];
}
}
echo "<b>Total Owing: {$_t}</b>";
缺点: 你只得到循环结束后的总价值。
对于标题..只需在循环之前打印它们。
答案 2 :(得分:0)
您可以在while循环之前回显表格标题;
<thead>
<tr>
<th>Name:</th>
<th>Quantity:</th>
<th>Cost:</th>
</tr>
</thead>
<?php
$sqlstr = mysql_query("SELECT * FROM sales where passport = '{$therecord['passport']}'");
if (mysql_numrows($sqlstr) != 0) {
....
}
....
?>
这将对SUM问题进行排序(使用mysql_result)。目前您只输出资源。
$sqltotal = mysql_result(mysql_query("SELECT SUM(cost) FROM sales where passport = '{$therecord['passport']}'"),0);
echo "<b>Total Owing: {$sqltotal}</b>";
答案 3 :(得分:0)
sqlstr = mysql_query(
"SELECT * FROM sales where passport = '{$therecord['passport']}'");
if (mysql_numrows($sqlstr) != 0) {
echo "<b>Sales for {$therecord['firstname']} {$therecord['lastname']}</b>";
while ($row = mysql_fetch_array($sqlstr)) {
echo "<table><tr>";
echo "<td>{$row['product']}</td>";
echo "<td>{$row['quantity']}</td>";
echo "<td>{$row['cost']}</td>";
echo "</tr>";
echo "</table>";
}
}
echo "<b>Total Owing: ".mysql_num_rows($sqlstr)."</b>";
只需运行一个或多个行,因为查询与上述相同!
答案 4 :(得分:0)
总和: 运行查询后,您需要获得结果
$sqltotal = mysql_query("SELECT SUM(cost) as total FROM sales where passport = '{$therecord['passport']}'");
$row = mysql_fetch_array($sqltotal);
echo "<b>Total Owing: {$row['total']}</b>";
答案 5 :(得分:0)
$sqlstr = mysql_query(
"SELECT * FROM sales where passport = '{$therecord['passport']}'");
if (mysql_numrows($sqlstr) != 0) {
echo "<b>Sales for {$therecord['firstname']} {$therecord['lastname']}</b>";
echo "<table>";
echo "<tr>";
echo "<th>Product</th>";
echo "<th>Quantity</th>";
echo "<th>Cost</th>";
echo "</tr>";
while ($row = mysql_fetch_array($sqlstr)) {
echo "<tr>";
echo "<td>{$row['product']}</td>";
echo "<td>{$row['quantity']}</td>";
echo "<td>{$row['cost']}</td>";
echo "</tr>";
}
$sqltotal = mysql_query(
"SELECT SUM(cost) AS Total FROM sales where passport = '{$therecord['passport']}'");
$totalRow = mysql_fetch_array($sqltotal)) {
echo "<tr>";
echo "<td colspan=\"2\"></td>";
echo "<td><b>Total Owing: {$totalRow['Total']}</b></td>";
echo "</tr>";
echo "</table>";
}
答案 6 :(得分:-1)
尝试这个总和:
$sqltotal = mysql_query("SELECT SUM(cost) AS totalcost FROM sales WHERE passport = '" . mysql_real_esacpe_string($therecord['passport] . "';
$row = mysql_fetch_array($sqltotal);
echo "<b>Total Owing: {$row['totalcost']}</b>";