解决Foreach行

时间:2013-11-17 21:12:29

标签: php foreach

我正在寻找有关如何在表行中显示foreach循环数据的帮​​助。下面的代码将数据放在屏幕上运行的单行中。我想每排三件。我目前总共有大约80个项目用于展示,并且每个月都会添加更多项目,所以显示4行3项后还可以帮助分页的任何人的奖励积分......仅供参考,我尝试过类似问题的答案变体问但是我找不到适合我的解决方案,或者我错误地实施了它。任何帮助将不胜感激。谢谢!

<table>
<tr>
<?php foreach (get_terms('term') as $cat) : ?>  
<td>
<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><strong><?php echo $cat->name; ?></strong></a><br>

<em><a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><?php echo $cat->description; ?></a></em><br>

<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><img src="<?php echo taxonomy_image_url($cat->term_id); ?>" /></a>
</td>
<?php endforeach; ?>
</tr>
</table> 

这是Tecbrat代码产生的输出:

<table>

<tr>
<td>
<a href="http://comm.org/mag/august-2013/"><strong>August 2013</strong></a><br>

<em><a href="http://comm.org/mag/august-2013/">Issue 101, Vol. 1</a></em><br>

<a href="http://comm.org/mag/august-2013/"><img src="http://comm.org/wp-content/uploads/FC-OCT.jpg" /></a>
</td>
</tr>

<tr>
<td>
<a href="http://comm.org/mag/february-2012/"><strong>February 2012</strong></a><br>

<em><a href="http://comm.org/mag/february-2012/">Issue 101, Vol. 1</a></em><br>

<a href="http://comm.org/mag/february-2012/"><img src="http://comm.org/wp-content/uploads/FC-OCT.jpg" /></a>
</td>
</tr>

<tr>
<td>
<a href="http://comm.org/mag/february-2013/"><strong>February 2013</strong></a><br>

<em><a href="http://comm.org/mag/february-2013/">Issue 101, Vol. 1</a></em><br>

<a href="http://comm.org/mag/february-2013/"><img src="http://comm.org/wp-content/uploads/FC-OCT.jpg" /></a>
</td>
</tr>

<tr>
<td>
<a href="http://comm.org/mag/january-2012/"><strong>January 2012</strong></a><br>

<em><a href="http://comm.org/mag/january-2012/">Issue 101, Vol. 1</a></em><br>

<a href="http://comm.org/mag/january-2012/"><img src="http://comm.org/wp-content/uploads/FC-OCT.jpg" /></a>
</td>
</tr>

<tr>
<td>
<a href="http://comm.org/mag/january-2013/"><strong>January 2013</strong></a><br>

<em><a href="http://comm.org/mag/january-2013/">Issue 101, Vol. 1</a></em><br>

<a href="http://comm.org/mag/january-2013/"><img src="http://comm.org/wp-content/uploads/FC-OCT.jpg" /></a>
</td>
</tr>

<tr>
<td>
<a href="http://comm.org/mag/november-2013/"><strong>November 2013</strong></a><br>

<em><a href="http://comm.org/mag/november-2013/">Issue 101, Vol. 1</a></em><br>

<a href="http://comm.org/mag/november-2013/"><img src="http://comm.org/wp-content/uploads/FC-OCT.jpg" /></a>
</td>
</tr>

<tr>
<td>
<a href="http://comm.org/mag/october-2013/"><strong>October 2013</strong></a><br>

<em><a href="http://comm.org/mag/october-2013/">Issue 101, Vol. 1</a></em><br>

<a href="http://comm.org/mag/october-2013/"><img src="http://comm.org/wp-content/uploads/FC-OCT.jpg" /></a>
</td>
</tr>

<tr>
<td>
<a href="http://comm.org/mag/september-2013/"><strong>September 2013</strong></a><br>

<em><a href="http://comm.org/mag/september-2013/">Issue 101, Vol. 1</a></em><br>

<a href="http://comm.org/mag/september-2013/"><img src="http://comm.org/wp-content/uploads/FC-OCT.jpg" /></a>
</td>
</tr>
</table> 

我能够使用此标记使其工作:

<table>
<tr>
<?php $col = 0; ?>
<?php foreach (get_terms('term') as $cat) : ?>

<?php if ($col > 0 && $col % 3 == 0): ?>
</tr>
<tr>
<?php endif; ?>
<?php $col++; ?>

<td>

<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><strong><?php echo $cat->name; ?></strong></a><br>

<em><a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><?php echo $cat->description; ?></a></em><br>

<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>
</td>
<?php endforeach; ?>
</tr>
</table>

现在我需要了解如何为每11个项添加分页,以便第12页上的数字12-22,依此类推。

2 个答案:

答案 0 :(得分:0)

未经测试,但试试这个。我刚把你的TR移到了foreach里。

<table>

<?php foreach (get_terms('term') as $cat) : ?>  
<tr>
<td>
<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><strong><?php echo $cat->name; ?></strong></a><br>

<em><a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><?php echo $cat->description; ?></a></em><br>

<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><img src="<?php echo taxonomy_image_url($cat->term_id); ?>" /></a>
</td>
</tr>
<?php endforeach; ?>
</table> 

答案 1 :(得分:0)

适用于我的问题的代码是:

<table>
<tr>
<?php $col = 0; ?>
<?php foreach (get_terms('term') as $cat) : ?>

<?php if ($col > 0 && $col % 3 == 0): ?>
</tr>
<tr>
<?php endif; ?>
<?php $col++; ?>

<td>

<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><strong><?php echo $cat->name; ?></strong></a><br>

<em><a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><?php echo $cat->description; ?></a></em><br>

<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>
</td>
<?php endforeach; ?>
</tr>
</table>