title: How can I convert this untidy table header sorting code to well compact function?
body: <p>I have written a code snippet to sort my table headers in PHP/MySQL. The HTML of table headers along with PHP embedded code is given by:</p>
  
  <pre><code>&lt;tr&gt;
      &lt;th&gt;&lt;a href="?orderby=id&lt;?php if($orderby == '`post_id`' &amp;&amp; $order == 'asc') echo '&amp;order=desc'; else echo '&amp;order=asc'; ?&gt;&lt;?php echo $q; ?&gt;" title="&lt;?php echo $title_th1; ?&gt;"&gt;ID&lt;/a&gt;&amp;nbsp;&lt;?php echo $icon_th1; ?&gt;&lt;/th&gt;
      &lt;th&gt;&lt;a href="?orderby=name&lt;?php if($orderby == '`post_name`' &amp;&amp; $order == 'asc') echo '&amp;order=desc'; else echo '&amp;order=asc'; ?&gt;&lt;?php echo $q; ?&gt;" title="&lt;?php echo $title_th2; ?&gt;"&gt;Name&lt;/a&gt;&amp;nbsp;&lt;?php echo $icon_th2; ?&gt;&lt;/th&gt;
      &lt;th&gt;&lt;a href="?orderby=url&lt;?php if($orderby == '`post_url`' &amp;&amp; $order == 'asc') echo '&amp;order=desc'; else echo '&amp;order=asc'; ?&gt;&lt;?php echo $q; ?&gt;" title="&lt;?php echo $title_th3; ?&gt;"&gt;URL&lt;/a&gt;&amp;nbsp;&lt;?php echo $icon_th3; ?&gt;&lt;/th&gt;
      &lt;th&gt;&lt;a href="?orderby=title&lt;?php if($orderby == '`p`.`post_title`' &amp;&amp; $order == 'asc') echo '&amp;dir=desc'; else echo '&amp;dir=asc'; ?&gt;&lt;?php echo $q; ?&gt;" title="&lt;?php echo $title_th4; ?&gt;"&gt;Title&lt;/a&gt;&amp;nbsp;&lt;?php echo $icon_th4; ?&gt;&lt;/th&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>PHP code that handles the incoming GET request and decides what should be the correct table sorting order column and sorting direction (i.e. asc or desc) is given below:
      
  
  <pre><code>// Sorting
  if(isset($_GET['orderby']))
  {
      $orderby = $_GET['orderby'];
  
      switch($orderby)
      {
          case "id":
          $orderby = "post_id";
          break;
          case "name":
          $orderby = "post_name";
          break;
          case "url":
          $orderby = "post_url";
          break;
          case "title":
          $orderby = "post_title";
          break;
          default:
          $orderby = "post_name";
      }
  }
  
  // Sort direction
  if(isset($_GET['order']) &amp;&amp; in_array($_GET['order'], array('asc', 'desc')))
  {
      $order = $_GET['order'];
  }
  
  // Get dynamic sort direction icon and anchor title
  if($orderby == 'post_id')
  {
      if($order == 'asc')
      {
          $icon_th1 = '&lt;img src="images/arrow_up.png" class="arrow_dir"&gt;';
          $title_th1 = 'Click to sort results descending';
      }
      else
      {
          $icon_th1 = '&lt;img src="images/arrow_down.png" class="arrow_dir"&gt;';
          $title_th1 = 'Click to sort results ascending';
      }
      $icon_th2 = '';
      $title_th2 = 'Click to sort results ascending';
      $icon_th3 = '';
      $title_th3 = 'Click to sort results ascending';
      $icon_th4 = '';
      $title_th4 = 'Click to sort results ascending';
  }
  
  elseif($orderby == 'post_name')
  {
      if($order == 'asc')
      {
          $icon_th2 = '&lt;img src="images/arrow_up.png" class="arrow_dir"&gt;';
          $title_th2 = 'Click to sort results descending';
      }
      else
      {
          $icon_th2 = '&lt;img src="images/arrow_down.png" class="arrow_dir"&gt;';
          $title_th2 = 'Click to sort results ascending';
      }
      $icon_th1 = '';
      $title_th1 = 'Click to sort results ascending';
      $icon_th3 = '';
      $title_th3 = 'Click to sort results ascending';
      $icon_th4 = '';
      $title_th4 = 'Click to sort results ascending';
  }
  
  elseif($orderby == 'post_url')
  {
      if($order == 'asc')
      {
          $icon_th3 = '&lt;img src="images/arrow_up.png" class="arrow_dir"&gt;';
          $title_th3 = 'Click to sort results descending';
      }
      else
      {
          $icon_th3 = '&lt;img src="images/arrow_down.png" class="arrow_dir"&gt;';
          $title_th3 = 'Click to sort results ascending';
      }
      $icon_th1 = '';
      $title_th1 = 'Click to sort results ascending';
      $icon_th2 = '';
      $title_th2 = 'Click to sort results ascending';
      $icon_th4 = '';
      $title_th4 = 'Click to sort results ascending';
  }
  
  elseif($orderby == 'post_title')
  {
      if($order == 'asc')
      {
          $icon_th4 = '&lt;img src="images/arrow_up.png" class="arrow_dir"&gt;';
          $title_th4 = 'Click to sort results descending';
      }
      else
      {
          $icon_th4 = '&lt;img src="images/arrow_down.png" class="arrow_dir"&gt;';
          $title_th4 = 'Click to sort results ascending';
      }
      $icon_th1 = '';
      $title_th1 = 'Click to sort results ascending';
      $icon_th2 = '';
      $title_th2 = 'Click to sort results ascending';
      $icon_th3 = '';
      $title_th3 = 'Click to sort results ascending';
  }
  ?&gt;
  </code></pre>
  
  <p>This code is working without any issue. But what I am thinking is to convert it into something more tidy and compact may be into some function either. Because the code length increases as the number of table headers increase and number of columns we need to use in the sorting procedure. Currently we have only 4 <code>&lt;th&gt;</code> participating in the sorting. But what if we had more than it may be 7, 8 or so? Because there are almost same code repeating in all lines, so I am wondering can we convert it into some function which can be called anywhere where we want to use sorting.</p>
  
  <p>Secondly, I am also thinking to convert my <code>&lt;tr&gt;&lt;th&gt;...&lt;/th&gt;&lt;/tr&gt;</code> HTML code from static to dynamic using some array and loop. Is it possible? If not, then no problem, my main focus is to solve the first query I just discussed above.</p>