【问题标题】:Print two columns array into HTML table将两列数组打印到 HTML 表中
【发布时间】:2014-09-15 09:48:39
【问题描述】:

我必须在 HTML 表中显示以下查询:

$query= "SELECT SUM(amount_pln) AS suma,country FROM fin_revenues GROUP BY country ORDER BY suma DESC";
$result = mysqli_query($mysqli,$query);

在 PHPMYADMIN 中运行查询时,它可以按我的需要完美运行,但我不知道如何在 html 表上获得相同的结果。

phpmyadmin中是这样显示的:

任何帮助将不胜感激。

【问题讨论】:

    标签: php html arrays html-table


    【解决方案1】:
        $query= "SELECT SUM(amount_pln) AS suma,country FROM fin_revenues GROUP BY country ORDER BY suma DESC";
        $result = mysqli_query($mysqli,$query);
    
        echo "<table><tr><td>SUMA</td><td>country</td></tr>";
    
        while($row = mysqli_fetch_assoc($result)){
        echo "<tr><td>".$row['suma']."</td><td>".$row['country']."</td></tr>";
        }
    echo "</table>";
    mysqli_free_result($result);
    

    【讨论】:

    • 谢谢,到目前为止我只得到标题 SUMA 和国家
    • @user2952715,我已经更新了我的答案。你有什么错误吗?
    • 两个答案看起来都相似,如果您的答案是第一个且正确的,而不是其他人再次发布相同答案的原因。从我这边投票支持你的回答
    【解决方案2】:
    $query= "SELECT SUM(amount_pln) AS suma,country FROM fin_revenues GROUP BY country ORDER BY suma DESC";
    $result = mysqli_query($mysqli,$query);
    echo '<table>
            <tr>
              <td>Suma</td>
              <td>Country></td>
           </tr>';
    while($row=mysqli_fetch_array($mysqli,$result))   // while there are records
    {
        echo "<tr><td>".$row['suma']."</td><td>".$row['country']."</td></tr>";   // echo the table with query results
    }
    echo '</table';
    

    【讨论】:

    • 我在以下行中出现错误,似乎是因为单引号 echo " $row['suma'] $row['country '] ";
    猜你喜欢
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2013-02-28
    相关资源
    最近更新 更多