2014-05-25 60 views
1

我可以使用if語句爲css嗎?如果其他人爲css

這就是我想要的文本的顏色改變:

<?php echo $status; ?> 

將有2狀態:待&交付 等待將是紅色和交付將是綠色的

我可以像做(對CSS):

.pending {text-decoration:underline; color:red;} 
.delivered {text-decoration:underline; color:green;} 

如果else語句:

if ($status==delivered) 
{ 
    //this is where i don't know what to do and code 
} 
else 
{ 
    //and here 
} 

我應該放什麼?或者其他解決方案?

+0

如果您發現任何答案有幫助,請接受它,然後單擊樂譜下方的勾號圖標。 –

回答

1

使用php/javascript /任何其他語言輸出html,並將類分配給所需的任何元素。

純PHP例子:

<?php 

if(true) { 

echo '<div class="pending">content</div>'; 

} else { 

echo '<div class="delivered">content</div>'; 

} 

?> 

使用變量(PHP + HTML)的另一種方法:

<?php 

if(true) { 

$status = 'pending'; 

} else { 

$status = 'delivered'; 

} 

?> 

<html> 
<head> 
</head> 
<body> 
<div class="<?php echo $status; ?>">content</div> 
</body> 
</html> 
1

如果在PHP中$status變量實際上符合您的類名稱,只是用它在你的PHP當顯示任何東西是被稱呼的時候:

eg如果$status == 'pending',則:

<div class="<?= $status ?>">...</div> 

將呈現

<div class="pending">...</div> 

和符合您.pending規則。