2016-11-29 51 views
-2

我试图通过回显它们将两个类添加到td。他们每次都包含一个类,但其他类可以改变。我跳上刚刚拥有所有类的数组中的如下功能...如何将两个值添加到类属性?

$StandClassArray = array('north stand', 'east stand', 'south stand', 'west stand'); 

注意,这些应该是两个单独的类,一个被称为“立”,一个被称为“北”或'south'等。所以td都需要'站'级和4个指南针之一。

当我加入这个我td以下....

$Side = 0 ; // This would have been passed to the function usually. 
echo "<td class = $StandClassArray[$Side]>Text</td>"; 

我在浏览器中得到什么......

<td class = "north" stand = "">Text</td> 

我试着做其他的方法,如...

echo "<td class = $StandClassArray[$Side] stand>Text</td>"; // Just the compass point in the array for this. 

但它给出了相同的结果。

我很确定我以前曾经遇到过这个问题,但不记得如何解决它。

+4

你需要用引号括您的HTML属性的值,特别是如果值包含一个空格。 –

回答

1

为什么不回显值:

<td class="<?php echo $StandClassArray[$Side] ?>">Text</td> 

另一种方法可能是(按照你的逻辑):

echo "<td class ='".$StandClassArray[$Side]."'>Text</td>"; 
+0

啊我看到了,我的最终输出中没有引号。修正了,谢谢。 – Farflame

3

的实际输出到浏览器是:

<td class = north stand>Text</td> 

哪些不是有效的标记。浏览器正试图尽可能为您纠正它。只是更明确一些输出,包括引号,你希望他们包括:

echo "<td class = \"$StandClassArray[$Side]\">Text</td>"; 

应该输出:

<td class = "north stand">Text</td> 
+0

谢谢,我现在看到,最终字符串中没有任何引号。卫生署。 – Farflame