PHP foreach循环不';不要分成两行

PHP foreach循环不';不要分成两行,php,html,css,Php,Html,Css,执行这段代码,我会得到一个类别列表。它是这样显示的 我的代码如下所示: <tr> <td align="right"> <legend><?php _e( 'Category', self::nspace ); ?></legend> </td> <td> <div class="radio-group"> <?php foreach ( $this->

执行这段代码,我会得到一个类别列表。它是这样显示的

我的代码如下所示:

<tr>
 <td align="right">
    <legend><?php _e( 'Category', self::nspace ); ?></legend>
 </td>
 <td>
    <div class="radio-group">
       <?php foreach ( $this->get_media_categories() as $slug => $name ): ?>
       <label>
             <tr>
                <td>
                   <input type="checkbox" name="media-categories[]" value="<?php echo $slug; ?>"<?php if ( in_array( $slug, $_REQUEST['media-categories'] ) ): ?> checked<?php endif; ?>> 
                   <?php echo $name; ?>
                </td>
             </tr>

       </label>
       <?php endforeach; ?>
    </div>
 </td>
</tr>
我的问题是,我应该如何打破每个循环,使它看起来像这样

编辑: 以下是全部代码:

<form id="mediacat-library-search-form" action="<?php echo get_site_url(); ?>/<?php if ( get_option( 'permalink_structure' ) ): ?><?php echo $this->settings_data['rewrite_url']; ?><?php else: ?>?page_id=11735<?php endif; ?>" method="post">
  <table class="table table-bordered" style="text-align: right;" >
    <div class="cols two-cols table-responsive table-bordered">
    <tr>
      <td align="right">
        <legend><?php _e( 'Category', self::nspace ); ?></legend>
      </td>
      <td>
        <div class="radio-group">
          <?php foreach ( $this->get_media_categories() as $slug => $name ): ?>
          <label>
    <tr>
    <td>
    <input type="checkbox" name="media-categories[]" value="<?php echo $slug; ?>"<?php if ( in_array( $slug, $_REQUEST['media-categories'] ) ): ?> checked<?php endif; ?>> 
    <?php echo $name; ?>
    </td>
    </tr>
    </label>
    <?php endforeach; ?>
    </div>
    </td>
    </tr>
    <tr>
      <td align="right">
        <legend><?php _e( 'Keyword', self::nspace ); ?></legend>
      </td>
      <td>
        <p class="form-row" id="mediacat-library-keyword">
          <input type="text" name="keyword" id="keyword" value="<?php if ( isset( $_REQUEST['keyword'] ) ) echo $_REQUEST['keyword']; ?>">
        </p>
      </td>
    </tr>
    <tr>
      <td>
      <td align="center" >
        <input type="hidden" name="mediacat_library_submit" value="1">
        <input type="submit"  style=" border: 2px solid #006a4e; background: #ffffff; text-shadow: #ffffff 0 1px 0; padding: 9px 18px; color: #006a4e; font-size: 14px; font-family: helvetica, serif; text-decoration: none; vertical-align: middle; cursor: pointer;"  value="<?php _e( 'Search', self::nspace ); ?>">
      </td>
      <td> </td>
    </tr>
  </table>
</form>
<?php if ( isset( $_REQUEST['keyword'] ) || isset( $_REQUEST['media-categories'] ) )  $this->mediacat_library( true ); ?>

您需要使用计数器来跟踪和拆分列。请检查下面的代码

<tr>
    <td align="right">
       <legend><?php _e( 'Category', self::nspace ); ?></legend>
    </td>
    <td>
       <div class="radio-group">
          <?php 
              $count = 0;
              foreach ( $this->get_media_categories() as $slug => $name ): ?>
          <?php if(($count%2) == 0) : ?>
           <label>
                <tr>
          <?php endif; ?>           
                   <td>
                      <input type="checkbox" name="media-categories[]" value="<?php echo $slug; ?>"<?php if ( in_array( $slug, $_REQUEST['media-categories'] ) ): ?> checked<?php endif; ?>> 
                      <?php echo $name; ?>
                   </td>
           <?php if(($count%2) == 0) : ?>
                </tr>
           </label>
          <?php endif; ?>
           <?php $count++; ?>
          <?php endforeach; ?>
       </div>
    </td>
 </tr>


