我有以下内容:
<table border="1">
<?php
$i = 0;
$tmp = 1;
foreach ($recip['Data']['Recipes'] as $key => $recipe) {
$tmp = $i % 2;
echo $tmp;
if($tmp == 0) {
echo '<tr>';
}
echo '<td>
<a href="/recipe_search.php?id=' . $recipe['ID'] . '">';
echo $recipe['TITLE'];
echo '</a> </td>';
if($tmp == 0){
echo '</tr>';
}
$i = $i + 1;
}
?>
</table>
我想要的是,两个值在一行中。因此,如果$tmp
为偶数,则应启动新行。不幸的是,代码不会这样做,每个值都代表一个新行。
我该如何管理?
答案 0 :(得分:3)
<table border="1">
<?php
$i = 0;
foreach ($recip['Data']['Recipes'] as $key => $recipe) {
if($i % 2 == 0) {
echo '<tr>';
}
echo '<td><a href="/recipe_search.php?id=' . $recipe['ID'] . '">';
echo $recipe['TITLE'];
echo '</a> </td>';
if(($i+1) % 2 == 0){
echo '</tr>';
}
$i++;
}
// if there is an odd number of entries, the last one will include only one recipie.
// but we must still echo </tr>
if(count($recip['Data']['Recipes']) % 2 != 0){
echo '</tr>';
}
?>
</table>