php - 使用 php 从 db 检索输入并具有不同的 css 类

标签 php css arrays database

我有一个网站可以显示正在运行的游戏。他们在一个中,每场比赛都有一个th。我想让他们彼此不同。例如,如果游戏是 HS U11A,那么它应该是 th class=blue

当前脚本:

$sql = "SELECT kamp, kategori, raekke, tid, loebenr FROM turneringsinfo WHERE loebenr LIKE '1' ORDER BY loebenr ASC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<table>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<th>" . $row["kategori"]. ' ' . $row["raekke"]. "</th>";
     }
     echo "</table>";
} else {
     echo "0 results";
}

输出:

<table>
<th>HS U11 A</th>
<th>DS U13 B</th>
<th>HS U17 A</th>
<th>HS SEN A</th>
<th>HS SEN D</th>
<th>HS U11 B</th>
<th>HS U19 A</th>

想要的输出:

<table>
<th class="red">HS U11 A</th>
<th class="green">DS U13 B</th>
<th class="blue">HS U17 A</th>
<th>HS SEN A</th>
<th>HS SEN D</th>
<th>HS U11 B</th>
<th>HS U19 A</th>

在这种情况下,我认为可以提供帮助的发现

$setning  = "You should eat fruits, vegetables, and fiber every day.";
$gammeltord = array("fruits", "vegetables", "fiber");
$nyttord   = array("pizza", "beer", "ice cream");
$nysetning = str_replace($gammeltord, $nyttord, $setning);


echo "$nysetning"

该站点也已上线,因此您可以在此处查看视觉效果:http://turneringsportalen.no/beta/tidslinje.php

希望我已经解释清楚了,期待您的帮助:)

问候Øystein

最佳答案

我不确定这些游戏名称是如何生成的,或者它们可能是静态字符串,但由于您特别要求标记这些值:

$tags = [
  'HS U11 A' => 'red',
  'DS U13 B' => 'green',
  'HS U17 A' => 'blue'
];

$sql = "SELECT kamp, kategori, raekke, tid, loebenr FROM turneringsinfo WHERE loebenr LIKE '1' ORDER BY loebenr ASC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  echo "<table>";
  // output data of each row
  while($row = $result->fetch_assoc()) {
    if(array_key_exists(($name = $row["kategori"]. ' ' . $row["raekke"]), $tags)){
      echo "<th class='".$tags[$name]."'>" . $name. "</th>";
    } else {
      echo "<th>" . $name. "</th>";
    }
  }
  echo "</table>";
} else {
  echo "0 results";
}

这通过检查数组 $tags 的键是否存在(使用这些值设置一个新的 var $name)来工作,如果它存在则打印出来。

关于php - 使用 php 从 db 检索输入并具有不同的 css 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40754013/

相关文章:

html - 如何在表格标题下方添加边框底部或水平线

c++ - CUDA,使用2D和3D阵列

php - elasticsearch有可能吗?

php - 从数组 PHP 中获取数据

php - 适用于小批量银行软件的最佳数据库 - PHP

arrays - 如何在 flutter 中解析json?

javascript - 如何修复 React-Redux 中的 'First visit render data isn' 无法访问,但第二次访问后可以进行渲染错误

php - Apache 错误 : cannot load mod_access_compat. 所以

javascript - 选择元素时下拉标题更改

css - 删除 Safari 中的显示/隐藏密码图标