问题
http://www.koolfree.com/ImageUpload/uploads/1340729929.jpg (Table Image)
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.
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.
E.g.
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.
Similarly,
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
I have attached a link of Screenshot. Kindly see the Screenshot and tell me how can I add multiple jobs per user ID?
回答1:
Have you tried this:
...
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>";
...
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>';")
来源:https://stackoverflow.com/questions/11202617/how-to-multiply-two-column-values-and-display-its-result-at-the-end-of-each-row