dousi1994 2013-02-14 06:06
浏览 31
已采纳

如何在html中为每个循环设置多个表结构? [关闭]

I want to set the structure in table product against plan, i want to show like that

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

but now it showing me like that

  product1 | product1plan1 | product1plan2  product2 | product2plan1 | product2plan1

here is my code

 <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>
  • 写回答

1条回答 默认 最新

  • dpzp5127 2013-02-14 06:23
    关注

    It is a little hard to figure this out without seeing the working example, but looking at the code in your question, you have a th tag inside a th tag, which is why you are seeing the results you are. I believe if you replace the inner th tag with a div tag, that should solve the issue.

    However, the th tag is really supposed to be used for the table heading, or in your example, the product name. In order to do that, you will need 2 loops: 1 to create the table headings with the product names and a second to print out the plans (in a table cell in a row beneath your header).

    Try this:

    <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>    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分