0

我正在为我的妻子和我使用 PHP/MySQL 构建费用跟踪器。我已经成功构建了数据库、表格、适当的 PHP 文件等。我现在希望做的是将费用列中输入的所有数值加在一起,并将存款列中输入的所有数值也加起来并让它们显示在我构建的 PHP“echo”文件中相应列下的表格底部。

这是我建立的:

<html>
<head>  
   <title>Budget</title>  
   <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
        <style type="text/css">
             INPUT[type="text"] { font-size: 14pt }
             INPUT[type="password"] { font-size: 14pt }
        </style>  
        <link rel="apple-touch-icon" href="images/template/engage.png"/>
   </head>  
   <body BGCOLOR=#FFFFFF onorientationchange="updateOrientation();" >
<div id="page_wrapper">
    <h1><img src="Budjet1.gif"></h1>
    <div id="content_left">

<hr>
<?php
$username="username";
$password="password";
$database="database_name";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM table";
$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<td><font face="Arial, Helvetica, sans-serif"><b>Day</b></font></td>
<td><font face="Arial, Helvetica, sans-serif"><b>Month</b></font></td>
<td><font face="Arial, Helvetica, sans-serif"><b>Year</b></font></td>
<td><font face="Arial, Helvetica, sans-serif"><b>Transaction</b></font></td>
<td><font face="Arial, Helvetica, sans-serif"><b>Category</b></font></td>
<td><font face="Arial, Helvetica, sans-serif"><b>Deposit</b></font></td>
<td><font face="Arial, Helvetica, sans-serif"><b>Expense</b></font></td>
<td><font face="Arial, Helvetica, sans-serif"><b>Notes</b></font></td>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"Day");
$f2=mysql_result($result,$i,"Month");
$f3=mysql_result($result,$i,"Year");
$f4=mysql_result($result,$i,"Transaction");
$f5=mysql_result($result,$i,"Category");
$f6=mysql_result($result,$i,"Deposit");
$f7=mysql_result($result,$i,"Expense");
$f8=mysql_result($result,$i,"Notes");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f7; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f8; ?></font></td>
</tr>
<?php
$i++;
}
?>
</table>
                 <p><font face="arial" color="#000000" size="4"><a href="enter.php">Make another entry!</a></font></p>
                 <p><font face="arial" color="#000000" size="4"><a href="logout.php">Logout</a></font></p>
</div>
</div>
</body>
</html>

所以我想要做的是让脚本将“费用”列中的所有值加在一起,并在费用列下方显示该结果。与“存款”列相同。

我对这些东西有点陌生,所以您可以提供的任何帮助将不胜感激。我使用以下方法进行了测试:

$sql = "select sum(column) from table";
$q = mysql_query($sql);
$row = mysql_fetch_array($q); 

接着...

echo 'Sum: ' . $row[0];

但我无法让它显示在表格的底部。相反,它显示在每隔一行之间。另外,我不确定如何获取两个单独行的结果。

在此先感谢您的帮助!

4

1 回答 1

0

由于无论如何您都在查询所有数据,因此您可以只计算 PHP 代码中的总数。像这样的东西:

$expenseTotal = 0;

while ($row = mysql_fetch_assoc($result)) 
{
    $expense = $row['Expense'];
    $expenseTotal += $expense;

    echo "<tr><td>$expense</td></tr>"; //TODO: add other columns
}

echo "<tr><td>$expenseTotal</td></tr>";
于 2013-02-16T19:30:50.023 回答