如何使 PHP html 表的行之间颜色不匹配?

发布于 2024-11-28 13:54:35 字数 1040 浏览 1 评论 0 原文

我一边编码一边学习 PHP。 w3schools 上的一个示例显示了使用 PHP 和 msql 在 html 表上显示数据库结果。我的问题是,我现在有太多行,我无法使它们在行之间具有不匹配的颜色。我尝试在 之后添加样式跨度和字体颜色,但它不接受。如果我这样做的话整个 PHP 就无法工作。

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

上面代码的输出将是:

Firstname   Lastname
Glenn   Quagmire
Peter   Griffin

http://www.w3schools.com/php/php_mysql_select .asp

I am learning PHP as I code. From an example on w3schools it showed using PHP and msql to display database results on a html table. My questions is, I now have too many rows and I could not make them have mismatch colors between rows. I've tried adding style span and font color after <td but it doesn't take it. the entire PHP just don't work if I do so.

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

The output of the code above will be:

Firstname   Lastname
Glenn   Quagmire
Peter   Griffin

http://www.w3schools.com/php/php_mysql_select.asp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

明明#如月 2024-12-05 13:54:35

不完全确定您所说的颜色不匹配是什么意思。假设您的意思是交替行颜色,我会执行以下操作:

$odd = false;

while (...)
{
    echo '<tr class="'.($odd ? "odd" : "even").'">';
    ...
    echo "</tr>";
    $odd = !$odd;
}

现在您的 tr 元素属于 oddeven 类 交替使用,并且可以在 CSS 中为其中之一指定一些额外的背景颜色,例如:

tr.odd { background-color: rgba(0, 0, 0, 0.05); }

Not entirely sure what you mean by color mismatch. Assuming that you mean alternating row colors I'd do the following:

$odd = false;

while (...)
{
    echo '<tr class="'.($odd ? "odd" : "even").'">';
    ...
    echo "</tr>";
    $odd = !$odd;
}

Now you have the tr element being of class odd or even alternatingly, and may specify some additional background color for one of them in your CSS, e.g.:

tr.odd { background-color: rgba(0, 0, 0, 0.05); }
柳若烟 2024-12-05 13:54:35

将此: 替换

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }

为:

$i = 0;
while($row = mysql_fetch_array($result))
  {
  echo "<tr ". ($i % 2 == 0 ? 'style="background-color:grey;"' : '' .">";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  $i++;
  }

每隔一行将具有灰色。

Replace this:

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }

with this:

$i = 0;
while($row = mysql_fetch_array($result))
  {
  echo "<tr ". ($i % 2 == 0 ? 'style="background-color:grey;"' : '' .">";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  $i++;
  }

Every other row will have grey color.

悲念泪 2024-12-05 13:54:35

也许您可以将这种方法与 jquery 一起使用

<script src="text/javascript">
    $('#table tbody tr:odd').addClass('odd');
    $('#table tbody tr:even').addClass('even');
</script>

,然后添加样式

.odd { background-color: #color }
.even { background-color: #color }

Maybe you can use this approach with jquery

<script src="text/javascript">
    $('#table tbody tr:odd').addClass('odd');
    $('#table tbody tr:even').addClass('even');
</script>

and then add the styles

.odd { background-color: #color }
.even { background-color: #color }
残疾 2024-12-05 13:54:35
$class = "even";    
while($row = mysql_fetch_array($result))
{
  if($class == "even")
  {
    echo "<tr class='$class'>";
    $class = "odd";
  }
  else
  {
    echo "<tr class='$class'>";
    $class = "even";
  }

  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
}

CSS

tr.even
{
    background-color:blue;//Pick your own color
}

tr.odd
{
    background-color:green;
}

这里是颜色名称的列表。如果您想要更详细的颜色选择,请点击此处

$class = "even";    
while($row = mysql_fetch_array($result))
{
  if($class == "even")
  {
    echo "<tr class='$class'>";
    $class = "odd";
  }
  else
  {
    echo "<tr class='$class'>";
    $class = "even";
  }

  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
}

CSS

tr.even
{
    background-color:blue;//Pick your own color
}

tr.odd
{
    background-color:green;
}

Here is a list of color names. If you want a more detailed color choices, click here.

春夜浅 2024-12-05 13:54:35

使用

  $flag = 0;
  while($row = mysql_fetch_array($result))
  {
  if ($flag%2 == 1)
  echo "<tr bgcolor=#123345>";
  else
  echo echo "<tr bgcolor=#643235>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  $flag = $flag +1;
  }

Use

  $flag = 0;
  while($row = mysql_fetch_array($result))
  {
  if ($flag%2 == 1)
  echo "<tr bgcolor=#123345>";
  else
  echo echo "<tr bgcolor=#643235>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  $flag = $flag +1;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文