2013-03-12 62 views
0

我希望對齊我的文本,以便當我發送消息時,它顯示在左側,但它的回覆,文本顯示在右側......我不知道是否使用「文本 - 對齊:左「,位置或對齊。這裏是我的代碼php中的文本對齊方式

if ($row['username'] == $username) 
{ 
    $color = 'blue'; 
    $align:left; // dont know if this is right 

} 
else 
{ 
    $color = 'red'; 
    $align:right; // dont know if this is right 
} 


echo '<i><p style="font-family:arial;color:'.$color.';font-size:15px;"> <strong>' . $row['username']. '</strong>: ' . $mymessage.'</i></p>'; 

} 
+0

嘗試使用浮動:正確的或浮動:左 – Growler 2013-03-12 18:12:26

+0

'$ align:left進行;'是不正確的PHP語句 – 2013-03-12 18:14:59

+0

或產生特殊類在PHP代碼元素,並在CSS定義這個類。 – ZloyPotroh 2013-03-12 18:15:00

回答

4

設置它與設置顏色的方式完全相同。

例如:

$align='right'; 

echo '<i><p style="font-family:arial;color:'.$color.';text-align:'.$align.';font-size:15px;"> <strong>' . $row['username']. '</strong>: ' . $mymessage.'</i></p>'; 
0

這應該工作:

if ($row['username'] == $username) 
{ 
    $color = 'blue'; 
    $align = 'left'; 

} 
else 
{ 
    $color = 'red'; 
    $align = 'right'; 
} 


echo '<i><p style="text-align:' . $align . ';font-family:arial;color:'.$color.';font-size:15px;"> <strong>' . $row['username']. '</strong>: ' . $mymessage.'</i></p>'; 
0

只需使用:

if ($row['username'] == $username) 

echo '<i><p style="font-family:arial;color:blue;float:left;font-size:15px;"> <strong>' . $row['username']. '</strong>: ' . $mymessage.'</i></p>'; 

else 
echo '<i><p style="font-family:arial;color:red;float:right;font-size:15px;"> <strong>' . $row['username']. '</strong>: ' . $mymessage.'</i></p>'; 
+0

text:align set left or right是另一種選擇。 – user2156064 2013-03-12 18:17:12

0

會更好,如果你使用的CSS類爲。

例如,你可以有一個類message和類reply

<style> 

.message { 
    color: blue; 
    text-align: left; 
} 

.reply { 
    color: red; 
    text-align: right; 
} 

</style> 

然後在PHP:

if ($row['username'] == $username) 
{ 
    $class = "message"; 
} 
else 
{ 
    $class = "reply"; 
} 


echo "<p class='$class'> 
    <strong>{$row['username']}</strong>: $mymessage 
</p>'; 

你的代碼看起來方式更乾淨。

1

試試這個,

$css = ($row['username'] == $username) ? 'color:blue;text-align:left;' : 'color:red;text-align:right;'; 

echo '<i><p style="font-family:arial;font-size:15px;'.$css.'"> <strong>' . $row['username']. '</strong>: ' . $mymessage.'</i></p>';