您只需使用CSS
列计数就可以做到这一点:

.radio-group {
    column-count: 2;
}

您正在为每列使用行。您需要在每2个
td
s之后使用适当的
结构关闭
tr
。当前您正在
td
内打印
tr
s。试一试-

<div class="radio-group">
   <table>
   <tr>
   <?php 
   $i = 0;
   foreach ( $this->get_media_categories() as $slug => $name ): 
   ?>
            <td>
               <input type="checkbox" name="media-categories[]" value="<?php echo $slug; ?>"<?php if ( in_array( $slug, $_REQUEST['media-categories'] ) ): ?> checked<?php endif; ?>> 
               <?php echo $name; ?>
            </td>
   <?php
   $i++; 
   if($i%2 == 0) {
      echo "</tr><tr>";
   }
   endforeach; 
   ?>
   </tr>
   </table>
</div>

您可以尝试以下方法:

我还没有测试过这段代码,但我认为它会起作用。它是计数器的简单组合

<?php
$tdCount = 0;
$tabletd = ''; 
foreach ( $this->get_media_categories() as $slug => $name ): ?>
           <label>
                <?php if($tdCount == 2) { ?>
                 <tr>
                    <?php echo $tabletd;?>
                 </tr>
                 <?php
                    $tdCount = 0;
                    $tabletd = ''; 
                  }
                    $tdCount++;
                    $checked = '';
                    if ( in_array( $slug, $_REQUEST['media-categories'] ) ){ $checked = 'checked';}
                    $tabletd.= '<td>
             <input type="checkbox" name="media-categories[]" value="'.$slug.'" '.$checked.'> 
             '.$name.'
                     </td>';
                   ?>

           </label>
<?php endforeach; ?>
<?php if($tabletd!= '')  {?>
<label>
                 <tr>
                    <?php echo $tabletd;?>
                 </tr>
           </label>
<?php } ?>


column count适用于block元素,这里是关于表的内容是正确的,我们可以使用CSS 3功能实现这一点。我已经用PHP给出了解决方案谢谢你的回答。不管我怎么做,都没有改变。谢谢你的回答。我试过这个,事实上它只改变了我的风格,但它仍然没有把它分成两行?请检查我编辑了我的问题,我确实添加了整个代码可能有冲突,我注意不到@RuchishParikhIt会打印-
第一次0,然后它会打印。但是如果我删除了表,我还有其他元素吗?我应该把这个放在桌子里面吗?请检查我张贴了整个页面?谢谢你的回答
<ul>
<?php foreach ( $this->get_media_categories() as $slug => $name ): ?>
    <li>
         <label>
                <input type="checkbox" name="media-categories[]" value="<?php echo $slug; ?>"<?php if ( in_array( $slug, $_REQUEST['media-categories'] ) ): ?> checked<?php endif; ?>> 
                <?php echo $name; ?>
         </label>
    </li>
<?php endforeach; ?>
</ul>
li {
    list-style: none;
    width: 50%;
    display: inline-block;
    /*rest of the styles*/
}
<?php
$tdCount = 0;
$tabletd = ''; 
foreach ( $this->get_media_categories() as $slug => $name ): ?>
           <label>
                <?php if($tdCount == 2) { ?>
                 <tr>
                    <?php echo $tabletd;?>
                 </tr>
                 <?php
                    $tdCount = 0;
                    $tabletd = ''; 
                  }
                    $tdCount++;
                    $checked = '';
                    if ( in_array( $slug, $_REQUEST['media-categories'] ) ){ $checked = 'checked';}
                    $tabletd.= '<td>
             <input type="checkbox" name="media-categories[]" value="'.$slug.'" '.$checked.'> 
             '.$name.'
                     </td>';
                   ?>

           </label>
<?php endforeach; ?>
<?php if($tabletd!= '')  {?>
<label>
                 <tr>
                    <?php echo $tabletd;?>
                 </tr>
           </label>
<?php } ?>