【问题标题】:How to Multiply two Column values and display its Result at the end of each Row?如何将两个列值相乘并在每行的末尾显示其结果?
【发布时间】:2012-06-27 11:49:15
【问题描述】:

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

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

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

例如

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

【问题讨论】:

    标签: php mysql html-table rows multiplication


    【解决方案1】:

    你试过了吗:

    ...
                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>';")
    

    【讨论】:

    • 您的答案生成了必要的结果,现在我想生成该结果的总数。那么,您能告诉我如何获得 Last Total Column 的总数吗?
    • 并告诉我如何获取每列的总计,即每列底部的工资和 ID 数。
    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 2021-10-23
    • 2021-01-10
    相关资源
    最近更新 更多