基于php html表的图库网格循环
我正在尝试使用PHP循环在html模板中创建一个图库 但是我必须将图库作为一个固定的表来编写,因此对每个图像和间距元素使用基于php html表的图库网格循环,php,loops,html-table,Php,Loops,Html Table,我正在尝试使用PHP循环在html模板中创建一个图库 但是我必须将图库作为一个固定的表来编写,因此对每个图像和间距元素使用。然后在新行上创建一个新表 我想我这样做是为了避免旧Outlook中出现任何问题 我已经创建了我的固体html布局,这是我需要它如何输出,见下文 <!-- 1st row --> <table border="0" cellspacing="0" cellpadding="0" style="width: 579px;" align="center
。然后在新行上创建一个新表
我想我这样做是为了避免旧Outlook中出现任何问题
我已经创建了我的固体html布局,这是我需要它如何输出,见下文
<!-- 1st row -->
<table border="0" cellspacing="0" cellpadding="0" style="width: 579px;" align="center">
<tr>
<td style="width: 135px; height: 148px;" valign="top">
<img src="img/image-1.jpg" height="135" width="135" style="width: 135px; height: 135px; background: red;" alt="" />
</td>
<td style="width: 13px; height: 148px;"><!-- space --></td>
<td style="width: 135px; height: 148px;" valign="top">
<img src="img/image-2.jpg" height="135" width="135" style="width: 135px; height: 135px; background: red;" alt="" />
</td>
<td style="width: 13px; height: 148px;"><!-- space --></td>
<td style="width: 135px; height: 148px;" valign="top">
<img src="img/image-3.jpg" height="135" width="135" style="width: 135px; height: 135px; background: red;" alt="" />
</td>
<td style="width: 13px; height: 148px;"><!-- space --></td>
<td style="width: 135px; height: 148px;" valign="top">
<img src="img/image-4.jpg" height="135" width="135" style="width: 135px; height: 135px; background: red;" alt="" />
</td>
</tr>
</table>
<!-- 2nd row -->
<table border="0" cellspacing="0" cellpadding="0" style="width: 579px;" align="center">
<tr>
<td style="width: 135px; height: 148px;" valign="top">
<img src="img/image-5.jpg" height="135" width="135" style="width: 135px; height: 135px; background: red;" alt="" />
</td>
<td style="width: 13px; height: 148px;"><!-- space --></td>
<td style="width: 135px; height: 148px;" valign="top">
<img src="img/image-6.jpg" height="135" width="135" style="width: 135px; height: 135px; background: red;" alt="" />
</td>
<td style="width: 13px; height: 148px;"><!-- space --></td>
<td style="width: 135px; height: 148px;" valign="top">
<img src="img/image-7.jpg" height="135" width="135" style="width: 135px; height: 135px; background: red;" alt="" />
</td>
<td style="width: 13px; height: 148px;"><!-- space --></td>
<td style="width: 135px; height: 148px;" valign="top">
<img src="img/image-8.jpg" height="135" width="135" style="width: 135px; height: 135px; background: red;" alt="" />
</td>
</tr>
</table>
哪个输出这个
我的问题是,我可以有1到无限量的图片在我的画廊 因此,我的PHP编写起来有点复杂 这是我下面的尝试,但有点拙劣
<?php
$images = get_field('image_thumbnails');
if( $images ): ?>
<?php $count = 0; ?>
<table border="0" cellspacing="0" cellpadding="0" style="width: 579px;" align="center">
<tr>
<?php foreach( $images as $image ): $count++ ?>
<td style="width: 135px; height: 148px;" valign="top">
<a href="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" target="_blank"><img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" height="135" width="135" style="width: 135px; height: 135px; border: none;" /></a>
</td>
<?php if ($count %4 == 0) { ?>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" style="width: 579px;" align="center">
<tr>
<?php } else { ?>
<td style="width: 13px; height: 148px;"><!-- space --></td>
<?php } ?>
<?php endforeach; ?>
</tr>
</table>
<?php endif;
?>
请使用以下代码查看我的测试: 4张图像 我得到了这个错误:“tr”的结束标记,它还没有完成 3张图片 没有错误,但看起来很奇怪,因为它空格了 6张图片 没有错误,但看起来很奇怪,因为它空格了 1张图片 有效并向左对齐。。。奇怪
我的问题是,任何人都可以帮助我实现一个比我更好的PHP循环,并且不会在任何给定的图像计数中留下任何语法错误吗
使用:
$images=get_字段('image_缩略图');
如果($images){
$count=计数($images);
对于($i=0;$i<$count%4;$i++){
//对齐阵列
array_push($images,array());
}
$rows=array_chunk($images,4);
?>
试试这个解决方案,。。
我并不是在写全部代码,而是为您提供了一些逻辑来实现所需的布局
<?php
$count = ceil(mysql_num_rows($query)); //number of tables
for($i=0;$i<$count;$i++)
{
?>
<table>
<tr>
<?php
$num = $j*4;
for($j=($num);$j<($num+3);$j++)
{
echo "<td><img src='".$image."'></td>"
}
</tr>
</table>
}
?>
谢谢Michael,这是完美无瑕的。一张表中保存了这么多好的内容。非常感谢,我将在我所有的html库中使用此方法。
<?php
$count = ceil(mysql_num_rows($query)); //number of tables
for($i=0;$i<$count;$i++)
{
?>
<table>
<tr>
<?php
$num = $j*4;
for($j=($num);$j<($num+3);$j++)
{
echo "<td><img src='".$image."'></td>"
}
</tr>
</table>
}
?>