0

http://www.koolfree.com/ImageUpload/uploads/1340729929.jpg(表格图片)

您好,我已经链接到一张图片(因为 stackoverflow 不允许我上传,因为声誉低于 10),您可以在其中看到一个包含 9 列和 3 行的表格,并且该表格已连接到数据库和所有表中的值存储在数据库中。

如您所见,有一个总计的最后一列,我想在其中逐行显示天数和工资值的乘法结果。

例如

user456工作了30天,他的薪水是100,所以它应该将30乘以100并生成结果,即3000,结果应该显示在 Total <>amount的最后一列中。

相似地,

user123工作了30天,他的薪水是250,所以它应该将30乘以250并生成结果,即7500,结果应该显示在 Total <>amount的最后一列中。

我在表中使用了以下代码,并为您提供帮助。

<?php
/* 
        VIEW.PHP
        Displays all data from 'accounts' table
*/

        // connect to the database
        include('connect-db.php');

        // get results from database
        $result = mysql_query("SELECT * FROM accounts") 
                or die(mysql_error());  

        // display data in table
        echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";

        echo "<table border='1' cellpadding='10'>";
        echo "<tr> <th>No</th> <th>Name & ID</th> <th>Days</th> <th>OverTime</th> <th>Date</th> <th>Salary</th> <th>Edit</th><th>Delete</th><th>Total</th></tr>";

        // loop through results of database query, displaying them in the table
        while($row = mysql_fetch_array( $result )) {

                // echo out the contents of each row into a table
                echo "<tr>";
                echo '<td>' . $row['id'] . '</td>';
                echo '<td>' . $row['keywords'] . '</td>';
                echo '<td>' . $row['title'] . '</td>';
                echo '<td>' . $row['description'] . '</td>';
                echo '<td>' . $row['date'] . '</td>';
                echo '<td>' . $row['salary'] . '</td>';
                echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
                echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
                echo '<td><>amount</a></td>';
                echo "</tr>"; 


        } 



        // close table>
        echo "</table>";


?>

请告诉我应该对代码进行哪些添加或更改以生成必要的结果?


http://www.koolfree.com/ImageUpload/uploads/1341093148.jpg

我附上了截图的链接。请查看屏幕截图并告诉我如何为每个用户 ID 添加多个作业?

4

1 回答 1

0

你有没有试过这个:

...
            echo '<td>' . $row['salary'] . '</td>';
            echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
            echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
            echo '<td>' . $row['title']*$row['salary'] . '</td>';
            echo "</tr>"; 
... 

要在一列中添加所有行的总数,您需要使用一个变量,该变量在每次 while 循环通过时递增:

    // Declare variable for the place where the value
    // of all the elements of the column are stored
    $Total_total=0;
    // loop through results of database query, displaying them in the table
    while($row = mysql_fetch_array( $result )) {
    ...
            echo '<td>' . $row['title']*$row['salary'] . '</td>';
            echo "</tr>"; 
            //Increment the value of the Total_total variable
            //by the salary value of one row till the while loop finishes
            $Total_total=$Total_total+$row['title']*$row['salary'];
    }

    // After the while loop add one more row where the "total's" will be shown

    echo "<tr>";
    echo '<td>' . Id column totals . '</td>';
    echo '<td>' . Totals . '</td>';
    echo '<td>' . Totals . '</td>';
    echo '<td>' . Totals . '</td>';
    echo '<td>' . Totals . '</td>';
    echo '<td>' . Totals . '</td>';
    echo '<td>' . Totals . '</td>';
    echo '<td>' . Totals . '</td>';
    echo '<td>' . $Total_total . '</td>';
    echo "</tr>"; 

// Do the same for all the other columns where a total count is needed.
// 1) Declare variable ("$Total_total=0;")
// 2) Increment it each time with itself and something you
// need the totals for when while loop goes through one time 
//  ("$Total_total=$Total_total+$row['salary'];")
// 3) Print out the results after the while loop
//  ("echo '<td>' . $Total_total . '</td>';")
于 2012-06-26T08:05:57.660 回答