2016-02-29 35 views
0
<table> 
<thead> 
<tr> 
<th>model</th> 
<th>a</th> 
<th>jumlah</th> 
</tr> 

    </thead> 
    <?php foreach((array)$query as $row): 
         if ($row->model='10') { 
          $jumlah=$row->a-10; 
         }else{ 
          $jumlah=$row->a-5; 
         };?> 
    <td><?php echo $row->model ?></td> 
    <td><?php echo $row->a ?></td> 
    <td><?php echo $jumlah ?></td> 

應該導致這樣爲什麼在我的代碼中如果還有其他重寫值?

enter image description here

但爲什麼是這樣的結果呢?

enter image description here

爲什麼coloumn模型超越控制10個數據? 如何解決?

+3

單個'='受讓人,''==檢查是否相等 – baao

+0

'='是賦值,你需要''==比較操作 –

+1

不同的語言,但完全相同的概念:http://stackoverflow.com/questions/2063480/the-3-different-equals –

回答

2

您使用賦值運算符(=)不是等號(= =)。改變這一行:

if ($row->model = '10') { 

這樣:

if ($row->model == '10') { 

或本:

if ($row->model == 10) { 

在第一種情況下,你告訴編譯器來存儲值10 進入變量$row->model

在我們正在檢查,以查看是否的 $row->model的值等於10。

字符串值。在我們正在檢查,以查看是否的 $row->model的值等於所述第三殼體的第二殼體數值爲10.

我的猜測是這樣的,你可能想使用第三種情況,除非數字在數據庫中存儲爲文本值。

1

出好心情的今天:

<?php foreach((array)$query as $row): 
        if ($row->model=='10') { # <- added double = sign 
         $jumlah=$row->a-10; 
        }else{ 
         $jumlah=$row->a-5; 
        };?> 
相關問題