2014-08-28 14 views
-3

我在HTML tr標記裏面使用了echo,我收到一個錯誤。如何在html表格行中使用echo?

這裏是我的代碼

的index.php

<?php 
$i=0; 
    while($row=mysql_fetch_array($ros)) 
    { 
    if($i%2==0) 
$classname="evenRow"; 
else 
$classname="oddRow"; 
echo '<tr class="id" >'; 
echo '<tr class="'echo $classname'">'; 
?> 

我收到以下錯誤:

Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in E:\xampp\htdocs\pagination\index.php on line 64

我要去哪裏錯了,我如何能實現我的期望輸出?

在此先感謝

+2

回聲內回聲?! – arunrc 2014-08-28 05:51:35

+0

我試圖用一個回聲沒有工作 – 2014-08-28 05:52:30

+0

檢查答案... – arunrc 2014-08-28 05:52:47

回答

1

問題不是你在一個錶行內,而是你在一個PHP字符串內,答案是:你不這樣做。

您:

  • 插值您的變量
  • 串連您的變量
  • 不要使用回聲和外部輸出

這樣的字符串:

<?php 
$i=0; 
while($row=mysql_fetch_array($ros)) { 
    if($i%2==0) { 
     $classname="evenRow"; 
    } else { 
     $classname="oddRow"; 
?> 
<tr class="id"> 
    <tr class="<?php echo $classname; ?>"> 
<?php 
    } 
# ... 

注:你似乎試圖嵌套表格行,這是不允許的。

您可以免除樣式表中的奇數/偶數類名稱和use :nth-child(odd) and :nth-child(even)

+0

我的classname變量不起作用 – 2014-08-28 06:01:04

0

更改這個

<?php 
$i=0; 
    while($row=mysql_fetch_array($ros)) 
    { 
    if($i%2==0) 
$classname="evenRow"; 
else 
$classname="oddRow"; 
echo '<tr class="id" >'; 
echo '<tr class="'.$classname.'">'; 
?> 
+1

'$ i ++'缺失。 – nn2 2014-08-28 05:56:25

2

只是這樣做。不要回聲兩次!

echo '<tr class=" '. $classname .' ">'; 
-1

不能使用echo內的echo

echo '<tr class="'echo $classname'">'; 

使用方法如下

echo '<tr class="'.$classname.'">'; 
+0

錯誤..這是串聯錯誤隊友! :) – NoobEditor 2014-08-28 06:04:17

0
<?php 
$i=0; 
while($row=mysql_fetch_array($ros)) 
{ 
    if($i%2==0) 
     $classname="evenRow"; 
    else 
     $classname="oddRow"; 

    echo "<tr class='id'>"; 
    echo "<tr class=".$classname.">"; 
} 
?>