2016-01-20 21 views
0

這是我的代碼:的if else循環PHP MySQL的

while($row = mysql_fetch_array($result)) 
{ 
    echo '<tr>'; 
    if($row['type'] == "product"){ 
    echo '<td>'.$row['item'].'<br>('.$row['price'].')</td>'; 
    }else{ 
    echo '<td>'.$row['item'].'</td>'; 
    } 
    echo '<td>'.$row['type'].'</td>'; 
    echo '</tr>';   
} 

我想是時候itemtype = product那麼價格將在該項目下顯示。

例子:

Item  |  type 

item1   product 
($10.00) 

item2   service 

item3   product 
($20.00) 
+0

什麼是你得到 –

+0

錯誤的位置是錯誤/問題? –

+0

輸出只會顯示所有的價格或全部沒有價格的項目... –

回答

0

使用此:

while ($row = mysql_fetch_array($result)) { 
    echo '<tr>'; 
    if ($row['type'] == "product") { 
     echo '<td>' . $row['item'] . '<br>' . $row['price'] . '</td>'; 
    } else { 
     echo '<td>' . $row['item'] . '</td>'; 
    } 
    echo '<td>' . $row['type'] . '</td>'; 
    echo '</tr>'; 
    } 
+0

與我的代碼有什麼不同? @@ –

+0

你的代碼包含php錯誤。不正確的連接 –

+0

......除了某些格式化之外,它與__kc低__的代碼相同。 – hherger

0

試試這個:

echo '<table>'; 
while($row = mysql_fetch_array($result)) 
{ 
    echo '<tr>'; 
    echo '<td>'.$row["item"].'</td><td>'.$row["type"].'</td>'; 
    echo '</tr>'; 

    if($row['type'] == "product") 
    { 
     echo '<tr><td colspan="2">('.$row["price"].')</td></tr>'; 
    } 
} 
echo '</table>';