使用 PHP foreach 函数创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16046828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:26:36  来源:igfitidea点击:

Creating a table with PHP foreach function

phparraysforeachhtml-table

提问by clo3o5

I'm in a class called database programming. We got a data set and and put it into our servers. Now I have to use a jquery plugin to help visualize that data. I am using Graph Us plugin and trying to use the "Fill In" option.

我在一个叫做数据库编程的课上。我们获得了一个数据集并将其放入我们的服务器中。现在我必须使用 jquery 插件来帮助可视化该数据。我正在使用 Graph Us 插件并尝试使用“填写”选项。

My professor helped me create this function:

我的教授帮我创建了这个函数:

<?php
include 'connect.php';
$country_query = "SELECT DISTINCT Country FROM FemaleMaleRatioNew";
$result = mysqli_query($sql_link, $country_query);
$new_row = array();
while ($row = mysqli_fetch_assoc($result)) {
    $country = $row['Country']; 
    $query = sprintf("SELECT Year, Value FROM FemaleMaleRatioNew WHERE Country =     '%s'", $country);  
    $country_result = mysqli_query($sql_link, $query);
    while ($country_row = mysqli_fetch_assoc($country_result) ) {
        $new_row[$country][] = array('year' => $country_row['Year'],
                            'value'=> $country_row['Value']
                            );

    }
}   

//print_r($new_row);


?>

the print_r($new_row);is only there to make sure it works and it does, it prints out the array when activated.

print_r($new_row);只存在,以确保它的工作原理,并启动时它,它打印出的阵列。

He then guided me to create the table like this:

然后他指导我像这样创建表格:

    <body>  

    <table id="demo">

    <?php  foreach($new_row as $row):?> 




            <tr>
                <td><?=$row['year'];?></td>
                <td><?=$row['country'];?></td>
            </tr>
    <?php endforeach;?>



    </table>

<script type="text/javascript">
$(document).ready(function() {

// Here we're "graphing up" only the cells with the "data" class
$('#demo td').graphup({
    // Define any options here
    colorMap: 'heatmap',
    painter: 'fill',
    // ...
});

});
</script>
</body>

What else do I need to do to get the table to work? I can't seem to figure it out. All it does is come out blank.

我还需要做什么才能使桌子工作?我似乎无法弄清楚。它所做的只是空白。

I'm sorry if this question isn't worded correctly or if I have not been clear on anything please let me know.

如果这个问题措辞不正确,或者我对任何事情都不清楚,我很抱歉,请告诉我。

回答by Kamil Szot

You have multiple rows for each country in your $new_row variable. You have to iterate over countries first and then over the individual rows of data:

在 $new_row 变量中,每个国家/地区都有多行。您必须先遍历国家,然后遍历各个数据行:

<?php  foreach($new_row as $country => $rows): ?>
  <?php  foreach($rows as $row): ?>
        <tr>
            <td><?=$country;?></td>
            <td><?=$row['Year'];?></td>
            <td><?=$row['Value'];?></td>
        </tr>
  <?php endforeach;?>
<?php endforeach;?>

Also please note that you need colon ':' not semicolon ';' after the foreachstatement. This syntax (which is less known) is described here: http://php.net/manual/en/control-structures.alternative-syntax.php

另请注意,您需要冒号 ':' 而不是分号 ';' 后foreach声明。这种语法(鲜为人知)在这里描述:http: //php.net/manual/en/control-structures.alternative-syntax.php

If you want to display some sort of aggregate (for example sum) per country and you want to calculate it in PHP (as opposed to MySQL) you can do it like this:

如果您想显示每个国家/地区的某种聚合(例如总和),并且想在 PHP(而不是 MySQL)中计算它,您可以这样做:

<?php foreach($new_row as $country => $rows): 
  $sum = 0;
  foreach($rows as $row): 
    $sum += $row['Value'];
  endforeach;
?>
        <tr>
            <td><?=$country;?></td>
            <td><?=$sum;?></td>
        </tr>
<?php endforeach;?>

回答by Marc B

You should be using a single JOINed query to do this stuff, but you may not have gotten that in class yet. Since it's homework, I won't give you the flat-out answer, but here's the pseudo-code:

您应该使用单个 JOINed 查询来执行此操作,但您可能还没有在课堂上使用过。既然是家庭作业,我不会给你直截了当的答案,但这是伪代码:

$countries = SELECT DISTINCT Country FROM YourTable;
while($country_row = fetch_row($countries)) {
   echo $country_row['Country'];
   echo <table>;
   $status = SELECT Year, Value FROM YourTable WHERE Country=$country_row['Country'];
   while($stats_row = fetch_row($status) {
        echo <tr><td>$stats_row['Year']</td><td>$stats_row['Value']}</td>
   }
   echo </table>
}