2017-03-07 42 views
0

餐桌上合適的位置,我想顯示7個結果在表(圖片1),我想將其移動到合適的位置。移動導致在PHP

週一= 1〜週日= 7,因爲我們可以在陣列上看到哪裏還有沒有價值,我想擁有它$ 0目前我無法弄清楚如何將它移到桌子上的正確位置。

image 1

這裏是我的代碼:

$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

所以你只是缺少$ 0值? – mrahmat

+0

是的,並將結果移到正確的一天! –

回答

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> 

example result

+0

Woooowwww !!!!!! 這真是太棒了,謝謝soooooo .... Vocêéo cara do PHP .... valeu ... Obrigadooooo Sergio ... –