2012-02-10 2 views
0

I'veは、次のことを得た場合:新しい表の行VAR = 0

<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> 

私が欲しい二つの値が1行である、ということ、です。 $ tmpが偶数の場合は、新しい行を開始する必要があります。残念ながら、コードはそれをしません、すべての値は新しい行に立っています。 これをどうすれば管理できますか?

+0

あなたはこのスニペットで何が起こっているのか分かりません。 – anthares

+0

テーブルhtmlの途中で '$ tmp'を' echo 'すると無効になるので、ブラウザがレンダリングする方法は予測できません。 –

答えて

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> 
+0

素晴らしい、ありがとう...私は約7分でそれを受け入れます – user896692

関連する問題