如何编写PHP循环以表格式显示数据库查询中的数据?

如何编写PHP循环以表格式显示数据库查询中的数据?,php,arrays,html-table,Php,Arrays,Html Table,阵列输出 print_r($hours_effort_result); //prints below output Array ( [0] => Array ( ) [1] => Array ( [0] => Array ( [RESOURCE_ID] => 1 [EMPLOYEE_NAME]

阵列输出

print_r($hours_effort_result); //prints below output

Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
            [0] => Array
            (
                [RESOURCE_ID] => 1
                [EMPLOYEE_NAME] => User 1
                [SITE_STATUS] => Onsite
                [ACTUAL_HOURS] => 120
                [MONTH_YEAR] => JUL-2015
            )

            [1] => Array
            (
                [RESOURCE_ID] => 2
                [EMPLOYEE_NAME] => User 2
                [SITE_STATUS] => Onsite
                [ACTUAL_HOURS] => 140
                [MONTH_YEAR] => JUL-2015
            )

        )

    [2] => Array
        (
            [0] => Array
            (
            [RESOURCE_ID] => 3
            [EMPLOYEE_NAME] => User 1
            [SITE_STATUS] => Offshore
            [ACTUAL_HOURS] => 170
            [MONTH_YEAR] => AUG-2015
            )   

            [1] => Array
                (
                    [RESOURCE_ID] => 4
                    [EMPLOYEE_NAME] => User 2
                    [SITE_STATUS] => Offshore
                    [ACTUAL_HOURS] => 180
                    [MONTH_YEAR] => AUG-2015
                )

        )

    [3] => Array
        (
        )

)

foreach ($hours_effort_result as $hours_effort_data){

        echo '<pre>';print_r($hours_effort_data);//below output
}

Array
(
)

Array
(
    [0] => Array
        (
            [RESOURCE_ID] => 1
            [EMPLOYEE_NAME] => User 1
            [SITE_STATUS] => Onsite
            [ACTUAL_HOURS] => 120
            [MONTH_YEAR] => JUL-2015
        )

    [1] => Array
        (
            [RESOURCE_ID] => 2
            [EMPLOYEE_NAME] => User 2
            [SITE_STATUS] => Onsite
            [ACTUAL_HOURS] => 140
            [MONTH_YEAR] => JUL-2015
        )

)

Array
(
    [0] => Array
        (
            [RESOURCE_ID] => 3
            [EMPLOYEE_NAME] => User 1
            [SITE_STATUS] => Offshore
            [ACTUAL_HOURS] => 170
            [MONTH_YEAR] => AUG-2015
        )

    [1] => Array
        (
            [RESOURCE_ID] => 4
            [EMPLOYEE_NAME] => User 2
            [SITE_STATUS] => Offshore
            [ACTUAL_HOURS] => 180
            [MONTH_YEAR] => AUG-2015
        )
)

Array
(
)
$project_months = Array('JUN-2015', 'JUL-2015', 'AUG-2015', 'SEP-2015' );
可以看到,月份
Jun
Sep
不包含任何数据,因此数组为空,而其他月份包含以表格格式打印的数据。要打印的唯一数据是
工程师姓名、现场和工时


如何循环数组以实现上述输出?

假设已定义此变量:

<?php 

$user_array = Array();

foreach ( $project_months as $month ) 
{
    foreach ($hours_effort_result as $key => $data)
    {
        if ( ! empty ($data) )
        {
            foreach ($data as $key => $val )
            {
                if ( $val['MONTH_YEAR'] == $month ) 
                {
                  $user_array[$val['EMPLOYEE_NAME']][$month]['SITE_STATUS'] = $val['SITE_STATUS'];
                  $user_array[$val['EMPLOYEE_NAME']][$month]['ACTUAL_HOURS'] = $val['ACTUAL_HOURS'];
                }
            }
        }
    }
}
?>
首先,创建一个用户数组:

<table>
<tr>
    <th>Name</th>
    <th>Site</th>
    <th>Jun-15</th>
    <th>Site</th>
    <th>Jul-15</th>
    <th>Site</th>
    <th>Aug-15</th>
    <th>Site</th>
    <th>Sep-15</th>
</tr>
<?php foreach ($user_array as $user_name => $user_data): ?>
    <tr>
        <td><?php print ($user_name); ?></td>
        <?php foreach ($project_months as $month): ?>
            <?php if ( array_key_exists( $month, $user_data ) ): ?>
                <td><?php print ($user_data[$month]['SITE_STATUS']); ?></td>
                <td><?php print ($user_data[$month]['ACTUAL_HOURS']); ?></td>
            <?php else: ?>
                <td>n/a</td>
                <td>n/a</td>
            <?php endif; ?>
        <?php endforeach; ?>
    </tr>
