I want to display table by default in ascendind order with dynamically changing header.

Here is my code. It is displaying the rows unordered. It should be in ascending at first look and then can change by column.

    $order = (isset($_GET['sortCostCode']) && strcasecmp($_GET['sortCostCode'], 'desc') == 0) ? 'DESC' : 'ASC'; 
    $column = isset($_GET['sortcolumn']) ? $_GET['sortcolumn'] : 'logid';
    $column = isset($_GET['sortcolumn']) ? $_GET['sortcolumn'] : 'prenom';

    $query = "SELECT * FROM `Humeur_log` ORDER BY `$column` $order LIMIT 0,30";
    $result = mysql_query($query) or die(mysql_error());;

    ?>
    <table>
          <tr>
          <th><a href='?sortcolumn=logid&sortCostCode=<?php echo $order == 'DESC' ? 'ASC' : 'DESC'; ?>'>Logid</a></th>
          <th><a href='?sortcolumn=prenom&sortCostCode=<?php echo $order == 'DESC' ? 'ASC' : 'DESC'; ?>'>Prenom</a></th>
          </tr>
    <?php
      while($rows=mysql_fetch_assoc($result))
      {
          echo "<tr>";
          echo "<td>" . $rows['logid'] . "</td>";
          echo "<td>" . $rows['prenom'] . "</td>";
          echo "</tr>";
      }
    ?>
       </table>

It should be in ascending

Ascending by what column? Remove line 3, and adjust logid to the column you want to use for the initial sort.

I want to display table by default in ascendind order with " logid "

Its working but it should be in ascending order. For the moment, it is displaying in descending order.

It should be able to change afterwords.

The code is fine and the output should be ascending (assuming you removed line 3), unless desc is specified in the url.

You are absolutely right. Now, It is working perfect..!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.