Gopipuli
Gopipuli

Reputation: 393

How do I sort this multiple queried result table in PHP using a drop down?

I got a code which generate a table like this

table

and the code is here

$num1 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' && Pathogen='$pathogen' && Topic='$topic1' && Indicator='$ind1' && IndicatorSubGroup='$subindg1' && (Country IN ('$sql_cntys') OR  WHORegionAC IN ('$sql_cntys')) ");
$num2 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' && Pathogen='$pathogen' && Topic='$topic2' && Indicator='$ind2' && IndicatorSubGroup='$subindg2' && (Country IN ('$sql_cntys') OR  WHORegionAC IN ('$sql_cntys')) ");
$num3 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' && Pathogen='$pathogen' && Topic='$topic3' && Indicator='$ind3' && IndicatorSubGroup='$subindg3' && (Country IN ('$sql_cntys') OR  WHORegionAC IN ('$sql_cntys')) ");
$num4 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' && Pathogen='$pathogen' && Topic='$topic4' && Indicator='$ind4' && IndicatorSubGroup='$subindg4' && (Country IN ('$sql_cntys') OR  WHORegionAC IN ('$sql_cntys')) ");
$num5 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' && Pathogen='$pathogen' && Topic='$topic5' && Indicator='$ind5' && IndicatorSubGroup='$subindg5' && (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' => $c);
    }
    $data[$c]['MidEstimate1'] = $row['MidEstimate'];
}
while($row = mysql_fetch_assoc($num2))
{
    $c = $row['Country'];
    if (!isset($data[$c]))
    {
        $data[$c] = array('Country' => $c);
    }
    $data[$c]['MidEstimate2'] = $row['MidEstimate'];
}

while($row = mysql_fetch_assoc($num3))
{
    $c = $row['Country'];
    if (!isset($data[$c]))
    {
        $data[$c] = array('Country' => $c);
    }
    $data[$c]['MidEstimate3'] = $row['MidEstimate'];
}

while($row = mysql_fetch_assoc($num4))
{
    $c = $row['Country'];
    if (!isset($data[$c]))
    {
        $data[$c] = array('Country' => $c);
    }
    $data[$c]['MidEstimate4'] = $row['MidEstimate'];
}

while($row = mysql_fetch_assoc($num5))
{
    $c = $row['Country'];
    if (!isset($data[$c]))
    {
        $data[$c] = array('Country' => $c);
    }
    $data[$c]['MidEstimate5'] = $row['MidEstimate'];
}
$i = 0;
echo "<table width='880' align='center'>";
foreach ($data as $row)
{
    echo ($i % 5) ? "<tr>" : "<tr>" ;
    echo "<td style='padding-left:10px' width='280'>" . $row['Country']."</td>";
    echo "<td align='center' width='120'>" . $row['MidEstimate1']."</td>";
    echo "<td align='center' width='120'>" . $row['MidEstimate2']."</td>";
    echo "<td align='center' width='120'>" . $row['MidEstimate3']."</td>";
    echo "<td align='center' width='120'>" . $row['MidEstimate4']."</td>";
    echo "<td align='center' width='120'>" . $row['MidEstimate5']."</td>";
    echo "</tr>" ;
}

echo "</table>" ;

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.

I got the answer from here Sort Multi Array

Upvotes: 0

Views: 653

Answers (2)

Gopipuli
Gopipuli

Reputation: 393

    <?PHP 
function orderBy($data, $field) { 
$code = "return strnatcmp(\$a['$field'], \$b['$field']);"; 
usort($data, create_function('$a,$b', $code)); 
return $data; } 

$data = orderBy($data, 'age'); 
?>

The complete reference of the code is available from here

Upvotes: 1

s.webbandit
s.webbandit

Reputation: 17000

Looks like you should gather all queries to one. Something like SELECT * FROM matble WHERE .....

And then iterate over result and create output depending on your requirements.

Upvotes: 0

Related Questions