<?php endforeach; ?>
</table>

尝试在此代码中插入一条特征线

$table='';
$table.='';
$table.='';
$table.=“工程师姓名”;
$table.=“站点”;
foreach($arr as$key=>$data)
{
foreach($key2=>$val的数据)
{
如果(!空($val))
{
$table.=''.$val['MONTH_YEAR'].';
}
其他的
{
$table.='';
}
}
}
$table.='
'; $table.=''; $table.=''; foreach($arr as$key=>$data) { foreach($key2=>$val的数据) { 如果(!空($val)) { $table.=''.$val['EMPLOYEE_NAME'].'; $table.=''.$val['SITE_STATUS'].'; $table.=''.$val['ACTUAL_HOURS'].'; } 其他的 { $table.=''; $table.=''; } } } $table.=''; $table.=''; echo$表;
您能否
打印($hours\u efforce\u result)?@slimshaddyyy将如何设置$hours\u efforce\u result放入while循环,然后在其中显示数据。如果您包括如何将数组设置为相等的值,我会写一个例子。为什么是下一票?@slimshaddyyy我怀疑没有足够的信息,或者这个网站已经询问过它。您想让您的thead动态添加数据吗?您需要再次访问
$data
,因为它是嵌套数组。@aldrin27即将发布相同的内容。无法直接访问
$data
类似的内容
foreach($key=>$data的小时数){foreach($index=>$val的数据){$project\u months[$index][$key]=$val;}}
对,错过了。实际上,如果您的表行是按用户名分组的,那么最好只是重新创建一个包含每个用户数据的数组。请您发布一个关于相同数据的片段。输出会在
中多次打印相同的月份。我需要更改格式吗?我可以查看输出吗?示例输出<代码>工程师现场名称2015年7月2015年7月2015年7月2015年7月用户1现场40用户2现场140<代码>矿山输出是<代码>工程师现场名称2015年7月2015年8月2015年8月用户1现场120用户2现场140用户1海上170用户2是,但您会看到月份名称重复,工程师名称显示在一行中
<?php 

$user_array = Array();

foreach ( $project_months as $month ) 
{
    foreach ($hours_effort_result as $key => $data)
    {
        if ( ! empty ($data) )
        {
            foreach ($data as $key => $val )
            {
                if ( $val['MONTH_YEAR'] == $month ) 
                {
                  $user_array[$val['EMPLOYEE_NAME']][$month]['SITE_STATUS'] = $val['SITE_STATUS'];
                  $user_array[$val['EMPLOYEE_NAME']][$month]['ACTUAL_HOURS'] = $val['ACTUAL_HOURS'];
                }
            }
        }
    }
}
?>
<table>
<tr>
    <th>Name</th>
    <th>Site</th>
    <th>Jun-15</th>
    <th>Site</th>
    <th>Jul-15</th>
    <th>Site</th>
    <th>Aug-15</th>
    <th>Site</th>
    <th>Sep-15</th>
</tr>
<?php foreach ($user_array as $user_name => $user_data): ?>
    <tr>
        <td><?php print ($user_name); ?></td>
        <?php foreach ($project_months as $month): ?>
            <?php if ( array_key_exists( $month, $user_data ) ): ?>
                <td><?php print ($user_data[$month]['SITE_STATUS']); ?></td>
                <td><?php print ($user_data[$month]['ACTUAL_HOURS']); ?></td>
            <?php else: ?>
                <td>n/a</td>
                <td>n/a</td>
            <?php endif; ?>
        <?php endforeach; ?>
    </tr>
<?php endforeach; ?>
</table>
$table = '';
    $table .= '<table>';
    $table .= '<thead>';
    $table .= '<th>Name of Engineer</th>';
    $table .= '<th>Site</th>';

    foreach ($arr as $key => $data)
    {
        foreach($data as $key2 => $val)
        {
            if ( ! empty($val) )
            {

                $table .= '<th>' . $val['MONTH_YEAR'] . '</th>';
            }
            else
            {
                $table .= '</th>';

            }
        }

    }
    $table .= '<br>';
    $table .= '</thead>';
    $table .= '<tbody>';

    foreach ($arr as $key => $data)
    {
        foreach($data as $key2 => $val)
        {
            if ( ! empty($val) )
            {
                $table .= '<td>'.$val['EMPLOYEE_NAME'].'</td>';
                $table .= '<td>'. $val['SITE_STATUS'] . '</td>';
                $table .= '<td>'. $val['ACTUAL_HOURS'] .'</td>';


            }
            else
            {
                $table .= '<td></td>';
                $table .= '<td></td>';

            }

        }


    }


    $table .= '</tbody>';
    $table .= '</table>';
    echo $table;