如何将两个列值相乘并在每一行的末尾显示其结果? [英] How to Multiply two Column values and display its Result at the end of each Row?

查看:138
本文介绍了如何将两个列值相乘并在每一行的末尾显示其结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Hello, I have linked to an Image (because stackoverflow was not allowing me to upload due to less than 10 reputation) in which you can see there is a Table with 9 Columns and 3 Rows and the Table is connected to the Database and all the values in the table are stored in the Database.

尽可能看到有一个Last Column of Total,我想在其中逐行显示天数和工资值的乘法结果。

As you can see there is a Last Column of Total in which I want to display the Multiplication Result of Days and Salary values individually Row-wise.

例如

user456 已经 30 天,他的工资 100 ,所以应该将 30 乘以 100 并生成结果,即 3000 ,结果应显示在总计的最后一列<> amount .

user456 has worked 30 Days and his Salary is 100, so it should multiply 30 by 100 and generate the result i.e. 3000 and the result should be displayed in Last Column of Total <>amount.

同样,

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

user123 has worked 30 Days and his Salary is 250, so it should multiply 30 by 250 and generate the result i.e. 7500 and the result should be displayed in Last Column of Total <>amount.

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

I have used the following code in the Table and sharing for your assistance.

<?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>";


?>

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

Kindly tell me what additions or changes should I made to the codes to generate the requisite result?

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

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

I have attached a link of Screenshot. Kindly see the Screenshot and tell me how can I add multiple jobs per user ID?

推荐答案

你试过这个:

...
            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循环经过时变量递增的变量:

To add the total of all rows in one column you need to use a variable which gets incremented each time the while loop goes through:

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

这篇关于如何将两个列值相乘并在每一行的末尾显示其结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