2012-02-10 27 views
0

從來就得到如下:新錶行,如果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> 

我要的是,這兩個值都在一行。所以如果$ tmp是偶數,則應該開始一個新行。不幸的是,代碼沒有這樣做,每個值都代表了新的一行。 我該如何管理?

+0

你不知道這段代碼中發生了什麼,對吧? – anthares 2012-02-10 08:14:02

+0

在表格html中間回聲'$ tmp'會使它無效,所以瀏覽器如何呈現它是不可預知的。 – 2012-02-10 08:14:28

回答

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 2012-02-10 08:18:55

相關問題