2014-10-05 27 views
0

我需要打印一些數據到html列表中。如果我在其他再次聲明一個變量,先前宣佈如果只打印從其他變量的值......從別的變量重新在其他內部打印總是相同的

for ($i = 1; $i < 13; $i++) { 
    $month = 'month' . $i; 
    if($row[$month] == 1) { 
     $paid[] = 'Pagado'; 
     $bonus = $row['bonus']; 
     //$cashed = 
    } else { 
     $paid[] = 'No Pagado'; 
     $bonus = '0'; // $bonus will print always this, even if the if is true. 
     $cashed = 'No'; 
    } 
} 
//Now make the HTML list 

foreach($monthNames as $key => $month) { 
    echo ' 
      <div class="list"> 
       <ul> 
        <li><a class="month">' . $month . '</a></li> 
        <li><a class="status">' . $paid[$key] .'</a></li> 
        <li><a class="bonus">' . $bonus . '</a></li> 
        <li><a class="cashed">' . $cashed . '</a></li> 
       </ul> 
      </div>'; 
} 

打印$的獎金應該是唯一的,如果其他人在執行,但是當如果是真的應該打印數據庫列數據而不是重新聲明的值。

我看不到任何錯誤,爲什麼總是打印0而不是$ row ['bonus'];當$ row [$ month] == 1?

謝謝!

+0

也許你$行[ '獎金']等於爲'0' – webduvet 2014-10-05 19:04:22

+0

你試過'var_dump($ row);'? – Machavity 2014-10-05 19:08:42

+0

嗯,如果我評論其他$獎金將打印分貝數據:列紅利是5.爲什麼如果我重新聲明$獎金否則只會打印?其他變量(如$兌現)的情況也一樣。如果我在其他地方重新聲明一個變量,html列表將只顯示重新聲明的變量(來自else)。 – 2014-10-05 19:09:54

回答

3

如果您在for循環後打印$ bonus,則變量$ bonus將僅取決於$ row ['month12'],因爲它在循環結束時被保存。

如果你想存儲每個月的$獎勵狀態,你應該將它保存到數組(如$ paid [])。

for ($i = 1; $i < 13; $i++) { 
    $month = 'month' . $i; 
    if($row[$month] == 1) { 
     $paid[] = 'Pagado'; 
     $bonus[] = $row['bonus']; 
     //$cashed = 
    } else { 
     $paid[] = 'No Pagado'; 
     $bonus[] = '0'; // $bonus will print always this, even if the if is true. 
     $cashed[] = 'No'; 
    } 
} 

然後你將有$獎勵數組,每個月會保持「獎金」狀態。 $加成[0]將有獎金地位的第1'(一月),$紅利[1]二月等等

HTML列表:

foreach($monthNames as $key => $month) { 
    echo ' 
      <div class="list"> 
       <ul> 
        <li><a class="month">' . $month . '</a></li> 
        <li><a class="status">' . $paid[$key] .'</a></li> 
        <li><a class="bonus">' . $bonus[$key] . '</a></li> 
        <li><a class="cashed">' . $cashed[$key] . '</a></li> 
       </ul> 
      </div>'; 
} 
+0

非常感謝你:) – 2014-10-05 19:37:50

相關問題