0

http://www.koolfree.com/ImageUpload/uploads/1340729929.jpg(テーブル画像)

こんにちは、私は画像にリンクしました(10未満のレピュテーションのためにstackoverflowがアップロードを許可しなかったため)、9列と3行のテーブルがあり、テーブルがデータベースとすべてに接続されていることがわかりますテーブルの値はデータベースに保存されます。

ご覧のとおり、合計の最後の列には、日数と給与の値の乗算結果を行ごとに個別に表示したいと思います。

例えば

user456は30日間働いており、給与は100なので、 30100を掛けて結果、つまり3000を生成し、結果を合計<>金額の最後の列に表示する必要があります。

同様に、

user123は30日間働いており、給与は250なので、 30250を掛けて結果、つまり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ごとに複数のジョブを追加する方法を教えてください。

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

1つの列にすべての行の合計を追加するには、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 に答える