简体   繁体   English

当使用PHP循环创建的表行时,如何根据内容更改表格单元格的属性

[英]how to change attribute of table cell based on contents when table rows created by while loop with PHP

I am a beginner with PHP and only know the basics. 我是PHP的初学者,只知道基础知识。 I want to do my first more advanced thing, but been have not been able to figure out how to do it. 我想做我的第一个更高级的事情,但一直无法弄清楚如何做到这一点。

I fetch data from mysql database and then create an html table. 我从mysql数据库中获取数据,然后创建一个html表。

while($row = $queryresult->fetch_assoc()) {
echo "<tr><td>".$row['col1']."</td><td>".$row['col2']."</td></tr>";}

I want to add a css class to a cell based on its contents. 我想基于其内容向单元格添加css类。 So if col1 on line one is "foo" then I want to have <td class="foo"> and if col1 is "bar" on line 2 of the table then I want <td class="bar"> 因此,如果第一行的col1是“foo”,那么我想要<td class="foo">并且如果col1在表的第2行是“bar”那么我想要<td class="bar">

I tried 我试过了

if ($row['col1'] == "foo") {
        $classvar = " class=foo";
    } elseif ($row['col1'] == "bar") {
        $classvar = " class=bar";
    }
echo "<tr><td".$classvar.">";

But that sets all rows to the same thing based on the first line iterated. 但是,根据迭代的第一行,它将所有行设置为相同的行。 So if the first line has col1="foo" then $classvar = " class=foo" on all lines. 因此,如果第一行有col1 =“foo”,那么所有行上的$ classvar =“class = foo”。 How could I have each table row evaluated separately? 我怎么能单独评估每个表行?

Place the test inside the while loop. 将测试放在while循环中。

Its also a good idea to first initialize $classvar to nothing for the cases where $row['col1'] is neither foo or bar 对于$row['col1']既不是foobar的情况,首先将$classvar初始化$classvar也是个好主意

Also a nice way of keeping the string concatenation easy to read is to use the {} around the {$array['key']} then you dont have to keep concatenating with the dot all the time 同时保持字符串连接易于阅读的一个很好的方式是使用{}将围绕{$array['key']}那么你没有跟上点连接所有的时间

And as someone else mentioned a class name must be inside either "" or '' 并且正如其他人提到的,类名必须在""''

while($row = $queryresult->fetch_assoc()) {
    $classvar = '';
    if ($row['col1'] == "foo") {
        $classvar = 'class="foo"';
    } 
    if ($row['col1'] == "bar") {
        $classvar = 'class="bar"' ;
    }
    echo "<tr><td {$classvar}>{$row['col1']}</td><td>{$row['col2']}</td></tr>";
}

Change elseif to if into your condition. 如果进入你的状态,将elseif改为。

while($row = $queryresult->fetch_assoc()) {

     if ($row['col1'] == "foo") {
         $classvar = " class=foo";
     } 
     if ($row['col1'] == "bar") {
         $classvar = " class=bar";
     }

        echo "<tr><td",$classvar,">",$row['col1'],"</td><td>",$row['col2'],"</td></tr>";
}

Replace 更换

echo "<tr><td>",$row['col1'],"</td><td>",$row['col2'],"</td></tr>";

to

echo "<tr><td ".$classvar.">".$row['col1']."</td><td>".$row['col2']."</td></tr>";

I prefer the templating approach: 我更喜欢模板方法:

<?php while($row = $queryresult->fetch_assoc()) { ?>
  <tr>
    <td class="<?=($row['col1'] == "foo") ? 'foo' : 'bar'?>"><?=html($row['col1'])?></td>
    <td><?=html($row['col2'])?></td>
  </tr>
<?php } ?>

Include this function first: 首先包括此功能:

function html($str){
  return htmlspecialchars($str, ENT_QUOTES);
}

As you can see above, I'm using a combination of <?='string'?> which is shorthand for <?php echo 'string' ?> . 如上所示,我使用的是<?='string'?>的组合,它是<?php echo 'string' ?> echo'tring <?='string'?>的简写。

And I also use the ternary operator: statement ? if_true : if_false 而且我也使用三元运算符: statement ? if_true : if_false statement ? if_true : if_false . statement ? if_true : if_false

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

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