Mysql
 sql >> Database >  >> RDS >> Mysql

Come moltiplicare due valori di colonna e visualizzarne il risultato alla fine di ogni riga?

Hai provato questo:

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

Per sommare il totale di tutte le righe in una colonna è necessario utilizzare una variabile che viene incrementata ogni volta che il ciclo while viene eseguito:

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