如何在PHP/HTML中显示两列数组?
我有一系列的图标。现在,我们只在4桶中显示它们。因此,如果您有7个图标,第一张幻灯片上的第4个图标将重复第二张幻灯片上的第8个图标。这是因为数组的第三个索引存储在同一个位置。为了解决这个问题,我想在数组中循环,而不是一个图标一个图标地显式迭代如何在PHP/HTML中显示两列数组?,php,html,arrays,magento,Php,Html,Arrays,Magento,我有一系列的图标。现在,我们只在4桶中显示它们。因此,如果您有7个图标,第一张幻灯片上的第4个图标将重复第二张幻灯片上的第8个图标。这是因为数组的第三个索引存储在同一个位置。为了解决这个问题,我想在数组中循环,而不是一个图标一个图标地显式迭代 <?php if (!empty($icons)) { // if we have icons attributes // NOTE: we've changed it to show as ma
<?php if (!empty($icons)) { // if we have icons attributes
// NOTE: we've changed it to show as many sets as we can
// (sets of 4)
$number_of_sets = ceil(count($icons) / 4);
$k=0; // for inner iteration
for ($j=0;$j < $number_of_sets; $j++) {
$slide_total ++;
?>
<div class="cf slide icon-slide">
<?php
// up to 4 icons
if (is_array($icons) && !empty($icons[$k])) {
$icon1 = $icons[$k];
$k++;
}
?>
<div class="col left">
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon1['thumb']?>" alt="<?=htmlentities($icon1['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon1['post_title']?></h4>
<p><?=$icon1['post_content']?></p>
</div>
</div>
<?php
// up to 4 icons
if (is_array($icons) && !empty($icons[$k])) {
$icon2 = $icons[$k];
$k++;
}
?>
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon2['thumb']?>" alt="<?=htmlentities($icon2['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon2['post_title']?></h4>
<p><?=$icon2['post_content']?></p>
</div>
</div>
</div>
<?php
// up to 4 icons
if (is_array($icons) && !empty($icons[$k])) {
$icon3 = $icons[$k];
$k++;
}
?>
<div class="colR right">
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon3['thumb']?>" alt="<?=htmlentities($icon3['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon3['post_title']?></h4>
<p><?=$icon3['post_content']?></p>
</div>
</div>
<?php
// up to 4 icons
if (is_array($icons) && !empty($icons[$k])) {
$icon4 = $icons[$k];
$k++;
}
?>
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon4['thumb']?>" alt="<?=htmlentities($icon4['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon4['post_title']?></h4>
<p><?=$icon4['post_content']?></p>
</div>
</div>
</div>
</div> <!-- // end icon slide -->
<?php
} // end for $j (number of sets of 4 icons
?>
以下是一些循环逻辑:
$i = count( $icons );
if ( $i % 4 != 0 )
$i = $i + ( $i % 4 );
for( $n = 0; $n < $i; $n++ )
{
if ( $n % 4 == 0 )
{
// code to open a row here
}
// code to open a column here
// echo your icon here, use isset() or !empty().
// code to close a column here
if ( $n > 0 && $n % 3 == 0 )
{
// code to close a row here
}
}
$i=count($icons);
如果($i%4!=0)
$i=$i+($i%4);
对于($n=0;$n<$i;$n++)
{
如果($n%4==0)
{
//代码在此处打开一行
}
//在此处打开列的代码
//在此处回显图标,使用isset()或!empty()。
//在此处关闭列的代码
如果($n>0&&$n%3==0)
{
//代码在此处关闭一行
}
}
顶部的IFs用于保持列号一致。否则,循环的结尾会有一些奇怪的地方。在第二个if的条件下,您可能需要添加|124;$n==$i-1
以关闭最后一行。由于顶部的IFs,列号将始终为4,因此我们可以确定最后一行将被关闭。特别是对于表,不同的列号可能会在某些浏览器上呈现得很糟糕。没错,我错过了上面的部分。
echo $i++%$slide_items==0 ? '</div><div class="cf slide icon-slide">' : '';
$i = count( $icons );
if ( $i % 4 != 0 )
$i = $i + ( $i % 4 );
for( $n = 0; $n < $i; $n++ )
{
if ( $n % 4 == 0 )
{
// code to open a row here
}
// code to open a column here
// echo your icon here, use isset() or !empty().
// code to close a column here
if ( $n > 0 && $n % 3 == 0 )
{
// code to close a row here
}
}