Ano*_*m.. 1 html php row html-table
我有一个 html 表,其中填充了 mysql 表中的数据。我使用以下代码:
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<tr>";
echo "</tr>\n";
while($row = mysql_fetch_row($result))
{
echo "<tr>";
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
Run Code Online (Sandbox Code Playgroud)
注意:我通常使用 PDO 语句,但在这个例子中我使用 mysql。
该代码正确生成表。问题是:我想将 CSS 应用到每隔一行,如下所示class=table_higlight。我怎样才能做到这一点?
使用:
$i = 0;
while($row = mysql_fetch_row($result)) {
if ($i % 2 == 0 )
echo '<tr class="even">';
else
echo '<tr class="odd">';
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
$i++
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6529 次 |
| 最近记录: |