如何将两个列值相乘并在每一行的末尾显示其结果?

时间:2012-06-26 07:30:56

标签: php mysql html-table rows multiplying

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

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

正如您所看到的,有一个Last Column of Total,我想在其中逐行显示Days和Salary值的乘法结果。

E.g。

user456 已经 30 天,他的薪水 100 ,所以它应该 30 乘以 100 并生成结果,即 3000 ,结果应显示在总计<>金额的最后一列中。

类似地,

user123 已经 30 天,他的工资 250 ,所以它应该 30 乘以 250 并生成结果,即 7500 ,结果应显示在总计<>金额的最后一列中。

我在表格中使用了以下代码并分享了您的帮助。

<?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添加多个作业?

1 个答案:

答案 0 :(得分: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>';")