2011-09-01 29 views
0

我能產生這樣的如何使用下拉列表在PHP中對這個多重查詢結果表進行排序?

table

表中的代碼和代碼是在這裏

$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>" ; 

我怎樣才能明智這個排序表列?即使用值爲「國家」,「指標1」,「指標2」,「指標3」,「指標4」,「指標5」的下拉列表。當用戶選擇值表時,必須列出具有相應排序列的值。請幫忙。

我得到的答案從這裏Sort Multi Array

+0

你致力於使用SELECT盒排序?如果不看看jQuery DataTables插件http://www.datatables.net/ –

+0

yest我想使用選擇框?任何幫助? – Gopipuli

+0

看看這會做你想做的任何事情:http://www.datatables.net/release-datatables/examples/api/multi_filter_select.html –

回答

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'); 
?> 

代碼的完整參考可從here

0

看起來你應該收集的所有查詢之一。像SELECT * FROM matble WHERE ....

然後遍歷結果並根據您的要求創建輸出。

+0

我不明白你的建議,你能給我一個提示/樣本嗎? – Gopipuli

相關問題