Mysql
 sql >> Base de données >  >> RDS >> Mysql

Comment multiplier deux valeurs de colonne et afficher son résultat à la fin de chaque ligne ?

Avez-vous essayé ceci :

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

Pour ajouter le total de toutes les lignes d'une colonne, vous devez utiliser une variable qui est incrémentée à chaque passage de la boucle 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>';")