while循环中的表并排 [英] Table in while loop side by side
本文介绍了while循环中的表并排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在while循环中它创建一个标题和图像链接列表,我想像下图一样并排显示它.每个标题都作为新列出现,图像应该在每个类别下.只有3行.
in a while loop its creating a list of heading and image links, i want to display it as side by side like in following image.Each heading shold comes as new column and image should come under each category.and only 3 rows.
这是我的代码:
<table>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
<th>Heading 3</th>
</tr>
<?php
//example array $images = array('Image 1', 'Image 2', 'Image 3', 'Image 4', 'Image 5', );
for ($i=0; $i<count($images); $i++) { ?>
<tr>
<td>
<?php echo $images[$i]; ?></td>
<td><?php echo $i<3?$images[$i]:''; ?></td>
<td><?php echo $i<1?$images[$i]:''; ?></td>
</tr> <?php } ?>
</table>
推荐答案
我不确定我是否完全理解您的需求,但是这个呢:
I'am no sure if i understand well what you need, but what about this:
$headings = ['heading 1', 'heading 2', 'heading 3'];
$images = [
'heading 1' => ['image 1', 'image 2', 'image 3', 'image 4', 'image 5'],
'heading 2' => ['image 1', 'image 2', 'image 3'],
'heading 3' => ['image 1']
];
$rows = [];
foreach($headings as $heading){
$rowId = 0;
$rows[$rowId][] = '<th>'.$heading.'</th>';
if(isset($images[$heading])){
foreach($images[$heading] as $image){
$rowId += 1;
if($rowId == 3){
$rowId = 0;
}
$rows[$rowId][] = '<td>'.$image.'</td>';
}
}
for($i = $rowId + 1; $i < 3; $i++){
$rows[$i][] = '<td> </td>';
}
}
echo '<table>';
foreach($rows as $row){
echo '<tr>';
foreach($row as $column){
echo $column;
}
echo '</tr>';
}
echo '</table>';
这篇关于while循环中的表并排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文