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><?php echo $specName?></td>
            <?php foreach($tunnid as $tund): ?>

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

我已经尝试了这个线程中的所有内容:表 PHP 中的嵌套循环 ,但这些都不起作用输出如下:

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

等等

我希望它是:

名称 1 - 值1
名称 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

4 回答 4

0
<?php
    $i = 0;
    foreach($specs as $specName => $spec){
?>
    <tr>
        <td><?php echo $specName?></td>
        <td><?php echo $tunid[$i++]; ?></td>
    </tr>
    }
?>
于 2013-06-14T11:53:24.070 回答
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>
于 2013-06-14T11:55:25.407 回答
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
于 2013-06-14T11:44:38.923 回答
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 包含一个名称的值,我的代码将起作用。但我认为情况并非如此!?

于 2013-06-14T11:47:38.527 回答