【问题标题】:Move result to the right position on table in PHP在PHP中将结果移动到表格上的正确位置
【发布时间】:2017-03-07 06:44:42
【问题描述】:

我正在尝试在表格(图 1)上显示 7 个结果,我想将它们移动到正确的位置。

Mon = 1 ~ Sun = 7,正如我们在阵列上看到的那样,在没有价值的地方,我希望它是 0 美元。目前我不知道如何将它移动到桌子上的正确位置。

这是我的代码:

$w_rep = "SELECT 
      `totals`.`totals_weeknumb`,
      `totals`.`totals_weekday`,
      `totals`.`totals_of_day`
    FROM `totals`
    WHERE `totals`.`totals_has_users_id` = :fromID";
$w_rep_stmt = $DB->prepare($w_rep);
$w_rep_stmt->execute(array(':fromID' => $fromID));
$w_rep_stmt->setFetchMode(PDO::FETCH_ASSOC);

$totals_of_day = array();
foreach ($w_rep_stmt as $report) {
    //$tdate = new DateTime($report['totals_date']);
    //$totals_date = $tdate->format('d-m-Y');

    $t_wnumber = (int)$report['totals_weeknumb'];
    $t_wday = (int)$report['totals_weekday'];

    $totals_of_day[$t_wnumber][$t_wday] = (float)$report['totals_of_day'];

    //$sum_of_week[$t_wnumber] = array_sum(array_column($totals_of_day, ''));

} // close foreach

var_dump(array_keys($totals_of_day));
var_dump($totals_of_day);

foreach ($totals_of_day as $w_n => $y) {
?>
<tr>
    <th><?php echo $w_n; ?></th>
    <td><a href="#" title="Click for more details">More Details</a></td>
    <?php
    foreach ($y as $w_day => $saved) {
        echo '<td>'. $curr.number_format($saved, 2) .'</td>';
    } // close foreach
    ?>
    <td><?php //echo $curr.$sum_of_week[]; ?></td>
</tr>
<?php
} // close 1st foreach

我尝试了很多选择:(

【问题讨论】:

  • 所以您只是缺少 0$ 值?
  • 是的,将结果移到正确的日期!

标签: php html arrays html-table


【解决方案1】:

基本上你需要迭代星期几并使用数组中的索引

foreach (range(1, 7) as $day) {
    echo (isset($y[$day]) ? $y[$day] : '-');
}

示例:

<?php
$totals_of_day = [
    7 => [3 => 150.8, 4 => 523.88, 5 => 95.32, 7 => 10.8],
    8 => [1 => 13.78, 2 => 107.33, 4 => 8.49, 5 => 125.67],
    9 => [1 => 71.3, 2 => 49.68, 6 => 95, 7 => 100.57],
    10 => [1 => 18.34, 2 => 44.9, 3 => 55.7, 4 => 15.58]
];
?>

<table border="1" style="border-collapse: collapse;">
    <tr>
        <th>Week</th>
        <th>Items</th>
        <th>Mon</th>
        <th>Tue</th>
        <th>Wed</th>
        <th>Thu</th>
        <th>Fri</th>
        <th>Sat</th>
        <th>Sun</th>
        <th>Total Savings</th>
    </tr>
    <?php foreach ($totals_of_day as $w_n => $y) { ?>
    <tr>
        <th><?php echo $w_n; ?></th>
        <td><a href="#" title="Click for more details">More Details</a></td>
        <?php foreach (range(1, 7) as $day) {
            echo '<td>'. (isset($y[$day]) ? number_format($y[$day], 2) : '-') .'</td>';
        } ?>
        <td><?php echo number_format(array_sum($y), 2); ?></td>
    </tr>
    <?php } ?>
</table>

【讨论】:

  • 呜呜呜!!!!!!太棒了,非常感谢你.... Você é o cara do PHP.... valeu... Obrigadooooo Sergio...
猜你喜欢
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
  • 2016-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多