2017-02-17 40 views
0

我想为CSS中的每个奇数行(#e5e8e8)和偶数行(#b9b8bb)添加背景色。如何在PHP中为ODD和EVEN行添加背景颜色?

for ($i = 2; $i <= $arrayCount; $i++) { 

     $_SESSION["a"] = trim($allDataInSheet[$i]["A"]); 

     $_SESSION["b"] = trim($allDataInSheet[$i]["B"]); 

     $_SESSION["c"] = trim($allDataInSheet[$i]["C"]); 

     $_SESSION["d"] = trim($allDataInSheet[$i]["D"]); 

     $_SESSION["e"] = trim($allDataInSheet[$i]["E"]); 

     $_SESSION["f"] = trim($allDataInSheet[$i]["F"]); 

     $_SESSION["g"] = trim($allDataInSheet[$i]["G"]); 


         echo '<!-- table content - start --> '; 
         echo ' <!-- ROW 1 -->'; 
         echo '<tr>'; 
          echo '<td width="10" style="border-bottom: 1px solid #ffffff;">'; 
           echo '<img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="10" height="1" style="display:block;border:none;"/>'; 
          echo ' </td>'; 

          echo '<td width="164" style="border-bottom: 1px solid #ffffff;" valign="middle">'; 
           echo '<table width="164" border="0" cellspacing="0" cellpadding="0">'; 
            echo '<tr>'; 
             echo '<td height="15" style="font-size:0px;line-height:0px;"><img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="1" height="15" style="display:block;border:none;"/></td>'; 
            echo ' </tr>'; 
            echo '<tr>'; 

             echo '<td width="114" valign="middle" align="left">'; 
              echo '<font style="font-family: HP Simplified, arial, sans-serif; font-size:15px;color:#000000;font-weight:normal; line-height:16px;">'; 

               echo $_SESSION["a"]; 
              echo '</font>'; 
             echo ' </td>'; 

             echo ' <td width="50" valign="middle" align="right">'; 

             echo '</td>'; 
            echo '</tr>'; 
            echo '<tr>'; 
             echo '<td height="15" style="font-size:0px;line-height:0px;"><img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="1" height="15" style="display:block;border:none;"/></td>'; 
            echo ' </tr>'; 
           echo '</table>'; 
          echo '</td>'; 

          echo ' <td width="10" style="border-right: 1px solid #ffffff;border-bottom: 1px solid #ffffff;">'; 
           echo '<img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="10" height="1" style="display:block;border:none;"/>'; 
          echo '</td>'; 
          echo '<td width="10" style="border-bottom: 1px solid #ffffff;">'; 
           echo '<img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="10" height="1" style="display:block;border:none;"/>'; 
          echo '</td>'; 
          echo '<td width="164" style="border-bottom: 1px solid #ffffff;" valign="middle">'; 
           echo '<table width="164" border="0" cellspacing="0" cellpadding="0">'; 
            echo '<tr>'; 
             echo '<td height="15" style="font-size:0px;line-height:0px;"><img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="1" height="15" style="display:block;border:none;"/></td>'; 
            echo ' </tr>'; 
            echo '<tr>'; 
             echo '<td width="114" valign="middle" align="left">'; 
              echo '<font style="font-family: HP Simplified, arial, sans-serif; font-size:15px;color:#000000;font-weight:normal; line-height:16px;">'; 
               echo $_SESSION["b"]; 

              echo '</font>'; 
             echo '</td>'; 
             echo '<td width="50" valign="middle" align="right">'; 
              echo '<font style="font-family: HP Simplified, arial, sans-serif; font-size:15px;color:#000000;font-weight:normal; line-height:16px;">'; 
               echo $_SESSION["c"]; 
              echo '</font>'; 
             echo '</td>'; 
            echo ' </tr>'; 
            echo '<tr>'; 
             echo '<td height="15" style="font-size:0px;line-height:0px;"><img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="1" height="15" style="display:block;border:none;"/></td>'; 
            echo '</tr>'; 
           echo '</table>'; 
          echo '</td>'; 
          echo '<td width="10" style="border-right: 1px solid #ffffff;border-bottom: 1px solid #ffffff;">'; 
           echo '<img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="10" height="1" style="display:block;border:none;"/>'; 
          echo '</td>'; 
          echo '<td width="10" style="border-bottom: 1px solid #ffffff;">'; 
           echo '<img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="10" height="1" style="display:block;border:none;"/>'; 
          echo '</td>'; 
          echo '<td width="164" style="border-bottom: 1px solid #ffffff;" valign="middle">'; 
           echo '<table width="164" border="0" cellspacing="0" cellpadding="0">'; 
            echo '<tr>'; 
             echo '<td height="15" style="font-size:0px;line-height:0px;"><img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="1" height="15" style="display:block;border:none;"/></td>'; 
            echo ' </tr>'; 
            echo '<tr>'; 
             echo '<td width="114" valign="middle" align="left">'; 
              echo '<font style="font-family: HP Simplified, arial, sans-serif; font-size:15px;color:#000000;font-weight:normal; line-height:16px;">'; 
              echo $_SESSION["d"]; 

              echo '</font>'; 
             echo '</td>'; 
             echo '<td width="50" valign="middle" align="right">'; 
              echo '<font style="font-family: HP Simplified, arial, sans-serif; font-size:15px;color:#000000;font-weight:normal; line-height:16px;">'; 
              echo $_SESSION["e"]; 
              echo '</font>'; 
             echo '</td>'; 
            echo '</tr>'; 
            echo '<tr>'; 
             echo '<td height="15" style="font-size:0px;line-height:0px;"><img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="1" height="15" style="display:block;border:none;"/></td>'; 
            echo '</tr>'; 
           echo '</table>'; 
          echo '</td>'; 
          echo '<td width="10" style="border-right: 1px solid #ffffff;border-bottom: 1px solid #ffffff;">'; 
           echo '<img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="10" height="1" style="display:block;border:none;"/>'; 
          echo '</td>'; 

          echo '<td width="10" style="border-bottom: 1px solid #ffffff;">'; 
           echo '<img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="10" height="1" style="display:block;border:none;"/>'; 
          echo '</td>'; 
          echo '<td width="164" style="border-bottom: 1px solid #ffffff;" valign="middle">'; 
           echo '<table width="164" border="0" cellspacing="0" cellpadding="0">'; 
            echo '<tr>'; 
             echo '<td height="15" style="font-size:0px;line-height:0px;"><img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="1" height="15" style="display:block;border:none;"/></td>'; 
            echo '</tr>'; 
            echo '<tr>'; 
             echo '<td width="114" valign="middle" align="left">'; 
              echo '<font style="font-family: HP Simplified, arial, sans-serif; font-size:15px;color:#000000;font-weight:normal; line-height:16px;">'; 
              echo $_SESSION["f"]; 

              echo '</font>'; 
             echo ' </td>'; 
             echo '<td width="50" valign="middle" align="right">'; 
              echo '<font style="font-family: HP Simplified, arial, sans-serif; font-size:15px;color:#000000;font-weight:normal; line-height:16px;">'; 
               echo $_SESSION["g"]; 
              echo ' </font>'; 
             echo '</td>'; 
            echo ' </tr>'; 
            echo ' <tr>'; 
             echo '<td height="15" style="font-size:0px;line-height:0px;"><img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="1" height="15" style="display:block;border:none;"/></td>'; 
            echo ' </tr>'; 
           echo '</table>'; 
          echo ' </td>'; 
          echo '<td width="10" style="border-bottom: 1px solid #ffffff;">'; 
           echo '<img src="http://ebiz.pmgasia.com/web/hp/HP_ProTrain_Monthly_eDM/ID/2017/February/images/x.gif" width="10" height="1" style="display:block;border:none;"/>'; 
          echo '</td>'; 

         echo '</tr>'; 

         echo '<!-- ROW 1 - END -->'; 


    } 

这里我想补充的背景颜色为每个奇数行(#e5e8e8)和偶数行(#b9b8bb)在PHP动态地此行我里面写了loop.My上面给出的表的代码创建对于电子邮件模板。因此它有很多和s.so ODD和EVEN选项不能正常工作。任何建议。预先感谢

+0

哪里是你的代码? – Option

+0

我刚刚编辑我的帖子..请提前帮助。谢谢 –

+0

Crikey!为什么这么多'回声'? – Option

回答

1

因此,不钻研你的代码来纠正它这里是一个通用的例子,因为它是一个CSS问题实际上..所以要做到这一点,你需要使用nth-child()。这里有一个例子:

tr:nth-child(odd) \t \t { background-color:#e5e8e8; } 
 
tr:nth-child(even) \t \t { background-color:#b9b8bb; }
<table> 
 
    <tr> 
 
     <td> Row 1</td> 
 
    </tr> 
 
    <tr> 
 
     <td> 
 
      Row 2 
 
     </td> 
 
    </tr> 
 
</table>

+0

如何在PHP中使用以下代码:tr:nth-​​child(odd)\t \t {background - 颜色:#e5e8e8; } tr:nth-​​child(偶数)\t \t {background-color:#b9b8bb; } –

+0

我上面给出的表格代码是为电子邮件模板创建的,所以它有很多和​​s.so ODD和EVEN选项不能正常工作。任何建议。预先感谢 –

+0

其作品不错。谢谢各位的支持 –