Sorting an HTML table with PHP after entering the database

advertisements

I'm creating a HTML table with data-rows from a MySQL database and some calculated values, like this:

    <?php

$connection = mysql_connect('localhost','root','') or die('Connection failed!');
mysql_select_db('MyDB', $connection);
$result = mysql_query('SELECT * FROM DB_TABLE');

?>

<table>
  <tr>
    <th scope="col">Heading 1</th>
    <th scope="col">Tariefplan</th>
    <th scope="col">Abonnementskost</th>
    <th scope="col">Maandelijkse korting</th>
    <th scope="col">Contractduur</th>
    <th scope="col">Inbegrepen in bundel</th>
    <th scope="col">Tarieven buiten bundel</th>
    <th scope="col"></th>

    <th scope="col">Bereken minuten</th>
    <th scope="col">Bereken SMS'en</th>
    <th scope="col">Bereken MB's</th>
    <th scope="col"></th>

    <th scope="col">Totale prijs normaal</th>
    <th scope="col">Totale prijs promotie</th>
    <th scope="col">Totale prijs contract</th>
  </tr>

<?php
while ($data = mysql_fetch_assoc($result)) {
?>
    <tr>
        <th scope="row"><?php echo $data['provider']; ?></th>
        <td><?php echo $data['planname']; ?></td>
        <td>&euro; <?php echo $data['price_normal']; ?> per maand<br />
            <sub>gedurende <?php echo ($data['contract_duration']-$data['promo_duration']); ?> maanden</sub></td>
        <td>- &euro; <?php echo $data['promo_discount']; ?> per maand<br />
            <sub>gedurende <?php echo $data['promo_duration']; ?> maanden</sub><br />
            <sub>promotie geldig tot <?php echo $data['promo_valid']; ?></sub></td>
        <td><?php echo $data['contract_duration']; ?> maanden</td>
        <td>
            <ul>
                <li><?php echo $data['included_min']; ?> minuten</li>
                <li><?php echo $data['included_sms']; ?> SMS'en</li>
                <li><?php echo $data['included_mb']; ?> MB's</li>
                <li>&euro; <?php echo $data['included_value']; ?> belwaarde</li>
            </ul>
        </td>
        <td>
            <ul>
                <li>&euro; <?php echo $data['price_min']; ?> per minuut</li>
                <li>&euro; <?php echo $data['price_sms']; ?> per SMS</li>
                <li>&euro; <?php echo $data['price_mb']; ?> per MB</li>
            </ul>
        </td>
        <td></td>

        <td>
            <?php
                if ($_POST['used_min'] <= $data['included_min']) {
                    $calc_min = 0;
                }
                else {
                    $calc_min = ($_POST['used_min'] - $data['included_min']) * $data['price_min'];
                }
                echo '&euro; ' . $calc_min;
            ?>
        </td>
        <td>
            <?php
                if ($_POST['used_sms'] <= $data['included_sms']) {
                    $calc_sms = 0;
                }
                else {
                    $calc_sms = ($_POST['used_sms'] - $data['included_sms']) * $data['price_sms'];
                }
                echo '&euro; ' . $calc_sms;
            ?>
        </td>
        <td>
            <?php
                if ($_POST['used_mb'] <= $data['included_mb']) {
                    $calc_mb = 0;
                }
                else {
                    $calc_mb = ($_POST['used_mb'] - $data['included_mb']) * $data['price_mb'];
                }
                echo '&euro; ' . $calc_mb;
            ?>
        </td>
        <td></td>

        <td>
            <?php
                $used_total = ($calc_min + $calc_sms + $calc_mb);
                if ($data['included_value'] > $used_total) {
                    $total_price_normal = $data['price_normal'];
                }
                else {
                    $total_price_normal = ($data['price_normal'] + $used_total);
                }
                echo '&euro; ' . $total_price_normal . ' per maand';
            ?>
        </td>
        <td>
            <?php
                $total_price_discount = ($total_price_normal - $data['promo_discount']);
                echo '&euro; ' . $total_price_discount . ' per maand';
            ?>
        </td>
        <td>
            <?php
                $total_price_contract = ($total_price_normal * ($data['contract_duration']-$data['promo_duration'])) + ($total_price_discount * $data['promo_duration']);
                echo '&euro; ' . $total_price_contract . ' per maand ' . $data['contract_duration'] . ' na maanden';
            ?>
        </td>
    </tr>
<?php } ?>
</table>

The last column in the table is a variable value witch calculates a total amount. This is NOT listed in the database. I want to output this table to an browserpage and let is be sort on that last column.

With PHP sort() or asort() function that doesn't work fine.

Does someone have a solution?


Do the calculation in the SQL query, and let the database handle the sorting