列具有特定名称时的格式,

时间:2014-10-20 09:03:48

标签: php

roblem是有评论员的地方 当列具有INTER名称或OUTER名称时,我需要格式化$ cell的内容 $ cell = number_format($ cell,2,',','。') 我刚刚开始使用php,所以不要太具体了,谢谢

<?php  
  // printing table rows  
  $rigapadi = 1 ;  
  while($row = mysql_fetch_row($result))  
  {  
      echo "<tr>";  
      $rigapadi=$rigapadi+1;    
      // $row is array... foreach( .. ) puts every element    
      // of $row to $cell variable    
      foreach($row as $cell)    
         if ($rigapadi % 2 == 0) {    
             # if column name = 'INTER' or 'OUTER' $cell = number_format($cell, 2, ',', '.');  
             echo "<td align=\"center\">$cell</td>";  
          } else {  
             echo "<td bgcolor=\"#E9EEF5\" align=\"center\">$cell</td>";  
          }   
       echo "</tr>\n";  
    }  
    mysql_free_result($result);  
    echo "</table>";  
    echo "<p/>";   
?>  

1 个答案:

答案 0 :(得分:0)

mysql_是偶像。你不能使用它们。请求帮助时请妥善缩进代码。

<?php
$rigapadi = 1;  
while($row = mysql_fetch_assoc($result)) {  
  echo '<tr>';
  $rigapadi++; 
  foreach($row as $name => $cell) {
    if($rigapadi % 2 === 0) {
      if($name === 'INTER' || $name === 'OUTER') {
        echo '<td align="center">' . number_format($cell, 2, ',', '.') . '</td>';
      } else {
        echo '<td align="center">' . $cell . '</td>';
      }
    } else {  
      echo '<td align="center" bgcolor="#E9EEF5">' . $cell . '</td>';
    }
  }
  echo "</tr>\n";
}
mysql_free_result($result);
echo "</table>";
?>

请注意,我使用fetch_assoc来获取$ name。

PS:<p/>不存在并且对齐和bgcolor而不是valids,但如果这是电子邮件的HTML代码,则可以接受。