简体   繁体   English

如何动态用背景色填充表格单元格?

[英]How can I dynamically fill a table-cell with background-color?

I've got a problem with a school schedule, wich is coded in php/html. 我的学校时间表有问题,这是用php / html编码的。 The lessons are filled in a html table (surprise!!) and a filled cell has to get a background color. 课程在html表中填写(惊讶!),并且已填充的单元格必须具有背景色。 The problem is, that the table is dynamically filled with data, so it's not possible to give the td-tag the background-color. 问题在于,表是动态填充数据的,因此无法为td标签赋予背景色。 Here is my code: 这是我的代码:

echo"<td id='cell' style='width:15%'>";
foreach($plan->result as $res){
   if($res->date == "20131007" && $item->startTime == $res->startTime){
      foreach ($res->kl as $kl){
         echo getKlasseById($ch, $kl->id).", ";
      }
      echo"<script type=\"text/javascript\">
      var td = document.getElementById('cell');
      td.style.backgroundColor='#FF8000';</script>";
   }
}

What this does is filling the fist cell of the table with the background-color and then nothing (concerning the color). 这样做是用背景颜色填充表格的第一个单元格,然后不填充任何内容(关于颜色)。

EDIT 编辑

Ok, I solved my problem. 好,我解决了我的问题。 As my programming prof would say, I tried to shoot pigeons with fusion bombs. 就像我的编程专家会说的那样,我试图用融合炸弹射击鸽子。 I just put a <div> around the foreach-loop with the desired background-color. 我只是将<div>放在具有所需背景颜色的foreach循环周围。 Anyway, thank you for your help. 无论如何,谢谢您的帮助。

Create a CSS class with the desired background color: 创建具有所需背景颜色的CSS类:

.colored { background-color: yourcolor; }

And, when you echo the <td> , add the class to it. 并且,当您回显<td> ,将类添加到其中。

Instead of rendering the td BEFORE the if, you may do that inside the if, if that's the main problem!.. 如果不是主要问题,可以在if内进行渲染,而不是在if之前渲染td。

echo "<td id=\"cell\" class=\"colored\">";

To be more clear, I mean that instead of this: 更清楚地说,我的意思是:

echo"<td id='cell' style='width:15%'>";
foreach($plan->result as $res){
   if($res->date == "20131007" && $item->startTime == $res->startTime){
      foreach ($res->kl as $kl){
         echo getKlasseById($ch, $kl->id).", ";
      }
      echo"<script type=\"text/javascript\">
      var td = document.getElementById('cell');
      td.style.backgroundColor='#FF8000';</script>";
   }
}

You may do this: 您可以这样做:

foreach($plan->result as $res){
   if($res->date == "20131007" && $item->startTime == $res->startTime){
      echo"<td id='cell' style='width:15%' class='colored'>";
      foreach ($res->kl as $kl){
         echo getKlasseById($ch, $kl->id).", ";
      }
   }
   else {
     echo"<td id='cell' style='width:15%'>";
     // Whatever goes into the else, if needed.
   }
}

Anyway.. You're not closing the <td> , are you? 无论如何..您没有关闭<td> ,对吗? :) :)

Why like that? 为什么这样? Why not use PHP instead. 为什么不使用PHP呢? All these samll Javascript elements only slow down the page, and makes the page slower to parse by browsers. 所有这些samll Javascript元素只会减慢页面速度,并使浏览器解析页面的速度变慢。 PHP+ some classes are way more efficient PHP +一些类更有效

foreach($plan->result as $res){
    $backgroundColor = "";
    if($res->date == "20131007" && $item->startTime == $res->startTime){
      foreach ($res->kl as $kl){
         echo getKlasseById($ch, $kl->id).", ";
     }
    $backgroundColor = "#FF8000";
   }
}
echo"<td id='cell' style='width:15%;background-color: ".$backgroundColor."'>";

Small sidenote: Your code is a bit unclear so I'm not sure wether I put the code in the proper order or with the proper statements, but you catch my drift 小旁注:您的代码有点不清楚,所以我不确定是否将代码按正确的顺序或正确的语句放置,但是您会发现我的漂移

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM