How do I sort this multiple queried result table in PHP using a drop down? <p>I got a code which generate a table like this</p> <p><img src="https://i.stack.imgur.com/VQYa0.jpg" alt="table"></p> <p>and the code is here</p> <pre><code>$num1 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' &amp;&amp; Pathogen='$pathogen' &amp;&amp; Topic='$topic1' &amp;&amp; Indicator='$ind1' &amp;&amp; IndicatorSubGroup='$subindg1' &amp;&amp; (Country IN ('$sql_cntys') OR WHORegionAC IN ('$sql_cntys')) "); $num2 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' &amp;&amp; Pathogen='$pathogen' &amp;&amp; Topic='$topic2' &amp;&amp; Indicator='$ind2' &amp;&amp; IndicatorSubGroup='$subindg2' &amp;&amp; (Country IN ('$sql_cntys') OR WHORegionAC IN ('$sql_cntys')) "); $num3 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' &amp;&amp; Pathogen='$pathogen' &amp;&amp; Topic='$topic3' &amp;&amp; Indicator='$ind3' &amp;&amp; IndicatorSubGroup='$subindg3' &amp;&amp; (Country IN ('$sql_cntys') OR WHORegionAC IN ('$sql_cntys')) "); $num4 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' &amp;&amp; Pathogen='$pathogen' &amp;&amp; Topic='$topic4' &amp;&amp; Indicator='$ind4' &amp;&amp; IndicatorSubGroup='$subindg4' &amp;&amp; (Country IN ('$sql_cntys') OR WHORegionAC IN ('$sql_cntys')) "); $num5 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' &amp;&amp; Pathogen='$pathogen' &amp;&amp; Topic='$topic5' &amp;&amp; Indicator='$ind5' &amp;&amp; IndicatorSubGroup='$subindg5' &amp;&amp; (Country IN ('$sql_cntys') OR WHORegionAC IN ('$sql_cntys')) "); $data = array(); while($row = mysql_fetch_assoc($num1)) { $c = $row['Country']; if (!isset($data[$c])) { $data[$c] = array('Country' =&gt; $c); } $data[$c]['MidEstimate1'] = $row['MidEstimate']; } while($row = mysql_fetch_assoc($num2)) { $c = $row['Country']; if (!isset($data[$c])) { $data[$c] = array('Country' =&gt; $c); } $data[$c]['MidEstimate2'] = $row['MidEstimate']; } while($row = mysql_fetch_assoc($num3)) { $c = $row['Country']; if (!isset($data[$c])) { $data[$c] = array('Country' =&gt; $c); } $data[$c]['MidEstimate3'] = $row['MidEstimate']; } while($row = mysql_fetch_assoc($num4)) { $c = $row['Country']; if (!isset($data[$c])) { $data[$c] = array('Country' =&gt; $c); } $data[$c]['MidEstimate4'] = $row['MidEstimate']; } while($row = mysql_fetch_assoc($num5)) { $c = $row['Country']; if (!isset($data[$c])) { $data[$c] = array('Country' =&gt; $c); } $data[$c]['MidEstimate5'] = $row['MidEstimate']; } $i = 0; echo "&lt;table width='880' align='center'&gt;"; foreach ($data as $row) { echo ($i % 5) ? "&lt;tr&gt;" : "&lt;tr&gt;" ; echo "&lt;td style='padding-left:10px' width='280'&gt;" . $row['Country']."&lt;/td&gt;"; echo "&lt;td align='center' width='120'&gt;" . $row['MidEstimate1']."&lt;/td&gt;"; echo "&lt;td align='center' width='120'&gt;" . $row['MidEstimate2']."&lt;/td&gt;"; echo "&lt;td align='center' width='120'&gt;" . $row['MidEstimate3']."&lt;/td&gt;"; echo "&lt;td align='center' width='120'&gt;" . $row['MidEstimate4']."&lt;/td&gt;"; echo "&lt;td align='center' width='120'&gt;" . $row['MidEstimate5']."&lt;/td&gt;"; echo "&lt;/tr&gt;" ; } echo "&lt;/table&gt;" ; </code></pre> <p>How can I sort this table column wise ? that is using a drop down list with values "Country", "Indicator 1", "Indicator 2", "Indicator 3", "Indicator 4", "Indicator 5". when a user select values table must list values with respective sorted column. Please help.</p> <p><strong>I got the answer from here</strong> <a href="http://www.the-art-of-web.com/php/sortarray/" rel="nofollow noreferrer">Sort Multi Array</a></p>