繁体   English   中英

如何使用下拉列表对这个多重查询结果表进行排序?

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

我有一个代码生成这样的表

表

代码在这里

$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”的下拉列表。 当用户选择值表必须列出具有相应排序列的值时。 请帮忙。

我从这里得到了答案 排序多数组

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

此处提供了代码的完整参考

看起来您应该将所有查询收集到一个。 SELECT * FROM matble WHERE ....

然后根据您的要求遍历结果并创建输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM