php foreach将表列中的多个数据分开

时间:2013-06-03 01:38:32

标签: php codeigniter

我想要做的是在2或3个表格列中分开数据

这是我的代码,但我的代码现在只显示在一列

<?php if(isset($queue_record)) : foreach($queue_record as $row) : ?>

<tr>
<td>
<label class="checkbox ">
<input type="checkbox" name="check_queue[]" id="inlineCheckbox1"
          class="inlineCheckbox1" value="<?php echo $row->tenantqueueid; ?>">
          <?php echo $row->queuename; ?>
</label>
</td>
</tr>

<?php endforeach; ?>
<?php endif; ?>

我实际想要显示的内容,但屏幕截图数据只是一个示例,实际上2列数据不同

enter image description here

1 个答案:

答案 0 :(得分:2)

你必须做这样的事情:

<?php 
if(isset($queue_record)){
    echo "<table>";
    echo "<tr>";
    echo "<th>Column 1 Header</th>";
    echo "<th>Column 2 Header</th>";
    echo "<th>Column 3 Header</th>";
    echo "</tr>";

    $num_cols = 3; //We set the number of columns
    $current_col = 0;
foreach($queue_record as $row):
if($current_col == "0")echo "<tr>"; //Creates a new row if $curent_col equals to 0
?>
<td>
<label class="checkbox ">
<input type="checkbox" name="check_queue[]" id="inlineCheckbox1"
          class="inlineCheckbox1" value="<?php echo $row->tenantqueueid; ?>">
          <?php echo $row->queuename; ?>
</label>
</td>
<?php 
   if($current_col == $num_cols-1){ // Close the row if $current_col equals to 2 in the example ($num_cols -1)
       echo "</tr>";
       $current_col = 0;
   }else{
       $current_col++;
   }
endforeach;
echo "</table>";

<?php endif; ?>