0

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

6
  • Are you committed to using SELECT boxes for the sorting? If not take a look at jQuery DataTables plugin datatables.net Commented Sep 1, 2011 at 7:26
  • yest I want to use a select box ? any help ? Commented Sep 1, 2011 at 7:31
  • See if this will do anything like what you want: datatables.net/release-datatables/examples/api/… Commented Sep 1, 2011 at 7:32
  • thanx but my table is a result of multiple queries how do I sort that ? also I want it by a selection of dropdown list. I managed with some ajax code to open a sortby.php page and tried wwith sql sorting using if condition but its not giving the right output. ?? Commented Sep 1, 2011 at 7:44
  • I got the answer from here [link]the-art-of-web.com/php/sortarray/[link], hope it might useful to all ... thanx for the help Commented Sep 2, 2011 at 4:53

2 Answers 2

1
    <?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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

I did not understand what you suggest, could u give me a hint/sample ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.