PHP嵌套foreach在表中

时间:2013-06-14 11:38:05

标签: php loops foreach

我似乎无法让我的桌子获得正确的输出。代码如下:

<div class="bubbleTitle">Spetsialistide tööaeg graafiku alusel</div>
    <table style="width: 600px" class="slicedTable">

        <tr>
        <th>Spetsialist</th>
        <th>Tunnid</th>
    </tr>

<tr>            
    <?php foreach($specs as $specName => $spec): ?>
    <td><?php echo $specName?></td>
            <?php foreach($tunnid as $tund): ?>

    <td><?php echo $tund?></td>
</tr>
     <?php endforeach; ?>
<?php endforeach; ?>

我已尝试过这个帖子中的所有内容:Nested Loop in table PHP,但这些都没有奏效 输出如下:

名称 - 价值
值2
值3
名称2 - 价值
值2
值3

我希望它是:

name1 - value1
名称2 - 值2等。

$tunnid来自哪里:

result = mysql_query("SELECT `worker_id`, SUM(TIMESTAMPDIFF(HOUR, `start`, `end`)) as `total` FROM
    `spa_worker_times` WHERE (`start` BETWEEN '".$validated['start']."' AND '".$validated['end']."') AND
    (`end` BETWEEN '".$validated['start']."' AND '".$validated['end']."') GROUP BY `worker_id`") or die(mysql_error()); 


$tunnid = array();
while ($row = mysql_fetch_assoc($result)) {
    $tunnid[] = $row['total'];

规格来自:

$data = $this->BookingProcedures->query("SELECT AProcedure.name, BookingGroup.booking_package_id > 0 AS pack_proc," .
                        "SUM(BookingProcedure.price) AS price, Worker.name, COUNT(BookingProcedure.id) AS num" .
                    " FROM spa_booking_procedure_specialists BookingProcedureSpecialist, " .
                        "spa_booking_procedures BookingProcedure, " .
                        "spa_booking_groups BookingGroup, spa_procedures AProcedure, spa_workers Worker" .
                    " WHERE !BookingProcedure.deleted" .
                        " AND DATE(BookingProcedure.start) >= '".$validated['start']."'" .
                        " AND DATE(BookingProcedure.start) <= '".$validated['end']."'" .
                        " AND BookingProcedureSpecialist.booking_procedure_id = BookingProcedure.id" .
                        " AND Worker.id = BookingProcedureSpecialist.specialist_id" .
                        " AND BookingGroup.id = BookingProcedure.group_id" .
                        " AND AProcedure.id = BookingGroup.procedure_id" .
                    " GROUP BY AProcedure.name, BookingGroup.booking_package_id > 0, Worker.name");

4 个答案:

答案 0 :(得分:0)

<div class="bubbleTitle">Spetsialistide tööaeg graafiku alusel</div>
<table style="width: 600px" class="slicedTable">
     <tr>
      <th>Spetsialist</th>
      <th>Tunnid</th>
     </tr>
     <tr>            
       <?php foreach($specs as $specName => $spec): ?>
        <td><h2><?php echo $specName?></h2>
        <table><tr>
        <?php foreach($tunnid as $tund): ?>
           <td><?php echo $tund?></td>
       <?php endforeach; ?>
       </tr></table></td>
       <?php endforeach; ?>
     </tr>
 </table>

这将输出如下

Name1
value1 | value2 | value3
Name2
value1 | value2 | value3

答案 1 :(得分:0)

<?php foreach($specs as $specName => $spec) { ?> 
  <tr>            
    <td><?php echo $specName?></td>
    <?php foreach($tunnid as $tund) { ?>
      <td><?php echo $tund?></td>
    <?php } ?>
  </tr>
<?php } ?>

将导致

名称值值
名称值 名称值
等等

$ tunnid来自哪里。我认为它只包含值,但是以哪种方式,它们是常规数组还是关联数?如果$ tunnid包含一个名称的值,我的代码将起作用。但我认为情况并非如此!?

答案 2 :(得分:0)

<?php
    $i = 0;
    foreach($specs as $specName => $spec){
?>
    <tr>
        <td><?php echo $specName?></td>
        <td><?php echo $tunid[$i++]; ?></td>
    </tr>
    }
?>

答案 3 :(得分:0)

<table style="width: 600px" class="slicedTable">
     <tr>
      <th>Spetsialist</th>
      <th>Tunnid</th>
     </tr>
     <tr>            
       <?php foreach($specs as $specName => $spec): ?>
        <td><?php echo $specName?></td>
    <!-- seperate with anything -->
    <td><?php echo implode(',', $tunnid); ?></td>
       <?php endforeach; ?>
     </tr>
 </table>