0

我想根据计划设置表格产品中的结构,我想这样显示

+------------------+---------------+---------------+
| product1         | product2      | product3      |
+------------------+---------------+---------------+
| product1plan1    | product2plan1 | product3plan1 |
+------------------+---------------+---------------+ 
| prouduct1plan2   | product2plan2 |               |
+------------------+---------------+---------------+
| product1plan3    |               |               |
+------------------+---------------+---------------+

但现在它向我展示了那样

  product1 | product1plan1 | product1plan2  product2 | product2plan1 | product2plan1

这是我的代码

 <table>
<thead>
     <tr>
    <?php 
       foreach( $name as $product ) {
        $c = 0;
        foreach ($product as $key ) {
            $c++;
            ?>              
       <th>
                 <?php echo $key['Product']['name']; ?>
         <?php for($i = 0; $i < sizeof( $key['ProductPlan'] ); $i++ ) { ?>
         <th class= 'packages' id = "<?php echo $key['Product']['name'], $key['ProductPlan'][$i]['product_plan_id']?>" 
           onclick = "document.getElementById('<?php echo $key['Product']['name']?>').value='<?php echo $key['ProductPlan'][$i]['product_plan_id']?>';"
                >
            <div product_name='<?php echo $key['Product']['slug']?>' class='amounts amount-<?php echo $key['Product']['slug']?>'>
               <div class='tick'>
                  <?php echo $this->Html->image('tick.png', array('width'=>'25', 'height'=>'25'));?>
              </div>
             <div class='amount'>
         <?php echo $key['ProductPlan'][$i]['name'];?> $
             </div>
          </div>            
           </th>
           <?php } ?>
       </th>
     <?php } ?>         
   <?php } ?>
</tr>
  </thead>

4

1 回答 1

2

在没有看到工作示例的情况下很难弄清楚这一点,但是查看问题中的代码,您在 th 标签内有一个 th 标签,这就是您看到结果的原因。我相信如果你用 div 标签替换内部 th 标签,那应该可以解决问题。

但是, th 标签实际上应该用于表格标题,或者在您的示例中,产品名称。为此,您将需要 2 个循环:第一个创建带有产品名称的表格标题,第二个用于打印计划(在标题下方一行的表格单元格中)。

试试这个:

<table>
    <thead>
        <tr>
            <th>
                <?php 
                    foreach ($name as $product) {
                        foreach ($product as $key) {
                            echo $key['Product']['name'];
                        }
                    }
                ?>
            </th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <?php 
                foreach($name as $product) {
                    $c = 0;
            ?>

            <?php 
                foreach ($product as $key) {
                $c++;
            ?>

            <td>
                <?php for($i=0;$i<sizeof($key['ProductPlan']);$i++) { ?>

                <div class   = 'packages'
                     id      = "<?php echo $key['Product']['name'],$key['ProductPlan'][$i]['product_plan_id']?>" 
                     onclick = "document.getElementById('<?php echo $key['Product']['name']?>').value='<?php echo $key['ProductPlan'][$i]['product_plan_id']?>';"
                >

                    <div product_name='<?php echo $key['Product']['slug']?>' class='amounts amount-<?php echo $key['Product']['slug']?>'>
                        <div class='tick'>
                            <?php echo $this->Html->image('tick.png', array('width'=>'25', 'height'=>'25'));?>
                        </div>

                        <div class='amount'>
                            <?php echo $key['ProductPlan'][$i]['name'];?> $
                        </div>
                    </div>
                </div>
                <?php }?>
            </td>

            <?php } ?>
            <?php } ?>
        </tr>
    </tbody>
</table>    
于 2013-02-14T06:23:39.220 回答